Main Content

hough

Hough transform

Description

[H,theta,rho] = hough(BW) calculates the Standard Hough Transform (SHT) of the binary image BW. You can use the Hough transform matrix H to detect lines.

The function uses the parametric representation of a line: rho = x*cos(theta) + y*sin(theta). Here, rho is the offset from the origin to a line along a vector perpendicular to the line, and theta is the angles in degrees between the x-axis and the vector perpendicular to the line. The function samples the SHT for many combinations of rho and theta values, and returns the sampled values in rho and theta, respectively. For more information, see Hough Transform.

example

[H,theta,rho] = hough(BW,Name=Value) also uses name-value arguments to adjust how the function samples values of theta and rho. For example, specify RhoResolution=0.5 to sample values of rho every 0.5 pixels.

example

Examples

collapse all

Read and display an image.

RGB = imread("gantrycrane.png");
imageshow(RGB)

Convert the image to grayscale, then find the edges in the image.

I  = im2gray(RGB);
BW = edge(I,"canny");

Calculate the Hough transform.

[H,T,R] = hough(BW,RhoResolution=0.5,Theta=-90:0.5:89);

The Hough matrix is of data type double with values outside the range [0, 1]. To display the Hough matrix, rescale the data to the range [0, 1]. Improve the image contrast for display by using the imadjust function.

Hdisp = rescale(H);
Hdisp = imadjust(Hdisp);

imshow(Hdisp,XData=T,YData=R)
xlabel("\theta")
ylabel("\rho")
axis on
axis normal
hold on
colormap(gca,hot)

Figure contains an axes object. The axes object with xlabel theta, ylabel rho contains an object of type image.

Read an image.

RGB = imread("gantrycrane.png");

Convert the image to grayscale, then find the edges in the image.

I  = im2gray(RGB);
BW = edge(I,'canny');

Calculate the Hough transform over a limited range of angles.

[H,T,R] = hough(BW,Theta=44:0.5:46);

The Hough matrix is of data type double with values outside the range [0, 1]. To display the Hough matrix, rescale the data to the range [0, 1]. Improve the image contrast for display by using the imadjust function.

Hdisp = rescale(H);
Hdisp = imadjust(Hdisp);
imshow(Hdisp,XData=T,YData=R)
xlabel("\theta")
ylabel("\rho")
axis on
axis normal
hold on
colormap(gca,hot)

Figure contains an axes object. The axes object with xlabel theta, ylabel rho contains an object of type image.

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

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: [H,theta,rho] = hough(BW,RhoResolution=0.5) specifies the spacing of Hough transform bins along the rho dimension as 0.5 pixels.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: [H,theta,rho] = hough(BW,"RhoResolution",0.5)

Bin spacing along the rho dimension, in pixels, specified as a positive number. RhoResolution must be less than norm(size(BW)).

Example: RhoResolution=2 uses Hough transform bins of width 2 along the rho dimension.

Data Types: double

Angles of the perpendicular projections to the lines, in degrees, specified as a numeric vector with elements in the range [-90, 90). The angles are measured in the clockwise direction from the x-axis.

Example: -90:0.5:89.5

Data Types: double

Output Arguments

collapse all

Hough transform matrix, returned as a numeric matrix. The matrix is of size nrho-by-ntheta, where nrho is the length of rho and ntheta is the length of theta. For more information, see Algorithms.

Angles of the perpendicular projections to the lines, in degrees, returned as a numeric vector. The angles are measured in the clockwise direction from the x-axis. For more information, see Algorithms.

Use this output argument as input to the houghlines and houghpeaks functions.

Data Types: double

Perpendicular offsets from the origin to the lines, in pixels, returned as a numeric vector. For more information, see Algorithms.

The length of the vector is 2*ceil(D/RhoResolution)+1 where D is the length of the image diagonal in Cartesian coordinates. Because the origin of the coordinate system is at the center of the top-left pixel, D = norm(size(BW)-1).

The values of rho range from -diagonal to diagonal, where diagonal = RhoResolution*ceil(D/RhoResolution).

Use this output argument as input to the houghlines function.

Data Types: double

Algorithms

The Standard Hough Transform (SHT) uses the parametric representation of a line:

rho = x*cos(theta) + y*sin(theta)

The origin of the coordinate system is assumed to be at the center of the upper-left corner pixel.

The variable theta is the angle of the perpendicular projection from the origin to the line, measured in degrees clockwise from the positive x-axis. The range of theta is –90° ≤ theta < 90°. The angle of the line itself is theta + 90°, also measured clockwise with respect to the positive x-axis.

The variable rho is the perpendicular offset from the origin to the line. The magnitude of rho is equivalent to the perpendicular distance between the origin and the line. However, the hough function supports negative values of rho to represent lines with an effective rotation angle outside the supported range of theta. Graphically, rho is negative when the line passes through the quadrant opposite to the normal vector defined by theta. In other words, rho is negative to represent lines for which the effective rotation angle would be –180° ≤ theta < –90° or 90° ≤ theta < 180°.

Graphical representation of how theta and rho are defined for a line, in green, relative to the perpendicular projection, in black.

The Hough transform matrix, H, is a parameter space matrix whose rows and columns correspond to rho and theta values, respectively. For every combination of rho and theta, the SHT calculates the line with those parameters and returns the sum of all true pixels in BW along that line. When many pixels have a true value along a parameterized line, the result is a peak value in the Hough transform matrix. Peak values represent potential lines in the input image. You can find peak values in the Hough transform matrix by using the houghpeaks function.

Because the origin is at the center of the upper-left corner pixel, there are no lines in the image for which rho is negative and theta is positive. Elements of the Hough transform matrix that correspond to negative rho and positive theta have the value 0.

Extended Capabilities

expand all

Version History

Introduced before R2006a

expand all