Exemplo n.º 1
0
 CircleF FindHighestScoreCircle()
 {
     if (circles.Count != 0)
     {
         CircleFWithScore circleFWithMaxScore = circles[0];
         foreach (CircleFWithScore circle in circles)
         {
             if (circle.score > circleFWithMaxScore.score)
             {
                 circleFWithMaxScore = circle;
             }
         }
         return(circleFWithMaxScore.circle);
     }
     else
     {
         throw new IndexOutOfRangeException();
     }
 }
Exemplo n.º 2
0
        public CircleF FindCircle(Image <Gray, Byte> image, int estimatedRadius, int patternType, int error = 30)
        {
            circles.Clear();
            Image <Gray, Byte> bilateralFilteredImage, edgeDetectedImage, eroded, img;

            img = image.Clone();
            bilateralFilteredImage = new Mat().ToImage <Gray, byte>();
            edgeDetectedImage      = new Mat().ToImage <Gray, byte>();
            eroded = new Mat().ToImage <Gray, byte>();
            Mat hierarchy = new Mat();
            VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

            //Mat

            CvInvoke.MorphologyEx(img, img, MorphOp.Close, GenerateEllipseKernel(13), new Point(-1, -1), 1, BorderType.Default, new MCvScalar());
            CvInvoke.BilateralFilter(img, bilateralFilteredImage, 9, 30, 30);
            CvInvoke.Canny(bilateralFilteredImage, edgeDetectedImage, 25, 25);
            CvInvoke.MorphologyEx(edgeDetectedImage, eroded, MorphOp.Close, GenerateEllipseKernel(11), new Point(-1, -1), 1, BorderType.Default, new MCvScalar());
            CvInvoke.FindContours(eroded, contours, hierarchy, RetrType.List, ChainApproxMethod.ChainApproxSimple);

            for (int i = 0; i < contours.Size; i++)
            {
                using (VectorOfPoint contour = contours[i])
                {
                    Rectangle r = CvInvoke.BoundingRectangle(contour);
                    double    w, h;
                    if (IsSquare(r.Width, r.Height))
                    {
                        w = r.Width;
                        h = r.Height;

                        double  rect_area   = ((w * w) / 4) * Math.PI;
                        CircleF circle      = CvInvoke.MinEnclosingCircle(contour);
                        double  circle_area = circle.Radius * circle.Radius * Math.PI;

                        if ((Math.Abs(rect_area - circle_area) < rect_area / 10) &&
                            (Math.Abs(Math.Sqrt(circle_area / 3.14) - estimatedRadius) < error) && (w > 21) && (h > 21))
                        {
                            CircleFWithScore temp = new CircleFWithScore(circle, CvInvoke.ContourArea(contour) / circle.Area);
                            circles.Add(temp);
                        }
                    }
                }
            }
            //CvInvoke.MatchTemplate(img,templ:templ,)
            //CvInvoke.Imshow("edge", eroded);
            //var watch = System.Diagnostics.Stopwatch.StartNew();
            CircleF result = FindHighestScoreCircle();

            if (MatchPattern(image, result, patternType))
            {
                //watch.Stop();
                //var elapsedMs = watch.ElapsedMilliseconds;
                //Console.WriteLine("\nFinished pattern matching in " + elapsedMs + "ms");
                return(result);
            }
            else
            {
                //watch.Stop();
                //var elapsedMs = watch.ElapsedMilliseconds;
                //Console.WriteLine("\nFinished pattern matching in " + elapsedMs + "ms");
                throw new IndexOutOfRangeException();
            }
        }