C# Class AForge.Imaging.HoughLine

Hough line.

Represents line of Hough Line transformation using polar coordinates. See Wikipedia for information on how to convert polar coordinates to Cartesian coordinates.

Hough Line transformation does not provide information about lines start and end points, only slope and distance from image's center. Using only provided information it is not possible to draw the detected line as it exactly appears on the source image. But it is possible to draw a line through the entire image, which contains the source line (see sample code below).

Sample code to draw detected Hough lines:

HoughLineTransformation lineTransform = new HoughLineTransformation( ); // apply Hough line transofrm lineTransform.ProcessImage( sourceImage ); Bitmap houghLineImage = lineTransform.ToBitmap( ); // get lines using relative intensity HoughLine[] lines = lineTransform.GetLinesByRelativeIntensity( 0.5 ); foreach ( HoughLine line in lines ) { // get line's radius and theta values int r = line.Radius; double t = line.Theta; // check if line is in lower part of the image if ( r < 0 ) { t += 180; r = -r; } // convert degrees to radians t = ( t / 180 ) * Math.PI; // get image centers (all coordinate are measured relative // to center) int w2 = image.Width /2; int h2 = image.Height / 2; double x0 = 0, x1 = 0, y0 = 0, y1 = 0; if ( line.Theta != 0 ) { // none-vertical line x0 = -w2; // most left point x1 = w2; // most right point // calculate corresponding y values y0 = ( -Math.Cos( t ) * x0 + r ) / Math.Sin( t ); y1 = ( -Math.Cos( t ) * x1 + r ) / Math.Sin( t ); } else { // vertical line x0 = line.Radius; x1 = line.Radius; y0 = h2; y1 = -h2; } // draw line on the image Drawing.Line( sourceData, new IntPoint( (int) x0 + w2, h2 - (int) y0 ), new IntPoint( (int) x1 + w2, h2 - (int) y1 ), Color.Red ); }

To clarify meaning of Radius and Theta values of detected Hough lines, let's take a look at the below sample image and corresponding values of radius and theta for the lines on the image:

Detected radius and theta values (color in corresponding colors): Theta = 90, R = 125, I = 249; Theta = 0, R = -170, I = 187 (converts to Theta = 180, R = 170); Theta = 90, R = -58, I = 163 (converts to Theta = 270, R = 58); Theta = 101, R = -101, I = 130 (converts to Theta = 281, R = 101); Theta = 0, R = 43, I = 112; Theta = 45, R = 127, I = 82.

Inheritance: IComparable
Datei anzeigen Open project: holisticware-admin/MonoVersal.AForgeNET Class Usage Examples

Public Properties

Property Type Description
Intensity short
Radius short
RelativeIntensity double
Theta double

Public Methods

Method Description
CompareTo ( object value ) : int

Compare the object with another instance of this class.

Object are compared using their intensity value.

HoughLine ( double theta, short radius, short intensity, double relativeIntensity ) : System

Initializes a new instance of the HoughLine class.

Method Details

CompareTo() public method

Compare the object with another instance of this class.

Object are compared using their intensity value.

public CompareTo ( object value ) : int
value object Object to compare with.
return int

HoughLine() public method

Initializes a new instance of the HoughLine class.
public HoughLine ( double theta, short radius, short intensity, double relativeIntensity ) : System
theta double Line's slope.
radius short Line's distance from image center.
intensity short Line's absolute intensity.
relativeIntensity double Line's relative intensity.
return System

Property Details

Intensity public_oe property

Line's absolute intensity, (0, +∞).

Line's absolute intensity is a measure, which equals to number of pixels detected on the line. This value is bigger for longer lines.

The value may not be 100% reliable to measure exact number of pixels on the line. Although these value correlate a lot (which means they are very close in most cases), the intensity value may slightly vary.

public short Intensity
return short

Radius public_oe property

Line's distance from image center, (−∞, +∞).
Negative line's radius means, that the line resides in lower part of the polar coordinates system. This means that Theta value should be increased by 180 degrees and radius should be made positive.
public short Radius
return short

RelativeIntensity public_oe property

Line's relative intensity, (0, 1].

Line's relative intensity is relation of line's Intensity value to maximum found intensity. For the longest line (line with highest intesity) the relative intensity is set to 1. If line's relative is set 0.5, for example, this means its intensity is half of maximum found intensity.

public double RelativeIntensity
return double

Theta public_oe property

Line's slope - angle between polar axis and line's radius (normal going from pole to the line). Measured in degrees, [0, 180).
public double Theta
return double