Ejemplo n.º 1
0
        public void RunEdgeDetection(EdgeDetectionOptions options)
        {
            if (HasRunEdgeDetection)
            {
                return;
            }
            using (Bitmap newBitmap = LoadBitmap())
            {
                Rectangle rect = new Rectangle(0, 0, newBitmap.Width, newBitmap.Height);
                using (UnmanagedImage image = new UnmanagedImage(newBitmap.LockBits(rect, ImageLockMode.ReadWrite, newBitmap.PixelFormat)))
                {
                    using (UnmanagedImage grayImage = UnmanagedImage.Create(image.Width, image.Height, PixelFormat.Format8bppIndexed))
                    {
                        Grayscale.CommonAlgorithms.BT709.Apply(image, grayImage);

                        Threshold threshold = new Threshold(options.Threshold);

                        using (UnmanagedImage edgesImage = EDGE_DETECTOR.Apply(grayImage))
                        {
                            Threshold thresholdFilter = new Threshold(options.Threshold);
                            thresholdFilter.ApplyInPlace(edgesImage);

                            if (options.ShowEdgesImage)
                            {
                                ImageForm.ShowImage("Enhanced Edges Image", edgesImage);
                            }

                            BlobCounter blobCounter = new BlobCounter();
                            blobCounter.MinHeight    = MINIMUM_BLOB_SIZE;
                            blobCounter.MinWidth     = MINIMUM_BLOB_SIZE;
                            blobCounter.FilterBlobs  = true;
                            blobCounter.ObjectsOrder = ObjectsOrder.Size;

                            blobCounter.ProcessImage(edgesImage);
                            Blob[] blobs = blobCounter.GetObjectsInformation();

                            Corners.Clear();
                            foreach (Blob blob in blobs)
                            {
                                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
                                List <IntPoint> corners    = null;

                                if (SHAPE_CHECKER.IsQuadrilateral(edgePoints, out corners))
                                {
                                    List <IntPoint> leftEdgePoints, rightEdgePoints;
                                    blobCounter.GetBlobsLeftAndRightEdges(blob, out leftEdgePoints, out rightEdgePoints);

                                    Corners.Add(corners);

                                    if (options.ShowBlobImages)
                                    {
                                        QuadrilateralTransformation quadTransformation = new QuadrilateralTransformation(corners, 200, 200);
                                        using (UnmanagedImage quadImage = quadTransformation.Apply(image))
                                        {
                                            ImageForm.ShowImage("Quad Image", quadImage);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }