예제 #1
0
        private bool Detect(Mat bin)
        {
            // Reset
            mRects.Clear();

            // Find contours
            Mat hierarchy = new Mat();
            IList <MatOfPoint> contoursList = new JavaList <MatOfPoint>();

            Imgproc.FindContours(bin.Clone(), contoursList, hierarchy, Imgproc.RetrTree, Imgproc.ChainApproxSimple);

            // Filter contours
            bool detected = false;

            for (int iContour = 0; iContour < contoursList.Count(); iContour++)
            {
                // Areas
                Core.Rect rect        = Imgproc.BoundingRect(contoursList[iContour]);
                double    ellipseArea = PI * (rect.Width / 2) * (rect.Height / 2);
                double    area        = Imgproc.ContourArea(contoursList[iContour]);

                // Ratios
                double boundWidthPerHeight = (double)rect.Width / rect.Height;
                double areaPerEllipse      = (double)(area) / ellipseArea;
                double rectPerFrame        = (double)(rect.Area()) / (bin.Size().Width *bin.Size().Height);

                // Check constraints
                if (rectPerFrame > MIN_SIGN_SIZE_PER_FRAME)
                {
                    if (1 - LIMIT_DIF_SIGN_SIZE < boundWidthPerHeight && boundWidthPerHeight < 1 + LIMIT_DIF_SIGN_SIZE)
                    {
                        if (1 - LIMIT_DIF_SIGN_AREA < areaPerEllipse && areaPerEllipse < 1 + LIMIT_DIF_SIGN_AREA)
                        {
                            mRects.Add(rect);
                            detected = true;
                        }
                    }
                }
            }

            return(detected);
        }