help houghlines
HOUGHLINES Extract line segments based on Hough transform.
LINES = HOUGHLINES(BW, THETA, RHO, PEAKS) extracts line segments
in the image BW associated with particular bins in a Hough
transform. THETA and RHO are vectors returned by function HOUGH.
Matrix PEAKS, which is returned by function HOUGHPEAKS,
contains the row and column coordinates of the Hough transform
bins to use in searching for line segments. HOUGHLINES returns
LINES structure array whose length equals the number of merged
line segments found. Each element of the structure array has
these fields:
point1 End-point of the line segment; two-element vector
point2 End-point of the line segment; two-element vector
theta Angle (in degrees) of the Hough transform bin
rho Rho-axis position of the Hough transform bin
The end-point vectors contain [X, Y] coordinates.
LINES = HOUGHLINES(...,PARAM1,VAL1,PARAM2,VAL2) sets various
parameters. Parameter names can be abbreviated, and case
does not matter. Each string parameter is followed by a value
as indicated below:
'FillGap' Positive real scalar.
When HOUGHLINES finds two line segments associated
with the same Hough transform bin that are separated
by less than 'FillGap' distance, HOUGHLINES merges
them into a single line segment.
Default: 20
'MinLength' Positive real scalar.
Merged line segments shorter than 'MinLength'
are discarded.
Default: 40
Class Support
-------------
BW can be logical or numeric and it must be real, 2-D, and nonsparse.
References
----------
Rafael C. Gonzalez, Richard E. Woods, Steven L. Eddins, "Digital
Image Processing Using MATLAB", Prentice Hall, 2003
Example
-------
% Search for line segments corresponding to five peaks in the Hough
% transform of the rotated circuit.tif image. Additionally, highlight
% the longest segment.
I = imread('circuit.tif');
rotI = imrotate(I,33,'crop');
BW = edge(rotI,'canny');
[H,T,R] = hough(BW);
imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2));
y = R(P(:,1));
plot(x,y,'s','color','white');
% Find lines and plot them
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
figure, imshow(rotI), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');
See also HOUGH, HOUGHPEAKS.
Documentation for houghlines
doc houghlines