Have you learned the six steps of c# OpenCvSharp contour drawing (6)

Contour drawing is done through the following 6 steps:

  1. Read the image Cv2.ImRead();
  2. Convert the image to grayscale Cv2.CvtColor();
  3. Blur the image and filter Cv2.Blur();
  4. Edge detection Canny();
  5. Find contoursCv2.FindContours();
  6. Draw contoursCv2.DrawContours();

Legend (please entertain yourself by playing poker)

 // 1、读取图像
 Mat image = Cv2.ImRead("0.jpg", ImreadModes.Color);

 // 2、 将图像转换为灰度图像
 Mat src_gray = new Mat();
 Cv2.CvtColor(image, src_gray, ColorConversionCodes.BGR2GRAY);

 // 3、 滤波
 Cv2.Blur(src_gray, src_gray, new OpenCvSharp.Size(3, 3));
 // 4、Canny边缘检测
 Mat canny_Image = new Mat();
 // 输入、输出、最小阀值、最大阀值
 Cv2.Canny(src_gray, canny_Image, 100, 200);

 //5、查找轮廓
 // 寻找轮廓
 OpenCvSharp.Point[][] contours;
 HierarchyIndex[] hierarchy;

 Cv2.FindContours(canny_Image, out contours, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxNone, new OpenCvSharp.Point(0, 0));
 // 将结果画出并返回结果
 Mat dst_Image = Mat.Zeros(canny_Image.Size(), image.Type());
 for (int i = 0; i < contours.Length; i++)
 {
     // 轮廓的颜色为绿色
     Scalar color = new Scalar(0, 255, 0);
     Cv2.DrawContours(dst_Image, contours, i, color, 2, LineTypes.Link8, hierarchy);
     
 }
 Cv2.ImShow("dst_Image", dst_Image); // 显示图像
 Cv2.WaitKey(0);

1. Reading the image Cv2.ImRead(); not introduced anymore

2. Convert the image to grayscale image Cv2.CvtColor(); will not be introduced

3. Filter Cv2.Blur()

Used to blur images.

Cv2.Blur(src, dst, ksize, anchor,borderType,borderValue)

parameter explain
src Input image, which can be a single-channel or multi-channel image.
dst The output image has the same dimensions and depth as the input image.
ksize The size of the blur kernel. It is a 2D Size structure that specifies the core size in the horizontal and vertical directions. Its width and height must be positive odd numbers, such as (3,3), (5,5), etc.
anchor The anchor point position of the blur kernel, the default is (-1,-1), indicating the center point of the kernel.
borderType Border mode, the default is BorderType.Default, which means using the default border mode
borderValue Border value, used when the border mode is BorderType.Constant, the default is Scalar(0).


 new OpenCvSharp.Size(3, 3) effect

4. Edge detection cv2.Canny()

The principle of the Canny edge detection algorithm is: first perform Gaussian filtering on the image, then calculate the gradient of the image through the Sobel operator, then extract the local maximum as the edge point through non-maximum suppression, and finally connect the edge points through double threshold detection .

 cv2.Canny(image,edges,threshold1,threshold2,apertureSize,L2gradient)

parameter explain
image The input image can be a single-channel grayscale image or a 3-channel color image.
edges The output edge image is a single-channel binary image.
threshold1 The first threshold is used for low-threshold filtering of edge strength.
threshold2 The second threshold is used for high-threshold filtering of edge strength.
apertureSize The aperture size of the Sobel operator, the default is 3.
L2gradient A Boolean value specifying whether to use the L2 norm when calculating gradient amplitudes. The default is false.

 Cv2.Blur(src_gray, src_gray, new OpenCvSharp.Size(3, 3)) 效果

5. Find contours Cv2.FindContours() 

It is used to detect contours in images, that is, to find the set of all continuous points in the image. This function needs to input a binary image, and the background color in the image is black and the foreground color is white. The function returns a list containing all contours, each contour is an array of points.

Cv2.FindContours(image, mode, method[, contours[, hierarchy[, offset]]])

 FindContours(InputArray image, out Point[][] contours, out HierarchyIndex[] hierarchy, RetrievalModes mode, ContourApproximationModes method, Point? offset = null) 

Precautions:

  • The input image must be a binary image, that is, it only contains black and white pixel values.
  • Contour detection can only be applied to binary images, so image thresholding or other image processing operations are usually required before applying the function.

6. Draw contours Cv2.DrawContours()

Draws the detected contours on the image, showing the boundaries in the image with the specified color and line width.

  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)

Guess you like

Origin blog.csdn.net/hb_ljj/article/details/135181825