find vertical hough lines in matlab

To find vertical Hough lines in MATLAB, follow these steps:

  1. Read in the image and convert it to grayscale.
main.m
im = imread('image.jpg');
im_gray = rgb2gray(im);
50 chars
3 lines
  1. Apply an edge detection algorithm to the grayscale image, such as the Sobel or Canny operator.
main.m
edge_im = edge(im_gray, 'sobel');
34 chars
2 lines
  1. Create a Hough transform using the hough function. Set the 'Theta' parameter to a vector of values around the vertical axis, e.g. (-90:89).
main.m
[H, T, R] = hough(edge_im, 'Theta', -90:89);
45 chars
2 lines
  1. Find the peaks in the Hough transform using the houghpeaks function. Set the 'Threshold' parameter to a value that will detect the strongest lines.
main.m
peaks = houghpeaks(H, 10, 'Threshold', 0.3*max(H(:)));
55 chars
2 lines
  1. Extract the lines using the houghlines function. Set the 'Theta' parameter to the same vector used in step 3.
main.m
lines = houghlines(edge_im, T, R, peaks, 'Theta', -90:89);
59 chars
2 lines
  1. Optional: visualize the detected lines on top of the original image using the line function.
main.m
figure, imshow(im), hold on
for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1), xy(:,2), 'LineWidth', 2, 'Color', 'green');
end
161 chars
6 lines

gistlibby LogSnag