Exemple #1
0
        private int GetOptimizedThreshold(int count, Bitmap image, int minSize, int maxSize)
        {
            var blobCounter = new AForge.Imaging.BlobCounter();

            AForge.Imaging.Blob[] blobs;
            int thresholdMax = 255;
            int thresholdMin = 0;

            blobCounter.FilterBlobs = true;
            blobCounter.MaxHeight   = maxSize;
            blobCounter.MaxWidth    = maxSize;
            blobCounter.MinHeight   = minSize;
            blobCounter.MinWidth    = minSize;
            for (var i = 255; i >= 0; i--)
            {
                if (i % 10 == 0)
                {
                    DetectShapesProgress?.Invoke(0.5f / 255f * (255 - i));
                }
                blobCounter.BackgroundThreshold = Color.FromArgb(255, i, i, i);
                blobCounter.ProcessImage(image);
                blobs = blobCounter.GetObjectsInformation();
                if (blobs.Length == count)
                {
                    thresholdMax = i;
                    break;
                }
            }
            DetectShapesProgress?.Invoke(0.5f);
            for (var i = 0; i <= 255; i++)
            {
                if (i % 10 == 0)
                {
                    DetectShapesProgress?.Invoke(0.5f + 0.5f / 255f * i);
                }
                blobCounter.BackgroundThreshold = Color.FromArgb(255, i, i, i);
                blobCounter.ProcessImage(image);
                blobs = blobCounter.GetObjectsInformation();
                if (blobs.Length == count)
                {
                    thresholdMin = i;
                    break;
                }
            }
            DetectShapesProgress?.Invoke(1f);
            var thr = (thresholdMax + thresholdMin) / 2;

            return(thr);
        }
Exemple #2
0
        private List <Shape> DetectShapes(int count, Bitmap image, int minSize, int maxSize)
        {
            var blobCounter = new AForge.Imaging.BlobCounter();

            AForge.Imaging.Blob[] blobs;
            blobCounter.FilterBlobs = true;
            blobCounter.MaxHeight   = maxSize;
            blobCounter.MaxWidth    = maxSize;
            blobCounter.MinHeight   = minSize;
            blobCounter.MinWidth    = minSize;
            var threshold = Recognition.GetThreshold(image);

            blobCounter.BackgroundThreshold = Color.FromArgb(255, threshold, threshold, threshold);
            blobCounter.ProcessImage(image);
            blobs = blobCounter.GetObjectsInformation();

            if (blobs.Length == count)
            {
                DetectShapesProgress?.Invoke(1f);
            }
            else
            {
                threshold = GetOptimizedThreshold(count, image, minSize, maxSize);
                blobCounter.BackgroundThreshold = Color.FromArgb(255, threshold, threshold, threshold);
                blobCounter.ProcessImage(image);
                blobs = blobCounter.GetObjectsInformation();
            }

            var shapes = new List <Shape>();

            foreach (var blob in blobs)
            {
                var edgePoints = blobCounter.GetBlobsEdgePoints(blob);
                // FindQuadrilateralCorners:
                // The first point in the list is the point with lowest X coordinate
                // (and with lowest Y if there are several points with the same X value).
                // The corners are provided in counter clockwise order
                var corners = PointsCloud.FindQuadrilateralCorners(edgePoints);

                shapes.Add(new Shape(corners.ToArray(), blob));
            }

            return(shapes);
        }