/// <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
            int n = Math.Min(count, lines.Count);

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

            return(dst);
        }
Example #2
0
        private void drawHoughLine(HoughLine line, Bitmap image, BitmapData sourceData, int houghLineWidth)
        {
            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
            int offset = -houghLineWidth / 2;

            for (int i = 0; i < houghLineWidth; i++)
            {
                Drawing.Line(sourceData,
                             new IntPoint((int)x0 + w2 + offset, h2 - (int)y0),
                             new IntPoint((int)x1 + w2 + offset, h2 - (int)y1),
                             Color.Green);
                offset += 1;
            }
        }
Example #3
0
        private Line TranslateHughLineToLine(HoughLine line, int width, int height)
        {
            // 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 = width / 2;
            int h2 = 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;
            }

            return
                (Line.FromPoints(
                     new IntPoint((int)x0 + w2, h2 - (int)y0),
                     new IntPoint((int)x1 + w2, h2 - (int)y1)
                     ));
        }
Example #4
0
        /// <summary>
        /// Calculate the Count lines in the image with most points.
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private HoughLine[] GetTop(int count)
        {
            HoughLine[] hl = new HoughLine[count];

            for (int i = 0; i <= count - 1; i++)
            {
                hl[i] = new HoughLine();
            }

            for (int i = 0; i <= _hMatrix.Length - 1; i++)
            {
                if (_hMatrix[i] > hl[count - 1].Count)
                {
                    hl[count - 1].Count = _hMatrix[i];
                    hl[count - 1].Index = i;
                    int j = count - 1;

                    while (j > 0 && hl[j].Count > hl[j - 1].Count)
                    {
                        HoughLine tmp = hl[j];
                        hl[j] = hl[j - 1];
                        hl[j - 1] = tmp;
                        j -= 1;
                    }
                }
            }

            for (int i = 0; i <= count - 1; i++)
            {
                int dIndex = hl[i].Index / STEPS;
                int alphaIndex = hl[i].Index - dIndex * STEPS;
                hl[i].Alpha = GetAlpha(alphaIndex);
                hl[i].D = dIndex + _min;
            }

            return hl;
        }
 public static HoughLine FindFastBestLine(DoubleImage src, HLParams p, double edgeMin) {
   HoughLine ret = new HoughLine(VisionLabPINVOKE.FindFastBestLine__SWIG_6(DoubleImage.getCPtr(src), HLParams.getCPtr(p), edgeMin), true);
   if (VisionLabPINVOKE.SWIGPendingException.Pending) throw VisionLabPINVOKE.SWIGPendingException.Retrieve();
   return ret;
 }
 public static HoughLine FindBestLine(FloatImage src, HLParams p) {
   HoughLine ret = new HoughLine(VisionLabPINVOKE.FindBestLine__SWIG_5(FloatImage.getCPtr(src), HLParams.getCPtr(p)), true);
   if (VisionLabPINVOKE.SWIGPendingException.Pending) throw VisionLabPINVOKE.SWIGPendingException.Retrieve();
   return ret;
 }
Example #7
0
        private HoughLine[] GetLines(float umbral)
        {
            short maxMapIntensity = 0;
            for ( int i = 0; i < HoughMap.GetLength(0); i++ )
            {
                for ( int j = 0; j < HoughMap.GetLength(1); j++ )
                {
                    if ( HoughMap[i, j] > maxMapIntensity )
                    {
                        maxMapIntensity = HoughMap[i, j];
                    }
                }
            }
            int		maxTheta = HoughMap.GetLength( 1 );
            int		maxRadius = HoughMap.GetLength( 0 );

            short	intensity;
            bool	foundGreater;

            int     halfHoughWidth = maxRadius >> 1;

            lines.Clear( );

            for ( int theta = 0; theta < maxTheta; theta++ )
            {

                for ( int radius = 0; radius < maxRadius; radius++ )
                {

                    intensity = HoughMap[radius,radius];

                    if ( intensity < 10 )
                        continue;

                    foundGreater = false;

                    for ( int tt = theta - 4, ttMax = theta + 4; tt < ttMax; tt++ )
                    {
                        if ( foundGreater == true )
                            break;

                        int cycledTheta = tt;
                        int cycledRadius = radius;

                        if ( cycledTheta < 0 )
                        {
                            cycledTheta = maxTheta + cycledTheta;
                            cycledRadius = maxRadius - cycledRadius;
                        }
                        if ( cycledTheta >= maxTheta )
                        {
                            cycledTheta -= maxTheta;
                            cycledRadius = maxRadius - cycledRadius;
                        }

                        for ( int tr = cycledRadius - 4, trMax = cycledRadius + 4; tr < trMax; tr++ )
                        {
                            if ( tr < 0 )
                                continue;
                            if ( tr >= maxRadius )
                                break;

                            if ( HoughMap[tr,cycledTheta] > intensity )
                            {
                                foundGreater = true;
                                break;
                            }
                        }
                    }
                    if ( !foundGreater )
                    {

                        lines.Add( new HoughLine( (double) theta, (short) ( radius - halfHoughWidth ), intensity, (double) intensity / maxMapIntensity ) );
                    }
                }
            }
            //lines.Sort();
            int count = 0, n = lines.Count;

            while ( ( count < n ) && ( ( (HoughLine) lines[count] ).RelativeIntensity >= umbral ) )
                count++;

            n = Math.Min( count, lines.Count );

            HoughLine[] dst = new HoughLine[n];
            lines.CopyTo( 0, dst, 0, n );
            return dst;
        }
 internal static HandleRef getCPtr(HoughLine obj)
 {
     return((obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr);
 }
Example #9
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(HoughLine obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
 internal static HandleRef getCPtr(HoughLine obj) {
   return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }
Example #11
0
            private HoughLine moveHL(HoughLine hl, float oldX, float oldY, float newX, float newY)
            {
                double t = hl.Theta;
                short r = hl.Radius;
                float cos = (float)Math.Cos(Math.PI * t / 180d);
                float sin = (float)Math.Sin(Math.PI * t / 180d);
                r = (short)(r + (oldX - newX) * cos + (newY - oldY) * sin);

                return new HoughLine(t, r, hl.Intensity, hl.RelativeIntensity);
            }
Example #12
0
            private void drawLine(Graphics g, HoughLine hl, Rectangle sub)
            {
                double t = hl.Theta;
                short r = (short)(hl.Radius);

                int xOffset = sub.X + sub.Width / 2;
                int yOffset = sub.Y + sub.Height / 2;

                if (r < 0)
                {
                    r = Math.Abs(r);
                    t = t - 180;
                }

                float cos = (float)Math.Cos(Math.PI * t / 180d);
                float sin = (float)Math.Sin(Math.PI * t / 180d);
                if (Math.Abs(cos) > 0.00001 && Math.Abs(sin) > 0.00001)
                {
                    double cot = cos / sin;

                    int y1 = (int)(cot * sub.X + yOffset - cot * xOffset - r / sin);
                    int y2 = (int)(cot * (sub.X + sub.Width) + yOffset - cot * xOffset - r / sin);
                    int x1 = (int)((sub.Y - yOffset + r / sin + cot * xOffset) / cot);
                    int x2 = (int)((sub.Y + sub.Height - yOffset + r / sin + cot * xOffset) / cot);

                    Point p1 = Point.Empty;
                    Point p2 = Point.Empty;

                    if (Math.Abs(y1 - yOffset) <= sub.Height / 2)
                        p1 = new Point(sub.X, y1);
                    if (Math.Abs(y2 - yOffset) <= sub.Height / 2)
                        if (p1 == Point.Empty)
                            p1 = new Point(sub.X + sub.Width, y2);
                        else
                            p2 = new Point(sub.X + sub.Width, y2);
                    if (Math.Abs(x1 - xOffset) <= sub.Width / 2)
                        if (p1 == Point.Empty)
                            p1 = new Point(x1, sub.Y);
                        else
                            p2 = new Point(x1, sub.Y);
                    if (Math.Abs(x2 - xOffset) <= sub.Width / 2)
                        if (p1 == Point.Empty)
                            p1 = new Point(x2, sub.Y + sub.Height);
                        else
                            p2 = new Point(x2, sub.Y + sub.Height);

                    g.DrawLine(p, p1, p2);
                }
            }
Example #13
0
            public void findLines(object o)
            {
                DateTime tick = DateTime.Now;
                const int lineCount = 10;
                Bitmap bitmap = (Bitmap)o;
                HoughLine[] hls = new HoughLine[lineCount];

                for (int i = 0; i < hls.Length; i++)
                    hls[i] = null;

                for (int i = 0; i < heightRes.Length - 1; i++)
                {
                    int y = heightRes[i];
                    int yDelta = heightRes[i + 1] - heightRes[i];
                    int xDelta = widthRes[i + 1] - widthRes[i];

                    for (int x = 0; x < bitmap.Width; x += xDelta)
                    {
                        Rectangle sub = new Rectangle(x, y, xDelta, yDelta);
                        Bitmap subImg = (Bitmap)bitmap.Clone(sub, bitmap.PixelFormat);
                        HoughLineTransformation hlt = new HoughLineTransformation();
                        hlt.ProcessImage(subImg);
                        hls = hlt.GetMostIntensiveLines(lineCount);
                        for (int j = 0; j < lineCount; j++)
                        {
                            if (hls.Length > j && hls[j] != null)
                            {
                                float xOffset = subImg.Width / 2f + x;
                                float yOffset = subImg.Height / 2f + y;
                                hls[j] = moveHL(hls[j], xOffset, yOffset, bitmap.Width / 2f, bitmap.Height);
                            }
                        }
                    }
                }
                short[] bestThreeRadius = new short[] { 0, 0, 0 };
                double[] bestThreeIntensity = new double[] { 0, 0, 0 };
                double[] bestThreeTheta = new double[] { double.MinValue, double.MinValue, double.MinValue };
                foreach (HoughLine hl in hls)
                {
                    if (hl == null)
                        continue;

                    short rNew = (short)(hl.Radius);
                    double tNew = hl.Theta;
                    double sNew = hl.RelativeIntensity;

                    bool isNew = true;
                    for (int i = 0; i < 3; i++)
                    {
                        short r = bestThreeRadius[i];
                        double t = bestThreeTheta[i];
                        double s = bestThreeIntensity[i];

                        if (t != double.MinValue && (Math.Abs(t - tNew) < 30 || 180 - Math.Abs(t - tNew) < 30) && Math.Abs(r - rNew) < 30)
                        {
                            bestThreeRadius[i] = (short)(((r * s) + (rNew * sNew)) / (s + sNew));
                            bestThreeTheta[i] = (short)(((t * s) + (tNew * sNew)) / (s + sNew));
                            bestThreeIntensity[i] += sNew;
                            isNew = false;
                            break;
                        }
                    }

                    if (isNew)
                    {
                        double minS = double.MaxValue;
                        int minSidx = 0;
                        for (int i = 0; i < 3; i++)
                        {
                            if (bestThreeIntensity[i] < minS)
                            {
                                minS = bestThreeIntensity[i];
                                minSidx = i;
                            }
                        }
                        if (sNew > bestThreeIntensity[minSidx])
                        {
                            bestThreeRadius[minSidx] = rNew;
                            bestThreeIntensity[minSidx] = sNew;
                            bestThreeTheta[minSidx] = tNew;
                        }
                    }
                }
                for (int i = 0; i < 3; i++)
                {
                    currentStateProbs[i] = double.MinValue;
                    if (bestThreeTheta[i] == double.MinValue || bestThreeIntensity[i] < 1)
                    {
                        bestThreeTheta[i] = double.MinValue;
                        continue;
                    }

                    currentThetaStates[i] = (int)bestThreeTheta[i];
                    currentRStates[i] = bestThreeRadius[i];
                    currentStateProbs[i] = bestThreeIntensity[i];
                }
                threadRunning = false;
                DateTime tack = DateTime.Now;
                //Console.WriteLine(tack.Subtract(tick).Milliseconds);
            }