OpenCV grayscale morphology

In the previous article, we briefly introduced the commonly used positioning tools, including a variety of template matching and caliper tools such as straight lines and arcs; today we will discuss the image preprocessing tools commonly used in vision (binarization, grayscale morphology, Filtering, image enhancement, frequency domain transformation, etc.); due to space reasons, let’s talk about the functions and usage methods of grayscale morphology in opencv. If there are any deficiencies, you can leave a message in the comment section, and everyone can learn together and make progress together!

1. Grayscale Dilation operation

 First look at a set of test renderings:

                          Figure 1) Figure 2)

Figure 1) is the original picture;

Figure 2) is the effect diagram after expansion.

 The implementation code is as follows:

          if (obj is gray_dilation_rect)//膨胀
                {
                    int _width = (obj as gray_dilation_rect).MarkWidth;
                    int _height = (obj as gray_dilation_rect).MarkHeight;            
                    temimage= Morphological_Proces.DilateImage(imageBuffer,
                                   MorphShapes.Rect,new OpenCvSharp.Size (_width, _height));
                    Cv2.CopyTo(temimage, imageBuffer);                   
                    temimage.Dispose();
                    temimage = null;
                }
 /// <summary>
        /// 图像膨胀:
        /// 对图片的高亮度部分(白色)进行操作,膨胀是对高亮度部分进行"领域扩张"
        /// </summary>
        /// <param name="src">输入图像</param>
        /// <param name="morphShape">结构元素类型</param>
        /// <param name="kernelSize">内核尺寸</param>
        /// <param name="iterations">迭代次数</param>
        /// <returns>返回膨胀后的图像</returns>
        static public Mat DilateImage(Mat src, MorphShapes morphShape, Size kernelSize, int iterations = 1)
        {
            if (src.Empty())
                return null;

            Mat dst = new Mat();
            Mat element = Cv2.GetStructuringElement(morphShape, kernelSize, new CVPoint(-1, -1));
            /*膨胀dilate:
             * src:输入图像(建议二值图)
             * dst:输出图像
             * element:用于膨胀的结构单元。如果element=new Mat()[为空的意思],则使用一个3x3的矩形结构单元
             * anchor :锚点位置,默认为(-1,-1)表示位于中心
             * iterations :膨胀次数
             * borderType :边界模式,一般使用默认值
             * borderValue :边界值,一般采用默认值
             */
            Cv2.Dilate(src, dst, element, new CVPoint(-1, -1), iterations);

            return dst;
        }

The effect of grayscale dilation

Observing the above grayscale expansion effect diagram, it can be seen that grayscale expansion has the following effects:

  • Grayscale expansion can make the bright area surrounded by dark areas in the grayscale image larger, while the dark area surrounded by bright areas becomes smaller (it can be simply understood that the white area in the black area increases successively, black in the white area, the area becomes smaller).

  • Due to the expansion operation, some smaller black points (small gray value) in the grayscale image will be covered (filled) by grayscales with larger grayscale values.

  • The effect of gray scale expansion is more obvious for some regions with stronger gray scale changes, that is, the effect of gray scale expansion is more obvious for areas with a larger gray scale change rate.

2. Grayscale Erode operation

  The test effect diagram is as follows:

                           Figure 3) Figure 4)

Figure 3) is the original picture;

Figure 4) is the effect diagram after corrosion.

 The implementation code is as follows:

  else if (obj is gray_erosion_rect)//腐蚀
                {
                    int _width = (obj as gray_erosion_rect).MarkWidth;
                    int _height = (obj as gray_erosion_rect).MarkHeight;
                    temimage = Morphological_Proces.ErodeImage(imageBuffer,
                                  MorphShapes.Rect, new OpenCvSharp.Size(_width, _height));

                    Cv2.CopyTo(temimage, imageBuffer);
                    temimage.Dispose();
                    temimage = null;

                }
 /// <summary>
        /// 图像腐蚀:
        /// 对图片的高亮度部分(白色)进行操作,腐蚀是对高亮度部分进行"领域蚕食""
        /// </summary>
        /// <param name="src">输入原图</param>
        /// <param name="morphShape">结构元素</param>
        /// <param name="kernelSize">内核尺寸</param>
        /// <param name="iterations">迭代次数</param>
        /// <returns></returns>
        static public Mat ErodeImage(Mat src, MorphShapes morphShape, Size kernelSize, int iterations = 1)
        {
            if (src.Empty())
                return null;

            Mat dst = new Mat();
            Mat element = Cv2.GetStructuringElement(morphShape, kernelSize, new CVPoint(-1, -1));
            /*腐蚀erode:
             * src:输入图像(建议二值图)
             * dst:输出图像
             * element:用于腐蚀的结构单元。如果element=new Mat()[为空的意思],则使用一个3x3的矩形结构单元
             * anchor :锚点位置,默认为(-1,-1)表示位于中心
             * iterations :腐蚀次数
             * borderType :边界模式,一般使用默认值
             * borderValue :边界值,一般采用默认值
             */
            Cv2.Erode(src, dst, element, new CVPoint(-1, -1), iterations);

            return dst;
        }

Characteristics of Grayscale Corrosion

The effect of grayscale erosion, compared with grayscale expansion, shows the opposite characteristics. The specific characteristics are as follows:

  • In the original image, the area size of the bright gamut surrounded by the dark gamut will shrink, and the area size of the dark gamut surrounded by the bright gamut will expand;
  • Some small bright spots in the image will disappear, and some small dark spots will become larger (it can be considered that grayscale has the effect of making the grayscale value of the image smaller, so white with larger grayscale tends to disappear, while Blacks with smaller shades of gray tend to increase in size.)
  • Like grayscale dilation, the effect of grayscale erosion is more pronounced in areas where the grayscale of the image changes drastically (larger image gradients).

3. Gray scale open operation

The test effect diagram is as follows: 

 

                                Figure 5) Figure 6)

Figure 5) is the original picture;

Figure 6) is the effect diagram of the opening operation.

 The implementation code is as follows: 

 else if (obj is gray_opening_rect)//开运算
                {
                    int _width = (obj as gray_opening_rect).MarkWidth;
                    int _height = (obj as gray_opening_rect).MarkHeight;
                    temimage = Morphological_Proces.MorphologyEx(imageBuffer, MorphShapes.Rect,
                                  new OpenCvSharp.Size(_width, _height), MorphTypes.Open);

                    Cv2.CopyTo(temimage, imageBuffer);
                    temimage.Dispose();
                    temimage = null;

                }
 /// <summary>
        /// 形态学处理
        /// </summary>
        /// <param name="src">输入原图</param>
        /// <param name="morphShape">形态结构元素</param>
        /// <param name="kernelSize">内核尺寸</param>
        /// <param name="morphType">形态操作类型</param>
        /// <param name="iterations">迭代次数</param>
        /// <returns></returns>
        static public Mat MorphologyEx(Mat src, MorphShapes morphShape, Size kernelSize,
            MorphTypes morphType,   int iterations = 1)
        {
            if (src.Empty())
                return null;

            Mat dst = new Mat();
            Mat element = Cv2.GetStructuringElement(morphShape, kernelSize, new CVPoint(-1, -1));
            /*腐蚀:对高亮度部分进行"领域蚕食""
             * 膨胀:对高亮度部分进行"领域扩张"
             * 开运算:先腐蚀,后膨胀的过程,可以去掉小的对象
             * 闭运算:先膨后腐蚀的顺序 ,可以填充图像中细小的空洞
             * 形态学梯度:用膨胀图减去腐蚀图,可以将图像边缘凸现出来,可以用其来保留边缘轮廓
             * 顶帽:源图像与开运算的图像相减,用来分离比领近点亮一些的斑块
             * 黑帽:闭运算图像与源图像做差,用来分离比领近点暗一些的斑块
             * 击中击不中:击中击不中变换是形态学中用来检测特定形状所处位置的一个基本工具。它的原理就是使用腐蚀,
             *             如果要在一幅图像A上找到B形状的目标   
             */
            Cv2.MorphologyEx(src, dst, morphType, element, new CVPoint(-1, -1), iterations);

            return dst;
        }

Features of grayscale opening operation

Combining the above two examples, it can be seen that the grayscale opening operation has the following characteristics:

  • For a grayscale image, brighter (higher pixel value) areas of an image are erased if their size is smaller than the size of the structuring element.

 4. Grayscale closing operation

 The test effect diagram is as follows: 

                                Figure 7) Figure 8)

Figure 7) is the original picture;

Figure 8) is the effect diagram of the closing operation.

 The implementation code is as follows: 

 else if (obj is gray_closing_rect)//闭运算
                {
                    int _width = (obj as gray_closing_rect).MarkWidth;
                    int _height = (obj as gray_closing_rect).MarkHeight;
                    temimage = Morphological_Proces.MorphologyEx(imageBuffer, MorphShapes.Rect,
                                   new OpenCvSharp.Size(_width, _height), MorphTypes.Close);

                    Cv2.CopyTo(temimage, imageBuffer);
                    temimage.Dispose();
                    temimage = null;

                }
 /// <summary>
        /// 形态学处理
        /// </summary>
        /// <param name="src">输入原图</param>
        /// <param name="morphShape">形态结构元素</param>
        /// <param name="kernelSize">内核尺寸</param>
        /// <param name="morphType">形态操作类型</param>
        /// <param name="iterations">迭代次数</param>
        /// <returns></returns>
        static public Mat MorphologyEx(Mat src, MorphShapes morphShape, Size kernelSize,
            MorphTypes morphType,   int iterations = 1)
        {
            if (src.Empty())
                return null;

            Mat dst = new Mat();
            Mat element = Cv2.GetStructuringElement(morphShape, kernelSize, new CVPoint(-1, -1));
            /*腐蚀:对高亮度部分进行"领域蚕食""
             * 膨胀:对高亮度部分进行"领域扩张"
             * 开运算:先腐蚀,后膨胀的过程,可以去掉小的对象
             * 闭运算:先膨后腐蚀的顺序 ,可以填充图像中细小的空洞
             * 形态学梯度:用膨胀图减去腐蚀图,可以将图像边缘凸现出来,可以用其来保留边缘轮廓
             * 顶帽:源图像与开运算的图像相减,用来分离比领近点亮一些的斑块
             * 黑帽:闭运算图像与源图像做差,用来分离比领近点暗一些的斑块
             * 击中击不中:击中击不中变换是形态学中用来检测特定形状所处位置的一个基本工具。它的原理就是使用腐蚀,
             *             如果要在一幅图像A上找到B形状的目标   
             */
            Cv2.MorphologyEx(src, dst, morphType, element, new CVPoint(-1, -1), iterations);

            return dst;
        }

The effect of grayscale closing operation

Through the above analysis, the grayscale closing operation has the following effects:

  • If in an image, the size of the bright area surrounded by the dark area is smaller than the size of the structural element, the grayscale closing operation will enhance the brightness of the bright area.

  • If the size of the structural element of the grayscale opening operation is larger, the influence on the original image is also greater, that is, the area where the bright color is enhanced in the original image is also larger.

 Summarize:

   The above briefly introduces some functions related to grayscale morphology in opencv. The following articles will introduce other tools for image pre-processing. If you are interested, you can discuss and learn together; the source addresses of all the functions introduced in the previous articles are as follows:

OPENCV+C#+C++ development resources and source code sharing_%Onmyway's Blog-CSDN Blog

PS: Success is a little progress every day!

Guess you like

Origin blog.csdn.net/qq_42857680/article/details/130971573