Search code examples
matlabimage-processinghough-transform

How can I discard some lines detected by Hough?


The following is my original picture,

enter image description here

This is my Hough code,

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

Usage

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);

The following is my line-detection picture,

enter image description here

As you can see, my Hough source code detects 3 of the most prominent lines.

I want to discard the bottom line.

So, I have written the following code to discard some area from the image,

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

Usage

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);

But, the result becomes worse,

enter image description here

Relevant Source Code,

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

Solution

  • I solved this problem.

    I used 0 in place of 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