overlay lines over edge detected image in matlab

To overlay lines on an edge-detected image in Matlab, follow these steps:

  1. Read in the image and perform edge detection using the edge function:
main.m
img = imread('image.jpg');
edges = edge(img, 'Canny');
55 chars
3 lines
  1. Use a line detection function, such as the Hough transform, to detect lines in the edge image:
main.m
[H, theta, rho] = hough(edges);
P = houghpeaks(H, 10);
lines = houghlines(edges, theta, rho, P);
97 chars
4 lines
  1. Overlay the detected lines onto the original image using the line function:
main.m
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
hold off;
160 chars
8 lines

The line function takes in the endpoints of each line as input and draws the line on the plot. The hold on and hold off commands are used to keep the original image displayed while the lines are overlaid on top. The result will be an image with the detected lines overlaid in green.

gistlibby LogSnag