extend hough line to infinity in matlab

To extend a Hough line to infinity in MATLAB, you can follow these steps:

  1. Obtain the Hough parameters for the detected line(s) using houghlines function. This will give you the start and end points of the line in the image coordinate system.
main.m
[H,T,R] = hough(BW); % where BW is the binary image
P = houghpeaks(H,numpeaks); % where numpeaks is the number of lines you want to detect
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
195 chars
4 lines
  1. Calculate the slope and intercept of the line in the image coordinate system using the start and end points.
main.m
for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   slope = (xy(2,2) - xy(1,2)) / (xy(2,1) - xy(1,1));
   intercept = xy(1,2) - slope * xy(1,1);    
end
172 chars
6 lines
  1. Find two points on the line which are outside the bounds of the image. You can do this by extending the line to the edge of the image.
main.m
x1 = 1;
y1 = slope * x1 + intercept;
x2 = size(BW, 2);
y2 = slope * x2 + intercept;
84 chars
5 lines
  1. Plot the extended line using the line function in MATLAB.
main.m
figure, imshow(BW), hold on
plot([x1, x2], [y1, y2], 'LineWidth', 2, 'Color', 'red');
86 chars
3 lines

This will plot the Hough line in red color and extend it to the edge of the image, making it appear as if it has infinite length.

gistlibby LogSnag