Main Content


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. peaks is 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 lines contains information about the extracted line segments.


lines = houghlines(___,Name,Value) uses name-value pair arguments to control various aspects of the line extraction.


collapse all

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);
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));

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 beginnings and ends of lines

   % 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;

Highlight the longest line segment by coloring it cyan.


Input Arguments

collapse all

Binary image, specified as a 2-D logical matrix or 2-D numeric matrix. For numeric input, any nonzero pixels are considered to be 1 (true).

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Line rotation angle, in degrees, specified as a numeric matrix. The angle is measured between the x-axis and the rho vector.

Data Types: double

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

Row and column coordinates of Hough transform bins, specified as a numeric matrix.

Data Types: double

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is 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 Name1,Value1,...,NameN,ValueN.

Example: lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);

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, the houghlines function merges the line segments into a single line segment.

Data Types: double

Minimum line length, specified as a positive number. houghlines discards lines that are shorter than the value specified.

Data Types: double

Output Arguments

collapse all

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:




Two element vector [X Y] specifying the coordinates of the end-point of the line segment


Two element vector [X Y] specifying the coordinates of the end-point of the line segment


Angle in degrees of the Hough transform bin


rho axis position of the Hough transform bin

Extended Capabilities

See Also


Introduced before R2006a