private static ConvexityDefect extractDefect(List<IntPoint> contour, int startIndex, int endIndex)
{
// Navigate the contour until the next point of the convex hull,
// taking note of the distance between the current contour point
// and the line connecting the two consecutive convex hull points
IntPoint start = contour[startIndex];
IntPoint end = contour[endIndex];
Line line = Line.FromPoints(start, end);
double maxDepth = 0;
int maxIndex = 0;
if (startIndex < endIndex)
{
for (int i = startIndex; i < endIndex; i++)
{
double d = line.DistanceToPoint(contour[i]);
if (d > maxDepth)
{
maxDepth = d;
maxIndex = i;
}
}
}
else
{
for (int i = startIndex; i < contour.Count; i++)
{
double d = line.DistanceToPoint(contour[i]);
if (d > maxDepth)
{
maxDepth = d;
maxIndex = i;
}
}
for (int i = 0; i < endIndex; i++)
{
double d = line.DistanceToPoint(contour[i]);
if (d > maxDepth)
{
maxDepth = d;
maxIndex = i;
}
}
}
return new ConvexityDefect(maxIndex, startIndex, endIndex, maxDepth);
}