Hough line transformation.

The class implements Hough line transformation, which allows to detect straight lines in an image. Lines, which are found by the class, are provided in polar coordinates system - lines' distances from image's center and lines' slopes are provided. The pole of polar coordinates system is put into processing image's center and the polar axis is directed to the right from the pole. Lines' slope is measured in degrees and is actually represented by angle between polar axis and line's radius (normal going from pole to the line), which is measured in counter-clockwise direction.

Found lines may have negative radius. This means, that the line resides in lower part of the polar coordinates system and its HoughLine.Theta value should be increased by 180 degrees and radius should be made positive.

The class accepts binary images for processing, which are represented by 8 bpp grayscale images. All black pixels (0 pixel's value) are treated as background, but pixels with different value are treated as lines' pixels.

See also documentation to HoughLine class for additional information about Hough Lines.

Sample usage:

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 ) { // ... }

Initial image:

Hough line transformation image:

Example #1
0
        private Bitmap HoughTransform(Bitmap originalImage)
        {
            var imgBitmap     = new Bitmap(originalImage);
            var lineTransform = new Accord.Imaging.HoughLineTransformation();
            // Convert it to binary and mark the possible lines
            // in white so it can be processed by the transform
            var sequence = new FiltersSequence(
                Grayscale.CommonAlgorithms.BT709,
                new NiblackThreshold(),
                new Invert()
                );
            var contoursBitmap = ImageFilter.PrewittFilter(imgBitmap, true);
            // Apply the sequence of filters above:
            Bitmap binaryImage = sequence.Apply(contoursBitmap);

            lineTransform.ProcessImage(binaryImage);
            // Now, let's say we would like to retrieve the lines and use them
            // for further processing. First, the lines can be ordered by their
            // relative intensity using
            HoughLine[] lines = lineTransform.GetLinesByRelativeIntensity(1);

            // Then, let's plot them on top of the input image. Since we will
            // apply many operations to a single image, it is better to first
            // convert it to an UnmanagedImage object to avoid having to lock
            // the image into memory multiple times.

            UnmanagedImage unmanagedImage = UnmanagedImage.FromManagedImage(binaryImage);

            // Finally, plot them in order:
            foreach (HoughLine line in lines)
            {
                line.Draw(unmanagedImage, color: Color.Red);
            }

            return(unmanagedImage.ToManagedImage());
        }