examples to convert image to polar coordinates do it explicitly - want a slick matrix method

17,812

Solution 1

given a matrix M (just a 2d Gaussian for this example), and a known origin point (X0,Y0) from which the polar transform takes place, we expect that iso-intensity circles will transform to iso-intensity lines:

 M=fspecial('gaussian',256,32); % generate fake image
 X0=size(M,1)/2; Y0=size(M,2)/2;
 [Y X z]=find(M);
 X=X-X0; Y=Y-Y0;
 theta = atan2(Y,X);
 rho = sqrt(X.^2+Y.^2);

 % Determine the minimum and the maximum x and y values:
 rmin = min(rho); tmin = min(theta);
 rmax = max(rho); tmax = max(theta);

 % Define the resolution of the grid:
 rres=128; % # of grid points for R coordinate. (change to needed binning)
 tres=128; % # of grid points for theta coordinate (change to needed binning)

 F = TriScatteredInterp(rho,theta,z,'natural');

 %Evaluate the interpolant at the locations (rhoi, thetai).
 %The corresponding value at these locations is Zinterp:

 [rhoi,thetai] = meshgrid(linspace(rmin,rmax,rres),linspace(tmin,tmax,tres));
 Zinterp = F(rhoi,thetai);

 subplot(1,2,1); imagesc(M) ; axis square
 subplot(1,2,2); imagesc(Zinterp) ; axis square

enter image description here

getting the wrong (X0,Y0) will show up as deformations in the transform, so be careful and check that.

Solution 2

I notice that the answer from bla is from polar to cartesian coordinates. However the question is in the opposite direction.

I=imread('output.png');   %read image
I1=flipud(I);
A=imresize(I1,[1024 1024]);
A1=double(A(:,:,1));
A2=double(A(:,:,2));
A3=double(A(:,:,3));  %rgb3 channel to double
[m n]=size(A1);
[t r]=meshgrid(linspace(-pi,pi,n),1:m); %Original coordinate

M=2*m;
N=2*n;
[NN MM]=meshgrid((1:N)-n-0.5,(1:M)-m-0.5);
T=atan2(NN,MM);
R=sqrt(MM.^2+NN.^2);                  

B1=interp2(t,r,A1,T,R,'linear',0);
B2=interp2(t,r,A2,T,R,'linear',0);
B3=interp2(t,r,A3,T,R,'linear',0); %rgb3 channel Interpolation
B=uint8(cat(3,B1,B2,B3));        

subplot(211),imshow(I);  %draw the Original Picture
subplot(212),imshow(B);  %draw the result
Share:
17,812
user1751512
Author by

user1751512

Updated on June 25, 2022

Comments

  • user1751512
    user1751512 almost 2 years

    I am trying to convert an image from cartesian to polar coordinates.

    I know how to do it explicitly using for loops, but I am looking for something more compact.

    I want to do something like:

    [x y] = size(CartImage);
    minr = floor(min(x,y)/2);
    
    r = linspace(0,minr,minr);
    phi = linspace(0,2*pi,minr);
    
    [r, phi] = ndgrid(r,phi);
    
    PolarImage = CartImage(floor(r.*cos(phi)) + minr, floor(r.sin(phi)) + minr);
    

    But this obviously doesn't work.

    Basically I want to be able to index the CartImage on a grid.

    The polar image would then be defined on the grid.

  • user1751512
    user1751512 over 11 years
    Dear Nate - Thank you for your answer - this is exactly what I was looking for... could you tell me however why the top left corner of the polar image is blue and not red? Thanks.
  • bla
    bla over 11 years
    It is actually a NaN value (and not zero) on the corners. The reason is in the method of the interpolation I used when I interpolated with TriScatteredInterp. You can change 'natural' to 'nearest' if it bothers you. This should solve it.