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
コード例 #1
0
        // Get specified amount of lines with highest intensity
        private HoughLine[] GetMostIntensiveLines(int count)
        {
            // lines count
            int n = Math.Min(count, lines.Count);

            // result array
            HoughLine[] dst = new HoughLine[n];
            lines.CopyTo(0, dst, 0, n);

            return(dst);
        }
コード例 #2
0
        /// <summary>
        /// Get specified amount of lines with highest <see cref="HoughLine.Intensity">intensity</see>.
        /// </summary>
        ///
        /// <param name="count">Amount of lines to get.</param>
        ///
        /// <returns>Returns array of most intesive lines. If there are no lines detected,
        /// the returned array has zero length.</returns>
        ///
        public HoughLine[] GetMostIntensiveLines(int count)
        {
            // lines count
            var n = Math.Min(count, this.lines.Count);

            // result array
            var dst = new HoughLine[n];

            this.lines.CopyTo(0, dst, 0, n);

            return(dst);
        }
コード例 #3
0
        // Get specified amount of lines with highest intensity
        private HoughLine[] GetMostIntensiveLines( int count )
        {
            // lines count
            int n = Math.Min( count, lines.Count );

            // result array
            HoughLine[] dst = new HoughLine[n];
            lines.CopyTo( 0, dst, 0, n );

            return dst;
        }
コード例 #4
0
        // Get lines using theirs intensity value [0, 1]
        public HoughLine[] GetLinesByIntensity(double intensity)
        {
            short minIntensity = (short)(maxMapValue * intensity);
            int   maxTheta     = houghMap.GetLength(0);
            int   maxR         = houghMap.GetLength(1);
            short v;
            bool  foundMax;

            int halfHoughWidth = maxR >> 1;


            ArrayList linesList = new ArrayList( );

            // for each Theta value
            for (int theta = 0; theta < maxTheta; theta++)
            {
                // for each R value
                for (int r = 0; r < maxR; r++)
                {
                    // get current value
                    v        = houghMap[theta, r];
                    foundMax = false;

                    if (v < minIntensity)
                    {
                        continue;
                    }

                    // check neighboors
                    for (int tt = theta - localPeakRadius, ttMax = theta + localPeakRadius; tt < ttMax; tt++)
                    {
                        if (tt < 0)
                        {
                            continue;
                        }
                        if ((foundMax == true) || (tt >= maxTheta))
                        {
                            break;
                        }

                        for (int tr = r - localPeakRadius, trMax = r + localPeakRadius; tr < trMax; tr++)
                        {
                            if ((tr < 0) || ((tr == r) && (tt == theta)))
                            {
                                continue;
                            }
                            if (tr >= maxR)
                            {
                                break;
                            }

                            // compare the neighboor with current value
                            if (houghMap[tt, tr] > v)
                            {
                                foundMax = true;
                                break;
                            }
                        }
                    }

                    //
                    if (!foundMax)
                    {
                        // we have local maximum
                        linesList.Add(new HoughLine((short)theta, (short)(r - halfHoughWidth), v));
                    }
                }
            }

            //
            int linesCount = linesList.Count;

            HoughLine[] lines = new HoughLine[linesCount];

            for (int i = 0; i < linesCount; i++)
            {
                lines[i] = (HoughLine)linesList[i];
            }

            return(lines);
        }