使用霍夫变换选择线

selecting line using hough transform

Problem: Find an unwanted line in an image using Hough transform.

我做了以下,

  1. 应用定向滤波器分析 12 个不同的方向,彼此旋转 15°。
  2. 应用阈值处理获得 12 个二值图像。

现在,我需要 select 标记为黄色的两张图片中的一张。因为,这两张图的线条最显眼

我试过下面的代码。好像不行。

MATLAB代码

    %   Read 12 images into workspace.
input_images  = {imread('1.png'),imread('2.png'),imread('3.png'),...
    imread('4.png'),imread('5.png'),imread('6.png'),...
    imread('7.png'),imread('8.png'),imread('9.png'),...
    imread('10.png'),imread('11.png'),imread('12.png')};

longest_line = struct('point1',[0 0], 'point2',[0 0], 'theta', 0, 'rho', 0);

for n=1:12
    %Create a binary image.
    binary_image = edge(input_images{n},'canny');

    %Create the Hough transform using the binary image.
    [H,T,R] = hough(binary_image);

    %Find peaks in the Hough transform of the image.
    P  = houghpeaks(H,3,'threshold',ceil(0.3*max(H(:))));

    %Find lines
    hough_lines = houghlines(binary_image,T,R,P,'FillGap',5,'MinLength',7);         
    longest_line = FindTheLongestLine(hough_lines, longest_line);
end


% Highlight the longest line segment by coloring it cyan.
plot(longest_line.point1, longest_line.point2,'LineWidth',2,'Color','cyan');

.

相关源代码

function longest_line = FindTheLongestLine( hough_lines , old_longest_line)
%FINDTHELONGESTLINE Summary of this function goes here
%   Detailed explanation goes here
    longest_line = struct('point1',[0 0] ,'point2',[0 0],'theta', 0, 'rho', 0);

    max_len = 0;

    N = length(hough_lines);

    for i = 1:N
       % Determine the endpoints of the longest line segment
       len = LenthOfLine(hough_lines(i));

       if ( len > max_len)
          max_len = len;
          longest_line = hough_lines(i);
       end
    end

    old_len = LenthOfLine(old_longest_line);
    new_len = LenthOfLine(longest_line);

    if(old_len > new_len)
       longest_line =  old_longest_line;
    end
end

function length = LenthOfLine( linex )
%LENTHOFLINE Summary of this function goes here
%   Detailed explanation goes here

    length = norm(linex.point1 - linex.point2);
end

测试图片

这是12张图片,drive.google.com/open?id=0B-2FDw63ZNTnRnEzYlNyS0V4YVE

您可以尝试根据您的具体问题更改 Hough 函数的参数,这不是一个完美的解决方案,但它可能对您来说已经足够好了:

img = im2double(rgb2gray(imread('line.jpg')));
% edge image
BW = edge(img,'canny');
% relevant angles (degrees) interval for the line you want
thetaInterval = -80:-70;
% run hough transform and take single peak
[H,T,R] = hough(BW,'Theta',thetaInterval);
npeaks = 1;
P = houghpeaks(H,npeaks);
% generate lines
minLen = 150; % you want the long line which is ~250 pixels long
% merge smaller lines (same direction) within gaps of 30 pixels
fillGap = 30; 
lines = houghlines(BW,T,R,P,'FillGap',fillGap,'MinLength',minLen );
% plot
imshow(img);
hold on
xy = [lines.point1; lines.point2];
plot(xy(:,1),xy(:,2),'g','LineWidth',2);

您的代码的问题是 houghlinesFillGap 属性。您应该在返回的行中允许更大的间隙,因为搜索的行不需要是连续的,例如500:

hough_lines = houghlines(binary_image,T,R,P,'FillGap',500,'MinLength',7);

这会根据需要找到图像 7 中的最大线。

可视化

要在图像顶部绘制找到的线,您可以使用以下代码:

figure
imshow(input_images{7});
hold on
% Highlight the longest line segment by coloring it cyan.
plot([longest_line.point1(1) longest_line.point2(1)], [longest_line.point1(2) longest_line.point2(2)],'LineWidth',2,'Color','cyan');

在霍夫变换中寻找最大峰值

作为替代方案,您可以考虑 select 对应于最大霍夫变换值的线,而不是最长的线。这可以通过 selecting longest_line 来完成,如下所示:

longest_line = ...
largest_H = 0; % init value

for n=1:12
    binary_image = edge(input_images{n},'canny');
    [H,T,R] = hough(binary_image);
    P  = houghpeaks(H,1,'threshold',ceil(0.3*max(H(:))));
    hough_lines = houghlines(binary_image,T,R,P,'FillGap',500,'MinLength',7);         

    if (largest_H < H(P(1, 1), P(1, 2)))
        largest_H = H(P(1, 1), P(1, 2));
        longest_line = hough_lines(1);
        longest_line.image = n;
    end
end

这 select 是图像 6 中的下一行,这是另一个允许的结果: