Aydo
Aydo

Reputation: 416

Image to Polar Transformation

I need to transform a black/white image to polar coordinates. The original image is saved in a matrix. Right now, I'm iterating through each pixel of the original image and I calculate the polar transformation of each pixel like this:

originX = 0;
originY = 0;



for x = 1:columns
    for y = 1:rows
        r = sqrt((x-originX)^2 + (y-originY)^2);
        a = atand((y-originY)/(x-originX));

        polarTrans(r, a) = origImage(x,y);
    end
end

The problem is, the calculated new positions for the pixels are NOT positive integer values, so I can't simply save them into another matrix polarTrans. Do you have any suggestions here? How else should I save the transformed image if not in a matrix?

Upvotes: 1

Views: 912

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

Vectorize, my friend.

% compute all [x,y] pairs for the whole image 
[x,y]=meshgrid(1:columns -Xorigin,1:rows -Yorigin);

% Ta-da! 
[alpha,rho]=cart2pol(x,y)

Now pixel [i,j] is [x(i,j), y(i,j)] in cartesian coordinates and [rho(i,j),alpha(i,j)] in polar coordinates. You have no need of storing originImage in any other way. Whenever you want to know the polar coordinates of an specific pixel value, you just do [rho(i,j),alpha(i,j)], for originImage(i,j) pixel value.

Upvotes: 1

Related Questions