您可以将图像视为一个二维矩阵,其中每个像素都有一个 X 和 Y 坐标
[(1,1) (1,2) (1,3) .... (1,c)]
[(2,1) (2,2) (2,3) .... (2,c)]
[(3,1) (3,2) (3,3) .... (3,c)]
[.... .... .... .... .... ]
[(r,1) (r,2) (r,3) .... (r,c)]
在您发布的代码中,它使用图像的中心floor(c/2) 和floor(r/2) 作为参考点,将这些 (X,Y) 坐标中的每一个映射到它的等效极坐标 (R, theta)。
% Map pixel value at (1,1) to it's polar equivalent
[r,theta] = cart2pol(1 - floor(r/2),1 - floor(c/2));
因此,用于 (1,1) 的任何像素值现在都应该出现在您的新极坐标空间 (r,theta) 中。需要注意的是,要进行这种转换,图像中实际像素值的信息并不重要,我们只想对图像中的每个像素执行这种转换。
所以首先我们要弄清楚图像的中心在哪里:
[r, c] = size(img);
r = floor(r / 2);
c = floor(c / 2);
然后我们计算出图像中每个点的 (X,Y) 坐标(在减去中心之后
[X, Y] = meshgrid(-c:c-1,-r:r-1);
现在将所有这些笛卡尔点转换为极坐标
[theta, rho] = cart2pol(X, Y);
warp 现在所做的就是说“在 (X,Y) 处 (theta, rho) 的相应位置显示 img 的值”
warp(theta, rho, zeros(size(theta)), img);
现在您似乎想要一个新的 2D 图像,其尺寸为 [nTheta, nRho]。为此,您可以使用griddata 将分散的(theta, rho)图像(由上面的warp 显示)插入到常规网格中。
% These is the spacing of your radius axis (columns)
rhoRange = linspace(0, max(rho(:)), 100);
% This is the spacing of your theta axis (rows)
thetaRange = linspace(-pi, pi, 100);
% Generate a grid of all (theta, rho) coordinates in your destination image
[T,R] = meshgrid(thetaRange, rhoRange);
% Now map the values in img to your new image domain
theta_rho_image = griddata(theta, rho, double(img), T, R);
查看所有interpolation methods 和griddata,找出最适合您的方案。
还有几个其他问题(例如中心的四舍五入)导致结果略微不正确。下面提供了一个完整的工作示例
% Create an image of circles
radii = linspace(0, 40, 10);
rows = 100;
cols = 100;
img = zeros(rows, cols);
for k = 1:numel(radii)
t = linspace(0, 2*pi, 1000);
xx = round((cos(t) * radii(k)) + (cols / 2));
yy = round((sin(t) * radii(k)) + (rows / 2));
toremove = xx > cols | xx < 1 | yy > rows | yy < 1;
inds = sub2ind(size(img), xx(~toremove), yy(~toremove));
img(inds) = 1;
end
[r,c] = size(img);
center_row = r / 2;
center_col = c / 2;
[X,Y] = meshgrid((1:c) - center_col, (1:r) - center_row);
[theta, rho] = cart2pol(X, Y);
rhoRange = linspace(0, max(rho(:)), 1000);
thetaRange = linspace(-pi, pi, 1000);
[T, R] = meshgrid(thetaRange, rhoRange);
theta_rho_image = griddata(theta, rho, double(img), T, R);
figure
subplot(1,2,1);
imshow(img);
title('Original Image')
subplot(1,2,2);
imshow(theta_rho_image);
title('Polar Image')
结果