如何在实际图像上使用houghlines而不是在hough图中检测线条

时间:2013-08-15 11:39:59

标签: matlab image-processing hough-transform

我想检测文本文档中的行。以下original image被侵蚀,以便使用erode函数更轻松地完成边缘检测任务。这是eroded image

现在检测我使用的行houghlines,并在我的脚本文件中使用以下代码。

I  = imread('c:\new.jpg');
rotI = imrotate(I,33,'crop');
bw_I = rgb2gray(rotI);
BW = edge(bw_I,'canny');
[H,T,R] = hough(BW);
imshow(H,[],'XData',T,'YData',R,...
            'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
P  = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
plot(x,y,'s','color','white');
% Find lines and plot them
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
figure, imshow(rotI), hold on
max_len = 0;
for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');

   % Plot beginnings and ends of lines
   plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
   plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');

   % Determine the endpoints of the longest line segment
   len = norm(lines(k).point1 - lines(k).point2);
   if ( len > max_len)
      max_len = len;
      xy_long = xy;
   end
end

% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','blue');

这产生了this result。现在我知道交叉点是检测到的线。我想要的是以某种方式显示在原始图像上检测到的这些线条,例如突出显示线条或加下划线。这可能吗?我会使用哪种功能?

编辑:我想说的是如何将检测到的线(交叉点)从最后一个结果转换为更清晰的结果。

1 个答案:

答案 0 :(得分:2)

您想将imshow应用于edge函数调用的结果。

This part of the Matlab documentation解释了您要完成的任务:

  1. 将图像读入MATLAB工作区。

    I  = imread('circuit.tif');
    
  2. 对于此示例,使用imrotate功能旋转并裁剪图像。

    rotI = imrotate(I,33,'crop');
    fig1 = imshow(rotI);
    
  3. 使用edge功能查找图像中的边缘。

    BW = edge(rotI,'canny');
    figure, imshow(BW);
    
  4. 这是你追求的第3步。您已经运行了edge功能 现在,剩下的就是使用imshow将结果BW可视化。