Extract line segments based on Hough transform
lines = houghlines(BW,theta,rho,peaks)
lines = houghlines(___,Name,Value,...)
line segments in the image
lines = houghlines(
BW associated with
particular bins in a Hough transform.
vectors returned by function
a matrix returned by the
houghpeaks function that
contains the row and column coordinates of the Hough transform bins
to use in searching for line segments. The return value
a structure array whose length equals the number of merged line segments
Read image into workspace.
I = imread('circuit.tif');
Rotate the image.
rotI = imrotate(I,33,'crop');
Create a binary image.
BW = edge(rotI,'canny');
Create the Hough transform using the binary image.
[H,T,R] = hough(BW); imshow(H,,'XData',T,'YData',R,... 'InitialMagnification','fit'); xlabel('\theta'), ylabel('\rho'); axis on, axis normal, hold on;
Find peaks in the Hough transform of the image.
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 by coloring it cyan.
BW— Binary image
Binary image, specified as a real, 2-D, nonsparse logical or numeric array.
theta— Line rotation angle in radians
Line rotation angle in radians, specified as a real, 2-D, nonsparse logical or numeric array.
rho— Distance from the coordinate origin
Distance from the coordinate origin, specified as a real, 2-D, nonsparse logical or numeric array. The coordinate origin is the top-left corner of the image (0,0).
peaks— Row and column coordinates of Hough transform bins
Row and column coordinates of Hough transform bins, specified as a real, nonsparse numeric array.
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
'FillGap'— Distance between two line segments associated with the same Hough transform bin
Distance between two line segments associated with the same
Hough transform bin, specified as a positive real scalar. When the
distance between the line segments is less than the value specified,
houghlines function merges the line segments
into a single line segment.
'MinLength'— Minimum line length
Minimum line length, specified as a positive real scalar.
lines that are shorter than the value specified.
lines— Lines found
Lines found, returned as a structure array whose length equals the number of merged line segments found. Each element of the structure array has these fields:
Two element vector
Two element vector
Angle in degrees of the Hough transform bin
Usage notes and limitations:
This function supports the generation of C code using MATLAB® Coder™. For more information, see Code Generation for Image Processing.
The optional parameter names
be compile-time constants. Their associated values need not be compile-time