【问题标题】:convert an image from Cartesian to Polar将图像从笛卡尔坐标转换为极坐标
【发布时间】:2016-04-14 04:55:54
【问题描述】:

我正在尝试将具有许多具有相同中心的圆的图像从笛卡尔坐标转换为极坐标(这样新图像将是圆,但线而不是圆,请参见下图),这正在解决使用以下代码就可以了:

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

问题是,我不明白为什么它甚至可以工作? (显然这不是我的代码),我的意思是,当我使用函数 cart2pol 时,我什至不使用图像,它只是从 meshgrid 函数生成的一些向量 x 和 y .. 另一个问题是,我想以某种方式获得一个新图像(不仅仅是能够使用 wrap 函数绘制它),它是原始图像,而是通过 theta 和 rho 坐标(意味着相同的像素但重新排列)......我什至不知道怎么问这个,最后我想要一个矩阵的图像,这样我就可以对每一行求和并将矩阵转换为列向量......

【问题讨论】:

    标签: image matlab image-processing polar-coordinates


    【解决方案1】:

    您可以将图像视为一个二维矩阵,其中每个像素都有一个 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 methodsgriddata,找出最适合您的方案。

    还有几个其他问题(例如中心的四舍五入)导致结果略微不正确。下面提供了一个完整的工作示例

    % 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')
    

    结果

    【讨论】:

    • 谢谢,但是当我尝试添加应该“创建”新图像的代码时,我收到了这个错误:错误使用 griddedInterpolant 示例值必须是单数组或双数组。 interp2>makegriddedinterp 中的错误(第 228 行) F = griddedInterpolant(varargin{:}); interp2 中的错误(第 136 行) F = makegriddedinterp(X, Y, V, method,extrap); scanPol 错误(第 91 行)theta_rho_image = interp2(theta, rho, img, T, R);
    • @RotemElharar 发生这种情况是因为为了执行插值,必须将像素值转换为浮点数(它们目前可能是整数)。为此,只需将interp2(theta, rho, img, T, R) 替换为interp2(theta, rho, double(img), T, R)
    • 事情仍然不对:(我现在收到此错误:使用 interp2 时出错>makegriddedinterp(第 237 行)输入网格不是有效的 MESHGRID。interp2 中的错误(第 136 行)F = makegriddedinterp (X, Y, V, method,extrap); scanPol 中的错误(第 91 行)theta_rho_image = interp2(theta, rho, double(img), T, R);
    • @RotemElharar 对此感到抱歉。 theta, rho 点是分散的(不是在规则网格上),所以我们必须使用 griddata 代替它,它具有相同的格式但处理分散的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多