2015-02-05 65 views
1

所以我已经实现了霍夫变换的每个部分,除了实际将线条绘制回原始图像之外。霍夫变换:将极坐标转换回笛卡儿,但仍然无法绘制它们

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

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 

点在于被极坐标转换峰的点。所以现在我需要画出所有这六行,而且我没有运气。

编辑


所以我试图改变基于关我的代码的建议。这是我想出的。

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会给NAN错误时根据离峰r为不为0

,然后我用,要得到我需要再计算一些数据价值......但由于某种原因,这不是很好的策划。

如果您注意到两个y值的+ 11。我必须这样做才能使线路成为他们需要的地方。我有一种感觉,还有其他的错误。

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

+0

您没有正确使用“abs”。我的意思是在积累阶段,而不是绘画阶段。画线时不要拿“abs”。当你在变换阶段计算'rho'时,在那里取绝对值。你似乎误解了我。 – rayryeng 2015-02-06 01:29:08

+0

我实际上完全改变了我的rho,所以没有负面因素。 – 2015-02-06 01:54:06

+0

这很好。绘制线条时不要使用“abs”。它们不会出现在图像上,因为它们在技术上会超出范围。 – rayryeng 2015-02-06 02:09:28

回答

4

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

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

记住,x,y分别代表位置。另外,图像的角落左上角定义了原点。现在你想绘制线的方程,你有你的rhotheta。简单地重新安排方程式,这样就可以解决的形式y = mx + b线的公式:

因此,简单地循环每个rhotheta你和借鉴,从开始的行起源于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 

上面的代码非常简单。首先,在MATLAB中使用imshow显示图像。接下来,使用hold on,以便我们可以在图像上画出我们的线条,并将其放在图像的顶部。接下来,我们计算出有多少个rho,theta对,然后我们将这两个坐标定义为x,1和width,因为我们将使用这些坐标来确定起始和结束坐标为y的坐标,如果这些坐标为x。接下来,对于我们每个rho,theta对,确定相应的y坐标,然后用line从蓝色起始和结束坐标绘制一条线。我们重复这个,直到我们用完线条。

如果产生的y坐标超出图像范围,请不要惊慌。 line将足够智能以简单地限制结果。

theta = 0

上面代码工作假设你有变换在霍夫没有检测到的垂直线时,或者当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 

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


祝你好运!

+0

那么第二种情况应该适用于所有线路,垂直线,水平线和其他线? – 2015-02-05 20:50:02

+0

@ChrisJones - 是!请记住,这不会处理负面的'rho',因为我假定您的Hough空间的原点位于图像的左上角......所以您将无法将这些视觉化。 – rayryeng 2015-02-05 20:51:17

+0

正如你所看到的,图像有负面的俄罗斯方块..我也放了一些错误的角度..这虽然工作非垂直线。 – 2015-02-05 21:04:40