Hough transform


[H, theta, rho] = hough(BW)
[H, theta, rho] = hough(BW, ParameterName, ParameterValue)


[H, theta, rho] = hough(BW) computes the Standard Hough Transform (SHT) of the binary image BW. Use the hough function to detect lines in an image. The function returns H, the Hough transform matrix. theta (in degrees) and rho are the arrays of rho and theta values over which hough generates the Hough transform matrix. BW can be logical or numeric, and it must be real, 2-D, and nonsparse.

[H, theta, rho] = hough(BW, ParameterName, ParameterValue) computes the SHT using parameter name/value pairs. When ParameterName is 'RhoResolution', specify a real scalar value between 0 and norm(size(BW)), exclusive, to determine the spacing of the Hough transform bins along the rho axis. The default value is 1.

When ParameterName is 'Theta', specify a vector of Hough transform theta values. Each element of the vector determines the theta value for the corresponding column of the output matrix H. The acceptable range of theta values is 90°θ<90°, and the default is -90:89.


Compute and display the Hough transform of a gantrycrane image.

RGB = imread('gantrycrane.png');

% Convert to intensity.
I  = rgb2gray(RGB);

% Extract edges.
BW = edge(I,'canny');
[H,T,R] = hough(BW,'RhoResolution',0.5,'Theta',-90:0.5:89.5);

% Display the original image.
title('Gantrycrane Image');

% Display the Hough matrix.
title('Hough Transform of Gantrycrane Image');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;

Compute the Hough transform over a limited theta range for the gantrycrane image.

RGB = imread('gantrycrane.png');
I  = rgb2gray(RGB); 
BW = edge(I,'canny'); 
[H,T,R] = hough(BW, 'Theta', 44:0.5:46);
title('Limited Theta Range Hough Transform of Gantrycrane Image');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal;

More About

collapse all


The hough function implements the Standard Hough Transform (SHT). The SHT uses the parametric representation of a line:

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

The variable rho is the distance from the origin to the line along a vector perpendicular to the line. 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°θ<90°. The angle of the line itself is θ+90°, also measured clockwise with respect to the positive x-axis.

The SHT is a parameter space matrix whose rows and columns correspond to rho and theta values respectively. The elements in the SHT represent accumulator cells. Initially, the value in each cell is zero. Then, for every non-background point in the image, rho is calculated for every theta. rho is rounded off to the nearest allowed row in SHT. That accumulator cell is incremented. At the end of this procedure, a value of Q in SHT(r,c) means that Q points in the xy-plane lie on the line specified by theta(c) and rho(r). Peak values in the SHT represent potential lines in the input image.

The Hough transform matrix, H, is nrho-by-ntheta.

Values of nrho and ntheta

nrho = 2*(ceil(D/RhoResolution)) + 1, where
D = sqrt((numRowsInBW - 1)^2 + (numColsInBW - 1)^2).
rho values range from -diagonal to diagonal, where
diagonal = RhoResolution*ceil(D/RhoResolution).
ntheta = length(theta)

Was this topic helpful?