houghlines
Extract line segments based on Hough transform
Description
extracts line segments in the imagelines
= houghlines(BW
,theta
,rho
,peaks
)BW
associated with particular bins in a Hough transform.theta
andrho
are vectors returned by functionhough
.peaks
is a matrix returned by thehoughpeaks
function that contains the row and column coordinates of the Hough transform bins to use in searching for line segments. The return valuelines
contains information about the extracted line segments.
uses name-value pair arguments to control various aspects of the line extraction.lines
= houghlines(___,Name,Value
)
Examples
Find Line Segments and Highlight longest segment
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'); axison, axisnormal, holdon;
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), holdonmax_len = 0;fork = 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情节(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 segmentlen = norm(lines(k).point1 - lines(k).point2);if( len > max_len) max_len = len; xy_long = xy;endend
Highlight the longest line segment by coloring it cyan.
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');
Input Arguments
BW
—Binary image
2-D logical matrix|2-D numeric matrix
Binary image, specified as a 2-D logical matrix or 2-D numeric matrix.For numeric input, any nonzero pixels are considered to be1
(true
).
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|logical
theta
—行rotation angle
numeric matrix
行rotation angle, in degrees, specified as a numeric matrix. The angle is measured between thex-axis and therho
vector.
Data Types:double
rho
—Distance from origin to line
numeric matrix
Distance from the coordinate origin, specified as a numeric matrix. The coordinate origin is the top-left corner of the image (0,0).
Data Types:double
peaks
—Row and column coordinates of Hough transform bins
numeric matrix
Row and column coordinates of Hough transform bins, specified as a numeric matrix.
Data Types:double
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, whereName
is the argument name andValue
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and encloseName
in quotes.
Example:行= houghlines (BW T R P 'FillGap',5,'MinLength',7);
FillGap
—Distance between two line segments associated with the same Hough transform bin
20
(default) |positive number
Distance between two line segments associated with the same Hough transform bin, specified as a positive number. When the distance between the line segments is less than the value specified, thehoughlines
function merges the line segments into a single line segment.
Data Types:double
MinLength
—Minimum line length
40
(default) |positive number
Minimum line length, specified as a positive number.houghlines
discards lines that are shorter than the value specified.
Data Types:double
Output Arguments
lines
— Detected lines
structure array
Detected lines, returned as a structure array whose length equals the number of merged line segments found. Each element of the structure array has these fields:
Field |
Description |
---|---|
|
Two element vector |
|
Two element vector |
|
Angle in degrees of the Hough transform bin |
|
|
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
houghlines
supports the generation of C code (requiresMATLAB®Coder™). For more information, seeCode Generation for Image Processing.The optional parameter names
'FillGap'
and'MinLength'
must be compile-time constants. Their associated values need not be compile-time constants.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
The optional parameter names
'FillGap'
and'MinLength'
must be compile-time constants. Their associated values need not be compile-time constants.
Version History
See Also
开放的例子
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)