将图像从笛卡儿转换为极地

Rot*_*rar 6 matlab image image-processing polar-coordinates

我正在尝试转换具有相同中心的多个圆圈的图像,从笛卡儿到极地(这样新图像将是圆形而不是圆形,请参见下图),这样就可以了以下代码:

[r, c] = size(img);
r=floor(r/2);
c=floor(c/2);
[X, Y] = meshgrid(-c:c-1,-r:r-1);
[theta, rho] = cart2pol(X, Y); 
subplot(221), imshow(img), axis on;
hold on;
subplot(221), plot(xCenter,yCenter, 'r+');
subplot(222), warp(theta, rho, zeros(size(theta)), img);
view(2), axis square;
Run Code Online (Sandbox Code Playgroud)

问题是,我不明白它为什么会起作用?(显然这不是我的代码),我的意思是,当我使用函数cart2pol时,我甚至不使用图像,它只是从meshgrid函数生成的一些向量x和y ..另一个问题是,我想要某种方式有一个新的图像(不仅仅是能够用环绕功能绘制它),这是原始图像,但是通过theta和rho坐标(意思是相同的像素,但重新排列)...我甚至不确定如何问这个,最后我想要一个矩阵的图像,以便我可以对每一行求和并将矩阵转换为列向量...

例

Sue*_*ver 7

您可以将图像视为一个 2D 矩阵,其中每个像素都有一个 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)]
Run Code Online (Sandbox Code Playgroud)

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));
Run Code Online (Sandbox Code Playgroud)

因此,无论用于 (1,1) 的像素值现在都应该出现在位于 的新极坐标空间中(r,theta)。需要注意的是,要进行此转换,图像中实际像素值的信息并不重要,我们只想对图像中的每个像素执行此转换。

首先我们找出图像的中心在哪里:

[r, c] = size(img);
r = floor(r / 2);
c = floor(c / 2);
Run Code Online (Sandbox Code Playgroud)

然后我们计算出图像中每个点的 (X,Y) 坐标(在减去中心之后)

[X, Y] = meshgrid(-c:c-1,-r:r-1);
Run Code Online (Sandbox Code Playgroud)

现在将所有这些笛卡尔点转换为极坐标

[theta, rho] = cart2pol(X, Y); 
Run Code Online (Sandbox Code Playgroud)

扭曲现在所做的就是“在 (X,Y)img的 (theta, rho) 相应位置显示 (X,Y) 的值”

warp(theta, rho, zeros(size(theta)), img);
Run Code Online (Sandbox Code Playgroud)

现在您似乎想要一个新的 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);
Run Code Online (Sandbox Code Playgroud)

查看所有插值方法,找出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')
Run Code Online (Sandbox Code Playgroud)

结果

在此输入图像描述