hough transform in matlab

To perform Hough Transform in Matlab, you can use the built-in hough function. Here's a step-by-step approach:

  1. Read in your image:
main.m
I = imread('your_image.jpg');
30 chars
2 lines
  1. Convert the image to grayscale:
main.m
Igray = rgb2gray(I);
21 chars
2 lines
  1. Perform edge detection on the grayscale image, using any edge detection algorithm of your choice. Here, we'll use the Canny edge detector:
main.m
Iedge = edge(Igray, 'Canny');
30 chars
2 lines
  1. Perform the Hough Transform using the hough function:
main.m
[H,theta,rho] = hough(Iedge);
30 chars
2 lines
  1. Find the peaks in the Hough Transform accumulator matrix, using the houghpeaks function. This will give you the values of the Hough Transform parameters that correspond to the strongest lines in the image:
main.m
P = houghpeaks(H, numPeaks);
29 chars
2 lines

numPeaks is the maximum number of peaks you want to find. You can adjust this depending on how many lines you expect to see in your image.

  1. Draw the lines on the original image using the houghlines function:
main.m
lines = houghlines(Iedge,theta,rho,P);
figure, imshow(I), hold on
for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
end
194 chars
7 lines

This will draw green lines on the original image where the detected lines are.

And that's it! You should now have the detected lines superimposed on the original image.

gistlibby LogSnag