Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 417
user1751512 Avatar asked Jan 16 '23 07:01

user1751512


2 Answers

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.

like image 89
bla Avatar answered Feb 12 '23 03:02

bla


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
like image 25
Andln Avatar answered Feb 12 '23 01:02

Andln