2013-05-27 21 views
6

Sto provando a trasformare un'immagine (rappresentata come una matrice) in R, nello spazio di coordinate polari con l'origine essendo 0,0 (angolo in alto a sinistra). Data la 215x215 matrice x che si presenta come:Polar-trasforma l'immagine in R

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) 

Tuttavia, xn sembra proprio:

enter image description here

Quando mi aspetto che questo:

enter image description here

Qualche idea su cosa sto facendo male?

risposta

9

C'è un problema con l'angolo: atan restituisce un angolo in radianti. se intorno ad esso, non ci sono molte informazioni a sinistra ...

Prova con:

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

Transformed image

Non è l'immagine che ci si aspetta, che apparentemente è la trasformazione inversa, con il centro al centro dell'immagine.

# 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

Grazie, ottima risposta! Potresti forse spiegare che cosa fa ogni parte del codice? Sono un po 'confuso sulla formula che stai usando per r e theta e sulla moltiplicazione dei valori sin/cos. Grazie ancora – by0

+1

Ho un po 'semplificato il codice e ho aggiunto alcuni commenti. –