【问题标题】:Polar-transform image in RR中的极坐标变换图像
【发布时间】:2013-05-22 12:16:01
【问题描述】:

我正在尝试将 R 中的图像(表示为矩阵)转换为原点为 0,0(左上角)的极坐标空间。鉴于215x215 矩阵x 看起来像:

x0 = as.vector(col(x)) y0 = as.vector(行(x)) r = sqrt( (x0^2) + (y0^2) )#x a = atan(y0/x0)#y m = as.matrix(data.frame(y=a, x=r)) m = 圆形(米) m[m>215] = 不适用 m[m==0] = 不适用 xn = x[m] xn = 矩阵(xn, 215, 215)

但是,xn 看起来像:

当我期望这个时:

知道我做错了什么吗?

【问题讨论】:

    标签: r image-processing transformation polar-coordinates atan2


    【解决方案1】:

    角度有问题:atan 返回以弧度表示的角度。 如果你四舍五入的话,剩下的信息不多了……

    尝试:

    a = atan(y0/x0) * 215 / (pi/2)
    

    这不是你期望的图像,这显然是逆变换, 中心在图像的中间。

    # Load the image
    library(png)
    library(RCurl)
    d <- readPNG( getBinaryURL( "http://i.stack.imgur.com/rMR3C.png" ) )
    image(d, col=gray(0:255/255))
    
    # Origin for the polar coordinates
    x0 <- ncol(d)/2
    y0 <- nrow(d)/2
    
    # The value of pixel (i,j) in the final image 
    # comes from the pixel, in the original image, 
    # with polar coordinates (r[i],theta[i]).
    # We need a grid for the values of r and theta.
    r <- 1:ceiling(sqrt( max(x0,nrow(d))^2 + max(y0,ncol(d))^2))
    theta <- -pi/2 + seq(0,2*pi, length = 200)
    r_theta <- expand.grid(r=r, theta=theta)
    
    # Convert those polar coordinates to cartesian coordinates:
    x <- with( r_theta, x0 + r * cos(theta) )
    y <- with( r_theta, y0 + r * sin(theta) )
    # Truncate them
    x <- pmin( pmax( x, 1 ), ncol(d) )
    y <- pmin( pmax( y, 1 ), nrow(d) )
    
    # Build an empty matrix with the desired size and populate it
    r <- matrix(NA, nrow=length(r), ncol=length(theta))
    r[] <- d[cbind(x,y)]
    image(r, col=gray(0:255/255))
    

    【讨论】:

    • 谢谢,很好的回答!你能解释一下代码的每一部分在做什么吗?对于您用于 r 和 theta 的公式以及 sin/cos 值的乘法,我有点困惑。再次感谢
    • 我稍微简化了代码并添加了一些 cmets。
    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多