6

R의 이미지 (행렬로 표시)를 원점이 0,0 (극좌표) 인 극좌표 공간으로 변환하려고합니다.R의 극좌표 변환 이미지

enter image description here

0 :이 기대하는 경우

enter image description here

: 같은

enter image description here

 
x0 = as.vector(col(x)) 
y0 = as.vector(row(x)) 

r = sqrt((x0^2) + (y0^2) )#x 
a = atan(y0/x0)#y 
m = as.matrix(data.frame(y=a, x=r)) 

m = round(m) 
m[m>215] = NA 
m[m==0] = NA 

xn = x[m] 
xn = matrix(xn, 215, 215) 

그러나, XN 그냥 보이는 :처럼 보이는 215x215 매트릭스 x을 감안할 때

내가 뭘 잘못하고 있는지 알 겠어?

답변

9

각도에 문제가 있습니다 : atan 각도를 라디안 단위로 반환합니다. 그것은

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

Transformed image

하지 분명히 역변환 당신이 기대하는 이미지를 : 당신이 그것을 반올림 경우 는 함께 시도 ...

왼쪽 많은 정보가없는 이미지 가운데에 센터가 있습니다.

# 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)) 
+0

감사합니다. 코드의 각 부분이 무엇을하고 있는지 설명해 주시겠습니까? 나는 r과 theta와 sin/cos 값의 곱셈에 사용하는 수식에 대해서는 약간 혼란 스럽습니다. 다시 한 번 감사드립니다 – by0

+1

코드를 다소 간소화하고 몇 가지 의견을 추가했습니다. –