Пример #1
0
            /// <summary>

            /// Determina si un contorno es un circulo, verificando que tenga al menos 7 puntos y todos estén al mismo radio del centro

            /// </summary>

            /// <param name="MaxVariation"> Máxima desviacion permitida (0 a 1) </param>

            /// <returns></returns>

            public static bool isCircle(this VectorOfPoint contour, float MaxVariation = 0.2f)
            {
                bool result = false;



                if (contour.Size >= 7)
                {
                    Moments Moments = CvInvoke.Moments(contour);

                    Vector2 Center = Vector2.zero;

                    Center.x = (float)(Moments.M10 / Moments.M00);

                    Center.y = (float)(Moments.M01 / Moments.M00);



                    result = true;

                    Vector2[] pts = contour.ToVector2();

                    float MaxRadius = (pts[0] - Center).magnitude;

                    float MinRadius = MaxRadius;

                    float MinLimit = 1 - MaxVariation;

                    float MaxLimit = 1 + MaxVariation;



                    //Verifica que el radio sea consistente

                    for (int j = 1; j < pts.Length; j++)
                    {
                        float r = (pts[j] - Center).magnitude;

                        if (r > MaxRadius)
                        {
                            MaxRadius = r;
                        }

                        if (r < MinRadius)
                        {
                            MinRadius = r;
                        }
                    }



                    if (MinRadius / MaxRadius < MinLimit)
                    {
                        result = false;
                    }



                    //Verifica que el perímetro sea consistente

                    if (result)
                    {
                        double perimetroReal = CvInvoke.ArcLength(contour, true);

                        float Radio = (MaxRadius + MinRadius) * 0.5f;

                        float perimetroIdeal = 6.28f * Radio;

                        float factor = (float)(perimetroIdeal / perimetroReal);

                        if ((factor < MinLimit) || (factor > MaxLimit))
                        {
                            result = false;
                        }
                    }



                    pts = null;
                }



                return(result);
            }