4

我正在尝试将图像从笛卡尔坐标转换为极坐标。

我知道如何使用 for 循环明确地做到这一点,但我正在寻找更紧凑的东西。

我想做类似的事情:

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

但这显然行不通。

基本上我希望能够在网格上索引 CartImage。

然后将在网格上定义极坐标图像。

4

2 回答 2

5

给定一个矩阵 M(本例中只是一个 2d 高斯)和一个发生极坐标变换的已知原点 (X0,Y0),我们预计等强度圆将转换为等强度线:

 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

在此处输入图像描述

弄错 (X0,Y0) 会在变换中显示为变形,所以要小心检查。

于 2012-10-17T03:17:52.180 回答
4

我注意到 bla 的答案是从极坐标到笛卡尔坐标。然而,问题是在相反的方向。

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
于 2015-05-05T14:21:19.787 回答