Hough变换:将极坐标转换回笛卡尔坐标,但仍无法绘制它们 [英] Hough Transform: Converted polar coordinates back to Cartesian, but still can't plot them

查看:229
本文介绍了Hough变换:将极坐标转换回笛卡尔坐标,但仍无法绘制它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经实现了Hough变换的每个部分,除了实际将线条绘制回原始图像。

So I have already implemented every part of a Hough Transform on my own, except for actually plotting the lines back onto the original image.

我可以设置我的我喜欢这样的数据数组。

I can set up my array of data that I have like this.

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 

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

The points are the points that were converted from the peaks in Polar Coordinates. So now I need to draw all six of these lines and I have had no luck.

编辑

所以我尝试根据建议改变我的代码。这就是我想出来的。

So I tried to change my code based off suggestions. This is what I came up with.

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

我不得不改变 r == 0 th == 0 因为 th = 0 当r不为0时,会给出 NAN 错误。

I had to change from r==0 to th==0 because th=0 would give NAN errors when r was not 0.

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

Based off the peaks, I then used that to get the data I needed to then calculate some values... but for some reason this isn't plotting well.

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

If you notice the + 11 for both y values. I had to do that to get the lines to be where they need to. I have a feeling something else went wrong.

我确实改变了它,以便我的Rho值现在都是正值。

I did change it so that my Rho values are all now positive.

推荐答案

如果你回想起霍夫空间的参数化,那么 rho theta 到<之间的直接关系code> x,y 是:

If you recall from the parameterization of the Hough space, the direct relation between rho,theta to x,y is:

rho = x*cos(theta) + y*sin(theta)

请记住 x,y 分别代表位置。此外,原点定义在图像的左上角角落。现在你要绘制线的等式,你有 rho theta 。只需重新排列等式,以便您可以求解格式行的等式 y = mx + b

Bear in mind that x,y represent the column and row location respectively. In addition, the origin is defined at the top-left corner of the image. Now that you want to plot the equation of the line, you have your rho and theta. Simply re-arrange the equation so that you can solve for an equation of the line of the form y = mx + b:

因此,只需循环遍历每个 rho theta ,并绘制一条从原点开始的行 x = 0 达到图像限制 x = width-1 。但是,因为MATLAB是1索引的,我们需要从 x = 1 x = width 。假设您的 rho theta 存储在相同长度的单独数组中,并且您的边缘图像存储在 im ,你可以这样做:

As such , simply loop over each rho and theta you have and draw a line that starts from the origin at x = 0 up to the limit of your image x = width-1. However, because MATLAB is 1-indexed, we need to go from x = 1 to x = width. Supposing that your rho and theta are stored in separate arrays of the same lengths and you have your edge image stored in im, you can do something like this:

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

以上代码非常简单。首先,使用 imshow 在MATLAB中。接下来,使用按住,这样我们就可以在图像顶部绘制我们的线条。接下来,我们计算有多少 rho,theta 对,然后我们将两个 x 坐标定义为1和 width 因为我们将使用这些来确定开始和结束 y 坐标的位置,给定这些 x 坐标。接下来,对于我们拥有的每个 rho,theta 对,确定相应的 y 坐标,然后使用 从中绘制一条线开头和结尾(x,y)坐标为蓝色。我们重复此操作,直到我们用完行。

The above code is pretty simple. First, show the image using imshow in MATLAB. Next, use hold on so we can draw our lines in the image that will go on top of the image. Next, we calculate how many rho,theta pairs there are, and then we define the two x coordinates to be 1 and width as we will use these to determine where the starting and ending y coordinates are, given these x coordinates. Next, for each rho,theta pair we have, determine the corresponding y coordinates, then use line to draw a line from the starting and ending (x,y) coordinates in blue. We repeat this until we run out of lines.

如果生成的 y 坐标,请不要惊慌超出图像范围。 将足够聪明,只需限制结果。

Don't be alarmed if the y coordinates that are produced go out of bounds in the image. line will be intelligent enough to simply cap the result.

上面的代码可以假设您在Hough变换中没有检测到垂直线,或者 theta = 0 。如果 theta = 0 (就像你的情况一样),这意味着我们有一条垂直线,这将产生一个无限的斜率和我们的公式 y = mx + b 无效。如果 theta = 0 ,该行的等式变为 x = rho 。因此,你需要一个额外的 if 语句,你的循环中会检测到这个:

The above code works assuming that you have no vertical lines detected in the Hough Transform, or when theta = 0. If theta = 0 (like in your case), this means that we have a vertical line which would thus produce an infinite slope and our formulation of y = mx + b is invalid. Should theta = 0, the equation of the line becomes x = rho. As such, you will need an additional if statement inside your loop that will detect this:

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






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


In order to draw the vertical line, I need to know how high the image is so that we can draw a vertical line from the top of the image (y = 1) down to the bottom of the image (y = height) which is anchored at x = rho. As such, the above code should now properly handle any line, as well as the degenerate case when the slope is infinite. Therefore, this second version of the code is what you're after.

祝你好运!

这篇关于Hough变换:将极坐标转换回笛卡尔坐标,但仍无法绘制它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆