OpenCvSharp - Gaussian/mean filtering, erosion and expansion, opening/closing operations (source code attached)

Table of contents

Core knowledge:

 Core functions:

CV2.Blur() mean filter

Cv2.GaussianBlue() Gaussian filter

Cv2.GetStructuringElement() creates a structuring element for morphological operations

Cv2.Erode() corrosion

Cv2.Dilate() dilation

 Cv2.MorphologyEx() Morphological operations (corrosion expansion, opening and closing operations)

Example:

source code:


Core knowledge:

  1. Mean filtering : A commonly used image smoothing method, which reduces the noise in the image by averaging the pixel values ​​within the pixel range, thereby achieving an image smoothing effect and making the image blurr.

  2. Dilation : A morphological operation used to expand bright areas (areas with larger pixel values) in the image. Its principle is to slide the structural element on the image, and set the pixels within the area covered by the structural element to the maximum pixel value. Dilation operations can increase the size of bright regions, fill small holes, and connect adjacent regions.

  3. Erosion : A morphological operation used to shrink bright areas in an image while expanding dark areas (areas with smaller pixel values). The principle is to slide structural elements on the image and set the pixels in the area covered by the structural elements to the minimum pixel value. Erosion operations can eliminate small noises, segment connected regions, and change the shape of regions.

  4. Opening operation (Opening) : First perform the erosion operation, and then perform the expansion operation. Usually used to remove small noise points.

  5. Closing operation (Closing) : The expansion operation is performed first, and then the erosion operation is performed. Usually used to fill small cavities.

 Core functions:

CV2.Blur() mean filter

Cv2.Blur(Mat src, Mat dst, Size ksize, Point anchor = null, BorderTypes borderType = BorderTypes.Default);
  • src: Input image.
  • dst: The output image, the result of the filtering operation will be stored here.
  • ksize: Kernel size, should be a positive odd number.
  • anchor: Kernel anchor point, the default is the center point.
  • borderType: Boundary expansion mode, default is BorderTypes.Default.

Cv2.GaussianBlue() Gaussian filter

void Cv2.GaussianBlur( src, dst, Size ksize, double sigmaX, double sigmaY = 0, BorderTypes borderType = BorderTypes.Default);
  • ksize: The size of the Gaussian kernel must be a positive odd number. This parameter determines the degree of blurring.
  • sigmaX: Gaussian kernel standard deviation in the X direction. If set to 0, it is ksize.widthcalculated from .
  • sigmaY: Gaussian kernel standard deviation in Y direction. If set to 0, it is sigmaXthe same as .
  • borderType: Boundary filling method, default is BorderTypes.Default.

 

Cv2.GetStructuringElement() creates a structuring element for morphological operations

 Used to create structural elements required for expansion and corrosion operations

Mat Cv2.GetStructuringElement(MorphShapes shape, Size ksize, Point anchor = null);
  • shape: The shape of the structuring element, which can be one of MorphShapes.Rect(rectangle), MorphShapes.Ellipse(ellipse), or MorphShapes.Cross(cross).
  • ksize: The size of the structural element, that is, the size of the matrix.
  • anchor: The anchor point of the structural element, defaults to null. In most cases, this parameter does not need to be set.

Cv2.Erode() corrosion

Cv2.Erode(Mat src, Mat dst, Mat kernel, Point anchor, int iterations, BorderTypes borderType, Scalar borderValue);
  • src: Input image.
  • dst: output image, the result of the erosion operation will be stored here.
  • kernel: Structural element that defines the shape and size of the corrosion.
  • anchor: The anchor point of the structural element, the default is the center point.
  • iterations: The number of iterations of corrosion, the default is 1.
  • borderType: Boundary expansion mode, default is BorderTypes.Default.
  • borderValue: The value to fill in when the boundary expands, the default is the default boundary value.

Cv2.Dilate() 膨胀

Cv2.Dilate(Mat src, Mat dst, Mat kernel, Point anchor = null, int iterations = 1, BorderTypes borderType = BorderTypes.Default, Scalar borderValue = default)

 参数同腐蚀

 Cv2.MorphologyEx() Morphological operations (corrosion expansion, opening and closing operations)

Cv2.MorphologyEx(Mat src, Mat dst, MorphTypes op, Mat kernel, OpenCvSharp.Point anchor = null, int iterations = 1, BorderTypes borderType = BorderTypes.Constant, Scalar borderValue = default);
  • src: Input image, source image for morphological operations.
  • dst: Output image, the results of morphological operations will be saved here.
  • op: The type of morphological operation, the options are MorphTypesenumeration, including MorphTypes.Erode, MorphTypes.Dilate, MorphTypes.Open(开运算), MorphTypes.Close(闭运算)etc.
  • kernel: Structural element that defines the shape and size of morphological operations.
  • anchor: The anchor point position of the structural element. The default is null, indicating that the anchor point of the structural element is in the center.
  • iterations: The number of iterations of the morphological operation, indicating the number of times to apply the operation, the default is 1.

Example:

Mat srcImage = Cv2.ImRead("input.jpg", ImreadModes.Grayscale);
Mat element = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));

// 腐蚀
Mat erodedImage = new Mat();
Cv2.Erode(srcImage, erodedImage, element);

// 膨胀
Mat dilatedImage = new Mat();
Cv2.Dilate(srcImage, dilatedImage, element);

// 开运算
Cv2.MorphologyEx(srcImage, dstImageOpen, MorphTypes.Open,element);

// 闭运算
Cv2.MorphologyEx(srcImage, dstImageClose, MorphTypes.Close, element);

source code:

1. Create a form program with four buttons

 2. Source code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;

namespace 图像模糊_均值滤波
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Mat srcImage; // 成员变量用于在不同方法中共享图像

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //选择图像文件
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "Image Files(*.jpg;*.png;*.bmp;*)|*.jpg;*.png;*.bmp;";
                if (openFileDialog.ShowDialog() != DialogResult.OK)
                    return;

                string imagePath = openFileDialog.FileName;

                // 读取图像
                 srcImage = Cv2.ImRead(imagePath);
                // 显示原图
                Cv2.NamedWindow("原图", WindowFlags.Normal);
                Cv2.ResizeWindow("原图", 800, 640);
                Cv2.ImShow("原图", srcImage);
            }
            catch (OpenCvSharp.OpenCVException ex)
            {
                //处理异常
                MessageBox.Show("图像处理发生错误" + ex.Message);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (srcImage == null)
            {
                MessageBox.Show("请先加载图像!");
                return;
            }
            // 均值滤波
            Mat dstImage = new Mat();
            Cv2.Blur(srcImage, dstImage, new OpenCvSharp.Size() { Width = 7, Height = 7 });

            Cv2.ImShow("均值滤波效果", dstImage);
            Cv2.WaitKey();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (srcImage == null)
            {
                MessageBox.Show("请先加载图像!");
                return;
            }

            // 进行腐蚀操作
            Mat element = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size() { Width = 15, Height = 15 });
            Mat dstImage2 = new Mat();
            Cv2.Erode(srcImage, dstImage2, element);
      
          
            Cv2.ImShow("腐蚀效果", dstImage2);
            Cv2.WaitKey();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (srcImage == null)
            {
                MessageBox.Show("请先加载图像!");
                return;
            }

            // 进行膨胀操作
            Mat element = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size() { Width = 15, Height = 15 });
            Mat dstImage3 = new Mat();
            Cv2.Dilate(srcImage, dstImage3, element);

            Cv2.ImShow("膨胀效果", dstImage3);
            Cv2.WaitKey();
        }
    }
    
}

Guess you like

Origin blog.csdn.net/m0_55074196/article/details/132450231