IPSDK 4.1.0.2
IPSDK : Image Processing Software Development Kit
Hough lines detection
linesPpties,outImghoughLines2d (inImg)
linesPpties,outImghoughLines2d (inImg,houghLinesImgParams,houghLinesExtractionParams)

Detailed Description

detects 2D lines from the extraction of local maxima in the accumulation Hough matrix computed on the input image

Preamble

In Hough transform, lines are expressed in polar system. The equation of a line is:

\[ \rho = x_0 \cos \theta + y_0 \sin \theta \]

with $ x_0 $ and $ y_0 $ constants and $ \rho $ and $ \theta $ the variables of the equation. $ \rho $, expressed in pixels, corresponds to the orthogonal distance between the origin of the considered reference frame and the line, while $ \theta $, expressed in radians, corresponds to the clockwise angle between the x-axis and the normal to the line (clockwise because, in image reference frame, y-axis points towards bottom).

houghLines2dImg_linePolarParams.png

For a given range of orientations, the family of straight lines going through a given point $(x_0, y_0)$ in cartesian coordinates can be written as:

\[ \rho_{\theta} = x_0 \cos \theta + y_0 \sin \theta \]

If we plot, for instance, the family of lines going through point located at (x=3, y=2) in a graph with $ \rho_{\theta} $ in ordinates and $ \theta $ in absciss, we obtain:

houghLines2dImg_linesFamilyForOnePoint.png

Now, let's add in this graph the 2 families of lines, respectively going through points (6, 3) and (12, 5):

houghLines2dImg_linesFamilyForThreePoints.png

We can see that our 3 sinusoids intersect in one single point $ (\rho = 0.948, \theta = 1.8925) $, that corresponds to the line going through the 3 points (we chose 3 aligned points on purpose for our example).

In conclusion, lines can be detected at positions in the graph where there are a lot of intersections between sinusoids.

Hough accumulator matrix construction

Hough accumulation matrix takes advantage of the property described above.

Considering:

The Hough transform applied to the input image with the described parameters realizes the following steps:

To reduce the number of columns of the accumulation matrix, the origin of the polar reference frame has been positioned at the center of the image. Thus, the maximal absolute value of $ \rho $ equals to half the length of the diagonal of the image (it would have equaled to the length of the diagonal if origin had been positioned at the origin of the image)

All the parameters described above are defined through the data item HoughLinesImgParams.

Applying the algorithm on the following input image with default parameters:

houghLines2dImg_inImg.png

We obtain the following accumulation matrix...

houghLines2dImg_outImg.png

... where the positions of the 3 white spots correspond to the properties of the 3 lines in the input image

Detection of lines from the accumulator matrix

Once the accumulation matrix is computed, detected lines correspond to the positions of pixels where the accumulation values are the highest in the matrix. A simple extraction of the local maxima in the accumulation matrix is then enough to deduce the properties of the detected lines. This extraction of local maxima is done with the Local Extrema Extraction 2d algorithm. Through the data item HoughLinesExtractionParams, the user can vary the following parameters of the local maxima extraction:

Algorithm results

Once these 2 steps are done, the algorithm returns both the computed accumulation matrix and the collection of properties of detected lines (rho (in pixels), theta (in radians), and associated accumulation intensity). Please note that while rho value in accumulation matrix corresponds to the distance between line and center of image, rho values in the returned collection of properties of detected lines equal to the distance between lines and the origin of the image.

Typical use cases of this algorithm are:

houghLines2d_brightLines.png
houghLines2d_gradient.png
See also
HoughLinesGradient2d is an extension of current algorithm, that could be a good alternative when the detection of edges of thing straight-lined stripes is required, in particular.

Example of Python code :

Example imports

import PyIPSDK
import PyIPSDK.IPSDKIPLFeatureDetection as fd
import PyIPSDK.IPSDKIPLFiltering as filter

Code Example

# opening of input image
inImg = PyIPSDK.loadTiffImageFile(inputImgPath)
# Initialize parameters for Hough lines accumulation matrix construction:
# - step for rho = 1
rhoStep = 2.0
# - range of 15 evenly spaced orientations between pi/2 and 3.pi/4 radians
thetaRange = PyIPSDK.createEvenlySpacedRange(math.pi/2, 3*math.pi/4, 15)
# only accumulate pixels of input image with intensity greater than 210
intensityThreshold = 210.0
imgParams = PyIPSDK.createHoughLinesImgParams(
rhoStep, thetaRange, intensityThreshold)
# Initialize parameters for extraction of lines from accumulation matrix
# to default
extractionParams = PyIPSDK.createDefaultHoughLinesExtractionParams()
# hough circle detection computation
outLines, outImg = fd.houghLines2d(
inImg, imgParams, extractionParams)

Example of C++ code :

Example informations

Header file

#include <IPSDKIPL/IPSDKIPLFeatureDetection/Processor/HoughLines2d/HoughLines2d.h>

Code Example

ImageConstPtr pInImg = loadTiffImageFile(inImgPath);
// Initialize parameters for Hough lines accumulation matrix construction:
// - step for rho = 1
const ipReal64 rhoStep = 1.0;
// - range of 15 evenly spaced orientations between pi/2 and 3.pi/4 radians
const EvenlySpacedRangePtr pThetaRange = createEvenlySpacedRange(
M_PI / 2, 3 * M_PI / 4, 15);
const EvenlySpacedRange& thetaRange = *pThetaRange;
// only accumulate pixels of input image with intensity greater than 10
const ipReal32 intensityThreshold = 10.0f;
rhoStep, thetaRange, intensityThreshold);
// Initialize parameters for extraction of lines from accumulation matrix:
// - only keep local maxima with a value greater than 50 % of global maximum
// of the accumulation matrix
const eHoughAccumThresholdType accumThresholdType =
const ipReal64 accumTresholdValue = 0.5f;
// - only one local maximum allowed in a neighborhood of radius equalling to 10
// pixels
const ipUInt32 localMaxSearchWindowRadius = 10;
const HoughLinesExtractionParamsConstPtr pInHoughExtractPrms =
accumThresholdType, accumTresholdValue, localMaxSearchWindowRadius);
// launch Hough lines detection algorithm
HoughLinesResult res = houghLines2d(
pInImg, pInHoughImgPrms, pInHoughExtractPrms);
// store results in variables
const HoughLines2dPptiesConstPtr& pLinesPpties = res._pLinesPpties;
const ImagePtr pAccumulationMatrix = res._pOutImg;