How to change the image from Cartesian coordinates to polar coordinates in Matlab?

I am trying to convert the image pixels from the xy coordinate to a polar coordinate, and I have a problem with it, since I want to write this function myself. Here is the code I've done so far:

function [ newImage ] = PolarCartRot % read and show the image image= imread('1.jpg'); %%imshow(image); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %change to polar coordinate [xyz]= size(image); r = sqrt(x*x+y*y); theta = atan2(y,x); for i =0:r for j= 0:theta newpixel = [i; j]; newImage(newpixel(1), newpixel(2),:) = image(i,j,:); end end figure; imshow (newImage); 
+1
source share
1 answer

It's not entirely clear what you are trying to do, so I am making my own example ...

Therefore, given the image, I convert the x / y coordinates of the pixel from Cartesian to polar using CART2POL .

The first figure shows the location of the points, and the second shows both the original image and the field with polar coordinates.

Please note that I am using the WARP function from the Image Processing Toolbox. Under the hood, it uses SURF / SURFACE to display a texture image.

 % load image load clown; img = ind2rgb(X,map); %img = imread(...); % or use any other image % convert pixel coordinates from cartesian to polar [h,w,~] = size(img); [X,Y] = meshgrid(1:w,1:h); [theta,rho] = cart2pol(X, Y); Z = zeros(size(theta)); % show pixel locations (subsample to get less dense points) XX = X(1:8:end,1:4:end); YY = Y(1:8:end,1:4:end); tt = theta(1:8:end,1:4:end); rr = rho(1:8:end,1:4:end); subplot(121), scatter(XX(:),YY(:),3,'filled'), axis ij image subplot(122), scatter(tt(:),rr(:),3,'filled'), axis ij square tight % show images figure subplot(121), imshow(img), axis on subplot(122), warp(theta, rho, Z, img), view(2), axis square 

pixel_coordsimage_warped


EDIT

As I originally stated, the question is not clear. You must accurately describe the mapping you need ...

To do this, you need to think about where the source is, before moving on to the polar coordinates. In the previous example, it is assumed that the origin is the base of the axes in (0,0) . Suppose you want to take the center of the image (w/2,h/2) as the source, then you will do this instead:

 [X,Y] = meshgrid((1:w)-floor(w/2), (1:h)-floor(h/2)); 

the rest of the code has not changed. To better illustrate the effect, consider the original image with concentric circles drawn in Cartesian coordinates, and notice how they are displayed on lines in polar coordinates when using the center of the circles as the origin:

concentric_circles


EDIT

Here is another example of how to display the image in polar coordinates, as indicated in the comments. Note that we are performing the mapping in the opposite direction pol2cart :

 [h,w,~] = size(img); s = min(h,w)/2; [rho,theta] = meshgrid(linspace(0,s-1,s), linspace(0,2*pi)); [x,y] = pol2cart(theta, rho); z = zeros(size(x)); subplot(121), imshow(img) subplot(122), warp(x, y, z, img), view(2), axis square tight off 

image_polar

Again, it’s better to show the effect if you give it an input image with straight lines and see how they are displayed in polar coordinates (vertical lines become circles, and horizontal lines become rays coming from the origin):

straight_lines

+13
source

All Articles