Beispiel #1
0
        public void FindTest()
        {
            List <IntPoint> contour = new List <IntPoint>();

            int max = 100;

            for (int i = 0; i < max; i++)
            {
                add(contour, i, max);
            }

            for (int i = 0; i < max; i++)
            {
                add(contour, max, i);
            }

            for (int i = 0; i < max; i++)
            {
                add(contour, 0, i);
            }

            for (int i = 0; i < max / 2; i++)
            {
                add(contour, i, i);
            }

            for (int i = 0; i < max / 2; i++)
            {
                add(contour, i + max / 2, max / 2 - i);
            }

            PointsMarker marker = new PointsMarker(contour);
            var          bitmap = AForge.Imaging.Image.CreateGrayscaleImage(max + 1, max + 1);

            bitmap = marker.Apply(bitmap);
            // ImageBox.Show(bitmap);

            GrahamConvexHull       graham      = new GrahamConvexHull();
            List <IntPoint>        hull        = graham.FindHull(contour);
            ConvexHullDefects      hullDefects = new ConvexHullDefects(10);
            List <ConvexityDefect> defects     = hullDefects.FindDefects(contour, hull);

            Assert.AreEqual(1, defects.Count);
            Assert.AreEqual(99, defects[0].Depth);
        }
        public void FindDefectsTest()
        {
            Bitmap bmp = Properties.Resources.hand;

            Bitmap gray = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(bmp);

            BlobCounter bc = new BlobCounter(gray);

            bc.ObjectsOrder = ObjectsOrder.Size;
            Blob[] blobs = bc.GetObjectsInformation();
            bc.ExtractBlobsImage(bmp, blobs[0], true);

            Bitmap blob = blobs[0].Image.ToManagedImage();

            BorderFollowing bf      = new BorderFollowing();
            List <IntPoint> contour = bf.FindContour(blob);

            GrahamConvexHull graham = new GrahamConvexHull();
            List <IntPoint>  hull   = graham.FindHull(contour);

            ConvexHullDefects      hullDefects = new ConvexHullDefects(10);
            List <ConvexityDefect> defects     = hullDefects.FindDefects(contour, hull);

            /*  PointsMarker marker = new PointsMarker(hull, Color.Green, 10);
             * marker.ApplyInPlace(blob);
             * ImageBox.Show(blob);
             */

            Assert.AreEqual(2, defects.Count);
            Assert.AreEqual(new IntPoint(130, 10), contour[defects[0].Start]);
            Assert.AreEqual(new IntPoint(93, 109), contour[defects[0].Point]);
            Assert.AreEqual(new IntPoint(64, 9), contour[defects[0].End]);
            Assert.AreEqual(99.549179077148438, defects[0].Depth, 1e-5);
            Assert.IsFalse(double.IsNaN(defects[0].Depth));
            //    Assert.AreEqual(9912.9531239366424, defects[0].Area);

            Assert.AreEqual(new IntPoint(49, 18), contour[defects[1].Start]);
            Assert.AreEqual(new IntPoint(61, 106), contour[defects[1].Point]);
            Assert.AreEqual(new IntPoint(18, 127), contour[defects[1].End]);
            Assert.AreEqual(35.615153852366504, defects[1].Depth, 1e-5);
            Assert.IsFalse(double.IsNaN(defects[1].Depth));
            //    Assert.AreEqual(2293.7535682510002, defects[1].Area);
        }
        static void Main(string[] args)
        {
            Threshold                   thresh        = new Threshold(10);
            Median                      median        = new Median(9);
            Erosion3x3                  erode         = new Erosion3x3();
            Dilatation3x3               dilate        = new Dilatation3x3();
            GrahamConvexHull            hullFinder    = new GrahamConvexHull();
            ConnectedComponentsLabeling ccLabeler     = new ConnectedComponentsLabeling();
            BorderFollowing             contourFinder = new BorderFollowing();
            GrayscaleToRGB              rgb           = new GrayscaleToRGB();
            ConvexHullDefects           defectFinder  = new ConvexHullDefects(10);

            Bitmap img = (Bitmap)Bitmap.FromFile("hand3.jpg");

            Bitmap image = Grayscale.CommonAlgorithms.BT709.Apply(img);

            thresh.ApplyInPlace(image);
            //median.ApplyInPlace(image);
            erode.ApplyInPlace(image);
            dilate.ApplyInPlace(image);

            BlobCounter counter = new BlobCounter(image);

            counter.ObjectsOrder = ObjectsOrder.Area;

            Blob[] blobs = counter.GetObjectsInformation();

            if (blobs.Length > 0)
            {
                counter.ExtractBlobsImage(image, blobs[0], true);

                UnmanagedImage hand = blobs[0].Image;

                var contour = contourFinder.FindContour(hand);

                if (contour.Count() > 0)
                {
                    var initialHull = hullFinder.FindHull(contour);

                    var defects = defectFinder.FindDefects(contour, initialHull);

                    var filteredHull = initialHull.ClusterHullPoints().FilterLinearHullPoints();

                    var palmCenter = defects.Centroid(contour);

                    var wristPoints = filteredHull.SelectWristPoints(defects, contour);

                    Bitmap color = rgb.Apply(hand).ToManagedImage();

                    //BitmapData data = color.LockBits(new Rectangle(0, 0, color.Width, color.Height), ImageLockMode.ReadWrite, color.PixelFormat);
                    //Drawing.Polyline(data, contour, Color.Blue);
                    //Drawing.Polygon(data, filteredHull, Color.Red);
                    //color.UnlockBits(data);

                    Graphics gr = Graphics.FromImage(color);

                    gr.DrawPolygon(new Pen(Brushes.Red, 3), filteredHull.ToPtArray());
                    gr.DrawLines(new Pen(Brushes.Blue, 3), contour.ToPtArray());
                    gr.DrawEllipse(new Pen(Brushes.Red, 3), palmCenter.X - 10, palmCenter.Y - 10, 20, 20);

                    foreach (ConvexityDefect defect in defects)
                    {
                        gr.DrawEllipse(new Pen(Brushes.Green, 6), contour[defect.Point].X - 10, contour[defect.Point].Y - 10, 20, 20);
                    }

                    foreach (AForge.IntPoint pt in filteredHull)
                    {
                        gr.DrawEllipse(new Pen(Brushes.Yellow, 6), pt.X - 10, pt.Y - 10, 20, 20);
                    }

                    foreach (AForge.IntPoint pt in wristPoints)
                    {
                        gr.DrawEllipse(new Pen(Brushes.PowderBlue, 6), pt.X - 10, pt.Y - 10, 20, 20);
                    }

                    ImageBox.Show(color);
                }
            }
        }