【问题标题】:Finding major axis/image orientation of binary image in R在R中查找二值图像的主轴/图像方向
【发布时间】:2013-04-17 04:32:49
【问题描述】:

我有一个高分辨率的二进制图像,看起来像:

我正在尝试计算应该稍微向右旋转的主轴并最终获得对象的方向轴

here 的帖子(在 matlab 中)建议一种方法是计算数据点的协方差矩阵并找到它们的特征值/特征向量

我正在尝试在 R 中实现类似的东西

%% MATLAB CODE Calculate axis and draw

[M N] = size(Ibw);
[X Y] = meshgrid(1:N,1:M);

%Mass and mass center
m = sum(sum(Ibw));
x0 = sum(sum(Ibw.*X))/m;
y0 = sum(sum(Ibw.*Y))/m;

#R code

d = dim(im)
M = d[1]
N = d[2]

t = meshgrid(M,N)
X = t[[2]]
Y = t[[1]]
m = sum(im);
x0 = sum(im %*% X)/m;
y0 = sum(im %*% Y)/m;

meshgrid <-function(r,c){
  return(list(R=matrix(rep(1:r, r), r, byrow=T), 
          C=matrix(rep(1:c, c), c)))
}

但是,在 R 中计算 mx0y0 需要很长时间。

有人知道 R 中的实现吗?

【问题讨论】:

  • 您可以使用system.time() 找出哪一行花费的时间更多吗?
  • 它的矩阵乘法。我的头像是2736x3648
  • 哪个操作系统?
  • Mac OSX x64 山狮
  • 由于您的图像是由直边组成的,您也可以使用Hough transform 来检测它们。

标签: image r matlab image-processing binary-data


【解决方案1】:

使用var 直接计算方差矩阵需要 1/3 秒。

# Sample data
M <- 2736
N <- 3648
im <- matrix( FALSE, M, N );
y <- as.vector(row(im))
x <- as.vector(col(im))
im[ abs( y - M/2 ) < M/3 & abs( x - N/2 ) < N/3 ] <- TRUE
#image(im)
theta <- runif(1, -pi/12, pi/12)
xy <- cbind(x+1-N/2,y+1-M/2) %*% matrix(c( cos(theta), sin(theta), -sin(theta), cos(theta) ), 2, 2)
#plot(xy[,1]+N/2-1, xy[,2]+M/2-1); abline(h=c(1,M),v=c(1,N))
f <- function(u, lower, upper) pmax(lower,pmin(round(u),upper))
im[] <- im[cbind( f(xy[,2] + M/2 - 1,1,M), f(xy[,1] + N/2 - 1,1,N) )]
image(1:N, 1:M, t(im), asp=1)

# Variance matrix of the points in the rectangle
i <- which(im)
V <- var(cbind( col(im)[i], row(im)[i] ))
# Their eigenvectors
u <- eigen(V)$vectors
abline( M/2-N/2*u[2,1]/u[1,1], u[2,1]/u[1,1], lwd=5 )
abline( M/2-N/2*u[2,2]/u[1,2], u[2,2]/u[1,2] )

【讨论】:

  • 干净。简洁的。快速地。谢谢!
【解决方案2】:

尝试用来自this link 的合适的Rblas.dll 替换默认的Rblas.dll。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    相关资源
    最近更新 更多