Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert an image from Cartesian to Polar

I'm trying to convert an image with many circles with the same center, from Cartesian to Polar (so that the new image will be the circles but lines instead of the circles, see the image below), and that's working out just fine using the following code:

[r, c] = size(img);
r=floor(r/2);
c=floor(c/2);
[X, Y] = meshgrid(-c:c-1,-r:r-1);
[theta, rho] = cart2pol(X, Y); 
subplot(221), imshow(img), axis on;
hold on;
subplot(221), plot(xCenter,yCenter, 'r+');
subplot(222), warp(theta, rho, zeros(size(theta)), img);
view(2), axis square;

The problem is, I don't understand why does it even work? (obviously it's not my code), I mean, when I use the function cart2pol I don't even use the image, it's just some vectors x and y generated from the meshgrid function.. and another problem is, I want somehow to have a new image (not just to be able to draw it with the wrap function) which is the original image but by the theta and rho coordinates (meaning the same pixels but rearranged)... I'm not even sure how to ask this, in the end I want to have an image which is a matrix so that I can sum each row and turn the matrix into a column vector...

example

like image 403
Rotem Elharar Avatar asked Jan 09 '16 15:01

Rotem Elharar


People also ask

How do you convert Cartesian to polar in Matlab?

[ theta , rho ] = cart2pol( x , y ) transforms corresponding elements of the two-dimensional Cartesian coordinate arrays x and y into polar coordinates theta and rho .

How do you convert to polar form?

To convert from rectangular coordinates to polar coordinates, use one or more of the formulas: cosθ=xr, sinθ=yr, tanθ=yx, and r=√x2+y2.

How do you convert polar to Cartesian form?

To convert from Cartesian coordinates to polar coordinates: r=√x2+y2 . Since tanθ=yx, θ=tan−1(yx) . So, the Cartesian ordered pair (x,y) converts to the Polar ordered pair (r,θ)=(√x2+y2,tan−1(yx)) .

Is Cartesian form same as polar form?

Although Cartesian coordinates can be used in three dimensions (x, y, and z), polar coordinates only specify two dimensions (r and θ). If a third axis, z (height), is added to polar coordinates, the coordinate system is referred to as cylindrical coordinates (r, θ, z).


1 Answers

You can think of your image as being a 2D matrix, where each pixel has an X and Y coordinate

[(1,1)    (1,2)    (1,3)   ....   (1,c)]
[(2,1)    (2,2)    (2,3)   ....   (2,c)]
[(3,1)    (3,2)    (3,3)   ....   (3,c)]
[....     ....     ....    ....   .... ]
[(r,1)    (r,2)    (r,3)   ....   (r,c)]

In the code that you posted, it maps each of these (X,Y) coordinates to it's equivalent polar coordinate (R, theta) using the center of the image floor(c/2) and floor(r/2) as the reference point.

% Map pixel value at (1,1) to it's polar equivalent
[r,theta] = cart2pol(1 - floor(r/2),1 - floor(c/2));

So whatever pixel value was used for (1,1) should now appear in your new polar coordinate space at (r,theta). It is important to note that to do this conversion, no information about the actual pixel values in the image matters, rather we just want to perform this transformation for each pixel within the image.

So first we figure out where the center of the image is:

[r, c] = size(img);
r = floor(r / 2);
c = floor(c / 2);

Then we figure out the (X,Y) coordinates for every point in the image (after the center has already been subtracted out

[X, Y] = meshgrid(-c:c-1,-r:r-1);

Now convert all of these cartesian points to polar coordinates

[theta, rho] = cart2pol(X, Y); 

All that warp now does, is say "display the value of img at (X,Y) at it's corresponding location in (theta, rho)"

warp(theta, rho, zeros(size(theta)), img);

Now it seems that you want a new 2D image where the dimensions are [nTheta, nRho]. To do this, you could use griddata to interpolate your scattered (theta, rho) image (which is displayed by warp above) to a regular grid.

% These is the spacing of your radius axis (columns)
rhoRange = linspace(0, max(rho(:)), 100);

% This is the spacing of your theta axis (rows)
thetaRange = linspace(-pi, pi, 100);

% Generate a grid of all (theta, rho) coordinates in your destination image
[T,R] = meshgrid(thetaRange, rhoRange);

% Now map the values in img to your new image domain
theta_rho_image = griddata(theta, rho, double(img), T, R);

Take a look at all the interpolation methods for griddata to figure out which is most appropriate for your scenario.

There were a couple other issues (like the rounding of the center) which caused the result to be slightly incorrect. A fully working example is provided below

% Create an image of circles
radii = linspace(0, 40, 10);

rows = 100;
cols = 100;
img = zeros(rows, cols);

for k = 1:numel(radii)
    t = linspace(0, 2*pi, 1000);
    xx = round((cos(t) * radii(k)) + (cols / 2));
    yy = round((sin(t) * radii(k)) + (rows / 2));

    toremove = xx > cols | xx < 1 | yy > rows | yy < 1;

    inds = sub2ind(size(img), xx(~toremove), yy(~toremove));

    img(inds) = 1;
end

[r,c] = size(img);
center_row = r / 2;
center_col = c / 2;

[X,Y] = meshgrid((1:c) - center_col, (1:r) - center_row);

[theta, rho] = cart2pol(X, Y);

rhoRange = linspace(0, max(rho(:)), 1000);
thetaRange = linspace(-pi, pi, 1000);

[T, R] = meshgrid(thetaRange, rhoRange);

theta_rho_image = griddata(theta, rho, double(img), T, R);

figure
subplot(1,2,1);
imshow(img);
title('Original Image')

subplot(1,2,2);
imshow(theta_rho_image);
title('Polar Image')

And the result

enter image description here

like image 198
Suever Avatar answered Oct 05 '22 06:10

Suever