Accord.Math.Geometry.ConvexHullDefects.FindDefects C# (CSharp) Method

FindDefects() public method

Finds the convexity defects in a contour given a convex hull.
public FindDefects ( List contour, List convexHull ) : List
contour List The contour.
convexHull List The convex hull of the contour.
return List
        public List<ConvexityDefect> FindDefects(List<IntPoint> contour, List<IntPoint> convexHull)
        {
            if (contour.Count < 4)
                throw new ArgumentException("Point sequence size should have at least 4 points.");

            if (convexHull.Count < 3)
                throw new ArgumentException("Convex hull must have at least 3 points.");


            // Find all convex hull points in the contour
            int[] indexes = new int[convexHull.Count];
            for (int i = 0, j = 0; i < contour.Count; i++)
            {
                if (convexHull.Contains(contour[i]))
                {
                    indexes[j++] = i;
                }
            }


            List<ConvexityDefect> defects = new List<ConvexityDefect>();

            // For each two consecutive points in the convex hull
            for (int i = 0; i < indexes.Length - 1; i++)
            {
                ConvexityDefect current = extractDefect(contour, indexes[i], indexes[i + 1]);

                if (current.Depth > MinimumDepth)
                    defects.Add(current);
            }

            // Consider area between the last point and the first point
            {
                ConvexityDefect current = extractDefect(contour, indexes[indexes.Length - 1], indexes[0]);

                if (current.Depth > MinimumDepth)
                    defects.Add(current);
            }

            return defects;
        }

Usage Example

        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);
            // Accord.Controls.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);
        }
All Usage Examples Of Accord.Math.Geometry.ConvexHullDefects::FindDefects