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

user1751512 picture user1751512 · Oct 17, 2012 · Viewed 17.6k times · Source

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.

Answer

bla picture bla · Oct 17, 2012

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.