我试图从这张图片中得到一个完整的黑暗“D”:
使用此代码,我得到以下结果:
```
Cv2.Threshold(im_gray, threshImage, 80, 255, ThresholdTypes.BinaryInv); // Threshold to find contour
Cv2.FindContours(
threshImage,
out contours,
out hierarchyIndexes,
mode: RetrievalModes.Tree,
method: ContourApproximationModes.ApproxSimple
);
double largest_area = 0;
int largest_contour_index = 0;
Rect rect = new Rect();
//Search biggest contour
for (int i = 0; i <contours.Length; i++)
{
double area = Cv2.ContourArea(contours[i]); // Find the area of contour
if (area > largest_area)
{
largest_area = area;
largest_contour_index = i; //Store the index of largest contour
rect = Cv2.BoundingRect(contours[i]); // Find the bounding rectangle for biggest contour
}
}
Cv2.DrawContours(finalImage, contours, largest_contour_index, new Scalar(0, 0, 0), -1, LineTypes.Link8, hierarchyIndexes, int.MaxValue);
```