compute hough space for horizontal lines in matlab

To compute Hough space for horizontal lines in MATLAB, you can follow these steps:

  1. Read the image containing the horizontal lines:

    main.m
    img = imread('horizontal_lines.jpg');
    
    38 chars
    2 lines
  2. Convert the image to grayscale:

    main.m
    img_gray = rgb2gray(img);
    
    26 chars
    2 lines
  3. Apply edge detection to the grayscale image:

    main.m
    edges = edge(img_gray, 'canny');
    
    33 chars
    2 lines
  4. Compute the Hough transform using the hough function:

    main.m
    [H,theta,rho] = hough(edges, 'Theta', -90:0.5:89.5);
    
    53 chars
    2 lines
  5. Find the peaks in the Hough space using the houghpeaks function:

    main.m
    peaks = houghpeaks(H, 10);
    
    27 chars
    2 lines
  6. Convert the Hough parameterization back to image parameterization using the houghlines function:

    main.m
    lines = houghlines(edges, theta, rho, peaks, 'FillGap', 20, 'MinLength', 100);
    
    79 chars
    2 lines
  7. Visualize the detected lines on the original image:

    main.m
    figure; imshow(img); hold on;
    for k = 1:length(lines)
        xy = [lines(k).point1; lines(k).point2];
        plot(xy(:,1), xy(:,2), 'LineWidth', 2, 'Color', 'green');
    end
    
    165 chars
    6 lines

In the hough function, the Theta parameter defines the range of angles (in degrees) to consider. Here, we are considering all possible angles between -90 and 89.5 degrees. The houghpeaks function returns the row and column indices corresponding to the peaks in the Hough space. The houghlines function takes these parameters and computes the corresponding image coordinates for each line. The FillGap and MinLength parameters control how the function joins segments and filters out short lines, respectively. Finally, we use imshow and hold on to display the original image and add the detected lines using the plot function.

gistlibby LogSnag