霍夫变换:将极坐标转换回笛卡尔坐标,但仍无法绘制它们

Chr*_*nes 1 matlab image-processing hough-transform

所以我已经自己实现了霍夫变换的每个部分,除了实际绘制线条回原始图像.

我可以设置我喜欢的数据数组.

points | theta | rho
-------|-------|----
[246,0]   -90    -246
[128,0]   -90    -128
[9,0]     -90     -9
[0,9]      0      9     
[0,128]    0     128
[0,246]    0     246 
Run Code Online (Sandbox Code Playgroud)

这些点是极坐标中的峰转换而来的点.所以现在我需要绘制所有这六条线并且我没有运气.

编辑


所以我尝试根据建议更改我的代码.这就是我提出的.

function help(img, outfile, peaks, rho, theta)
    imshow(img);
    x0 = 1;
    xend = size(img,2); 
    peaks_len=length(peaks);
    for i=1:peaks_len
        peak=peaks(i,:);
        r_ind=peak(1);
        t_ind=peak(2);
        r=rho(r_ind);
        th=theta(t_ind);
        %display([r,th,peak]);

        %// if a vertical line, then draw a vertical line centered at x = r
%         display([r, th]);

        if (th == 0)
            display('th=0');
            display([1, size(img,1)]);
            line([r r], [1 size(img,1)], 'Color', 'green');
        else
            %// Compute starting y coordinate
            y0 = abs((-cosd(th)/sind(th))*x0 + (r / sind(th)))+11;%-25; 

            %// Compute ending y coordinate
            yend = abs((-cosd(th)/sind(th))*xend + (r / sind(th)))+11;%-25;
            display('y');
            display([y0, yend]);
            display('x');
            display([x0 xend]);
             %// Draw the line
            line([x0 xend], [y0 yend], 'Color', 'green');
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

我不得不改变r==0,th==0因为当r不为0时th=0NAN出错.

基于峰值,我然后使用它来获取我需要的数据然后计算一些值...但由于某种原因,这不是很好的绘图.

如果你注意到两个y值都是+ 11.我必须这样做才能让线条成为他们需要的地方.我有一种感觉出现了其他问题.

我确实改变了它,所以我的Rho值现在都是正面的.

ray*_*ica 5

如果从霍夫空间之间的直接关系的参数召回rho,thetax,y是:

rho = x*cos(theta) + y*sin(theta)
Run Code Online (Sandbox Code Playgroud)

请记住,分别x,y代表位置.此外,原点是在定义左上角的图像角.既然你想绘制线的等式,你就得到了你的rhotheta.只需重新排列等式,以便您可以求解表格线的等式y = mx + b:

因此,只需循环遍历每个rho,theta然后绘制一条从原点开始x = 0直到图像极限的线x = width-1.然而,由于MATLAB是1指数的,我们需要去x = 1x = width.假设您rhotheta存储在相同长度的单独数组中并且存储了边缘图像im,您可以执行以下操作:

imshow(im); %// Show the image
hold on; %// Hold so we can draw lines
numLines = numel(rho); %// or numel(theta);

%// These are constant and never change
x0 = 1;
xend = size(im,2); %// Get the width of the image

%// For each rho,theta pair...
for idx = 1 : numLines
    r = rho(idx); th = theta(idx); %// Get rho and theta
    %// Compute starting y coordinate
    y0 = (-cosd(th)/sind(th))*x0 + (r / sind(th)); %// Note theta in degrees to respect your convention

    %// Compute ending y coordinate
    yend = (-cosd(th)/sind(th))*xend + (r / sind(th));

    %// Draw the line
    line([x0 xend], [y0 yend], 'Color', 'blue');
end
Run Code Online (Sandbox Code Playgroud)

上面的代码非常简单.首先,imshow在MATLAB中显示图像.接下来,使用hold on这样我们可以在图像中绘制我们的线条.接下来,我们计算rho,theta有多少对,然后我们将两个x坐标定义为1,并且widthy给定这些x坐标的情况下,我们将使用它们来确定起始坐标和结束坐标的位置.接下来,对于rho,theta我们拥有的每一对,确定相应的y坐标,然后使用line(x,y)蓝色的起始坐标和结束坐标绘制一条线.我们重复这个,直到我们用完线.

如果生成的y坐标超出图像范围,请不要惊慌. line将足够聪明,可以简单地限制结果.

什么时候 theta = 0

假设您在Hough变换中没有检测到垂直线,或者何时,上面的代码可以正常工作theta = 0.如果theta = 0(在你的情况下),这意味着我们有一条垂直线,因此会产生无限的斜率,我们的表述y = mx + b无效.应该theta = 0,线的方程变为x = rho.因此,您需要if在循环内部使用另一个语句来检测:

imshow(im); %// Show the image
hold on; %// Hold so we can draw lines
numLines = numel(rho); %// or numel(theta);

%// These are constant and never change
x0 = 1;
xend = size(im,2); %// Get the width of the image

%// For each rho,theta pair...
for idx = 1 : numLines
    r = rho(idx); th = theta(idx); %// Get rho and theta

    %// if a vertical line, then draw a vertical line centered at x = r
    if (th == 0)
        line([r r], [1 size(im,1)], 'Color', 'blue');
    else
        %// Compute starting y coordinate
        y0 = (-cosd(th)/sind(th))*x0 + (r / sind(th)); %// Note theta in degrees to respect your convention

        %// Compute ending y coordinate
        yend = (-cosd(th)/sind(th))*xend + (r / sind(th));

        %// Draw the line
        line([x0 xend], [y0 yend], 'Color', 'blue');
   end
end
Run Code Online (Sandbox Code Playgroud)

为了绘制垂直线,我需要知道图像的高度,以便我们可以从图像的顶部(y = 1)向下绘制到y = height锚定在的图像()的底部的垂直线x = rho.因此,上面的代码现在应该正确处理任何行,以及当斜率无限时的退化情况.因此,代码的第二个版本就是你所追求的.


祝好运!