Implementation process:
- edge search
- Contour discovery
- Draw outline
API:
- Find contours
public static void FindContours(InputArray image, out Point[][] contours, out HierarchyIndex[] hierarchy, RetrievalModes mode, ContourApproximationModes method, Point? offset = null);
parameter
image | Input image, grayscale image |
---|---|
contours | Contour output |
hierarchy | Optional output vector containing information about the image topology |
mode | Contour search mode |
method | contour approximation method |
offset | Optional parameter, offset, such as (-10, 10) means that the contour point is offset by 10 pixels in the negative X direction and 10 pixels in the positive Y direction. |
Contour search mode:
- RetrievalModes.EXTERNAL - extract only the outermost contours
- RetrievalModes.LIST - extract all contours and place them in the list
- RetrievalModes.CCOMP - extracts all contours and organizes them into a two-level hierarchy: the top layer is the outer boundary of the connected domain, and the second layer is the inner boundary of the hole.
- RetrievalModes.TREE - extract all contours and reconstruct the entire hierarchy of nested contours
Contour approximation method:
- ContourApproximationModes.ApproxNone - Translate (convert) all points from chain code form to point sequence form
- ContourApproximationModes.ApproxSimple - compresses horizontal, vertical and diagonal divisions, that is, the function only retains the end pixels;
- ContourApproximationModes.ApproxTC89L1/ContourApproximationModes.ApproxTC89KCOS - applies the Teh-Chin chain approximation algorithm. CV_LINK_RUNS - uses a completely different contour extraction algorithm through horizontal fragments connected to 1. Only the CV_RETR_LIST extraction mode can be used in this method.
- Draw outline
public static void DrawContours(InputOutputArray image, IEnumerable<IEnumerable<Point>> contours, int contourIdx, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, IEnumerable<HierarchyIndex>? hierarchy = null, int maxLevel = int.MaxValue, Point? offset = null);
parameter
image | input image |
---|---|
contours | Contour output |
contourIdx | Contour number |
color | outline color |
thickness | Line width |
lineType | line type |
hierarchy | Information about contour image topology |
maxLevel | Maximum level for drawing outlines |
code demo
private string picFile = "";
private Mat inputMat;
private Mat outMat;
private Mat displayMat;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
if (fileDialog.ShowDialog() == DialogResult.OK)
{
picFile = fileDialog.FileName;
inputMat = Cv2.ImRead(picFile, ImreadModes.Grayscale);
displayMat = Cv2.ImRead(picFile, ImreadModes.AnyColor);
outMat = new Mat(new Size(inputMat.Cols, inputMat.Rows), inputMat.Type());
Cv2.Canny(inputMat, outMat, 30, 90);
//仅查找外轮廓
Cv2.FindContours(outMat, out OpenCvSharp.Point[][] contours,out HierarchyIndex[] outputArray, RetrievalModes.External,ContourApproximationModes.ApproxNone);
//绘制轮廓
Scalar color = new Scalar(255, 0, 0);
for (int i = 0; i < contours.Length; i++)
{
if (contours[i].Length > 100)
{
Cv2.DrawContours(displayMat,contours, i,color,5,LineTypes.Link8, outputArray, 0);
}
}
picBox_Display.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(inputMat);
picBox_After.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(displayMat);
}
}
draw all contours
Draw only the outer contour
Additional notes:
The OpenCV library used in .NET in this case is OpenCvSharp4