我如何丢弃霍夫检测到的一些线?

时间:2017-05-26 21:19:09

标签: matlab image-processing hough-transform

以下是我原来的照片,

enter image description here

这是我的Hough代码,

function [hough_lines, H, T, R, P] = get_hough_lines( input_image , peaks)
    binary_image = edge(input_image,'canny');
    [H,T,R] = hough(binary_image);
    P  = houghpeaks(H, peaks, 'threshold', ceil(0.3*max(H(:))));
    hough_lines = houghlines(binary_image,T,R,P,'FillGap',500,'MinLength',7);   
end

用法

input_image  = imread('7.png');    
max_peaks = 3;
discard_pixels = 10;    
[hough_lines, H, T, R, P] = get_hough_lines(input_image, max_peaks);        
draw_hough_lines(input_image, hough_lines);

以下是我的线检测图片,

enter image description here

如您所见,我的Hough源代码检测到3条最突出的行。

我想放弃底线。

所以,我编写了以下代码来丢弃图像中的某些区域,

function [output_image] = discard_image_area( input_image, pixel_count)
    output_image = input_image;    
    [height, width] = size(input_image);        
    % discard top
    output_image(1:pixel_count, :) = 125;
    % discard bottom
    output_image((height-pixel_count):height, :) = 125;    
    % discard left
    output_image(:,1:pixel_count) = 125;
    % discard right
    output_image(:,(width-pixel_count):width) = 125;
end

用法

input_image  = imread('7.png');    
max_peaks = 3;
discard_pixels = 10;    
[hough_lines, H, T, R, P] = get_hough_lines( discard_image_area(input_image, 
     discard_pixels), max_peaks);    
draw_hough_lines(input_image, hough_lines);

但是,结果变得更糟,

enter image description here

相关源代码,

function draw_hough_lines(rotI, lines )
    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
end

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。

我使用0代替125

function [output_image] = discard_image_area( input_image, pixel_count)
    output_image = input_image;

    [height, width] = size(input_image);

    % discard top
    output_image(1:pixel_count, :) = 0;
    % discard bottom
    h = height - pixel_count;
    output_image(h:height, :) = 0;    
    % discard left
    output_image(:,1:pixel_count) = 0;
    % discard right
    output_image(:,(width-pixel_count):width) = 0;
end