Convert image from Cartesian to Polyarny

I am trying to convert an image with many circles with the same center, from Cartesian to polar (so that the new image will be circles and lines instead of circles, see image below), and that development just use 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 that I do not understand why this works? (obviously this is not my code), I mean, when I use the cart2pol function, I don’t even use the image, these are just some of the vectors x and y generated from the meshgrid function. and another problem is that I want to somehow have a new image (and not just draw it using the wrapper function), which is the original image, but using theta and rho coordinates (which means the same pixels, but reordered ) ... I don’t even know how to do this, in the end I want to have an image that is a matrix so that I can sum each row and turn the matrix into a column vector ...

example

+4
1

, X Y

[(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)]

(X, Y) (R, ), floor(c/2) floor(r/2) .

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

, , (1,1), (r,theta). , , .

, , :

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

(X, Y) ( ,

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

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

, warp , , " img at (X, Y) (theta, rho)"

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

, 2D-, [nTheta, nRho]. griddata (theta, rho) ( warp ) .

% 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);

griddata, , .

(, ), .

% 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')

enter image description here

+3

Source: https://habr.com/ru/post/1623565/


All Articles