opencv-principle and operation of linear filtering (box filtering, mean filtering, Gaussian filtering)

principle

  • Smooth/Blur is one of the simplest and most commonly used operations in image processing

  • One of the reasons for using this operation is to reduce noise when preprocessing the image

  • The use of Smooth/Blur operation is behind the mathematical convolution calculation
    Insert picture description here

  • Usually these convolution operator calculations are linear operations, so it is also called linear filtering

convolution

How to understand convolution
Insert picture description here
Suppose there is a 6x6 image pixel matrix.

Convolution process: 6x6 is a 3x3 window, moving from left to right, from top to bottom, the sum of each yellow pixel and the corresponding gray pixel value is averaged and assigned to the central red pixel as its new after convolution processing The pixel value. Move one pixel grid at a time.

Why take the average: the pixel value cannot exceed 255

Image blur principle

Box filter

The cores used are as follows
Insert picture description here

Mean filtering (normalized box filtering)

Insert picture description here
The kind of convolution mentioned above is mean filtering,
regardless of weight.

Gaussian filtering

Gaussian filtering preserves some characteristics of the image on the basis of blurring the image.
For example, if the value of a certain pixel is very large, its value is still very large after Gaussian filtering, and it will not be affected by other points with small values.

So you can understand Gaussian filtering as: mean filtering with weights.
Insert picture description here
Note: The size of σ determines the width of the Gaussian function.

Gaussian kernel (mask)

In theory, the Gaussian distribution has non-negative values ​​in all domains, which requires an infinite convolution kernel. In fact, you only need to take the value within 3 times the standard deviation around the mean, and just remove the other parts.

The two important steps of Gaussian filtering are to find the Gaussian template and then perform convolution . The template (the mask is also called a mask or a Gaussian kernel in some places in the reference). So you need to know how it came at this time? How to use it?

Take a chestnut:
Assuming that the coordinates of the center point are (0,0), then take the coordinates of the 8 points closest to it. For calculation, the value of σ needs to be set. Assuming σ=1.5, the Gaussian template with a blur radius of 1 is as follows. At
Insert picture description here
this time, we have to make sure that these nine points add up to 1 (this is the characteristic of the Gaussian template- weight ), and the sum of the weights of these 9 points is equal to 0.4787147, so the above 9 values ​​have to be divided by 0.4787147 respectively to get the final Gaussian template.

Insert picture description here

Gaussian filter calculation

With the Gaussian template, the calculation of Gaussian filtering will be smooth.
Take a chestnut:
Assuming that there are 9 pixels, the Gaussian filter of gray value (0-255) is calculated as follows:
Insert picture description here
Reference source: (https://blog.csdn.net/nima1994/article/details/79776802)

Adding up these 9 values ​​is the value of the Gaussian filter at the center point.
Repeat this process for all points, and get the Gaussian blurred image.

Gaussian filtering steps

In summary, the steps can be summarized:

(1) Move the central element of the relevant core so that it is directly above the pixel to be processed in the input image
(2)
Take the pixel value of the input image as the weight, and multiply it by the relevant core (3) Add the results obtained in the above steps to do To output
simply is a process of obtaining a Gaussian template according to the Gaussian distribution and then doing convolution and addition.

related functions

Box filtering-boxblur

void boxFilter(
    InputArray src,
    OutputArray dst, 
    int ddepth, 
    Size ksize, 
    Point anchor=Point(-1,-1), 
    boolnormalize=true, 
    int borderType=BORDER_DEFAULT 
);

The meaning of the function parameters is as follows:

(1) src of InputArray type, input image. This function processes the channels independently and can process pictures of any number of channels, but it should be noted that the depth of the picture to be processed should be one of CV_8U, CV_16U, CV_16S, CV_32F and CV_64F.

(2) The dst of the OutputArray type, that is, the target image, has the same size and type as the input image.

(3) Int type ddepth, the depth of the output image, -1 means the depth of the original image is used, that is, src.depth().

(4) The ksize of the Size type (explained later on the Size type), the size of the kernel. Generally write Size( w, h) to represent the size of the kernel (where w is the pixel width and h is the pixel height). Size(3,3) means 3x3 core size, Size(5,5) means 5x5 core size

(5) The anchor of the Point type represents the anchor point (that is, the point to be smoothed). Note that it has the default value Point (-1, -1). If the coordinate of this point is negative, it means that the center of the core is the anchor point, so the default value Point(-1,-1) means that the anchor point is at the center of the core.

(6) Normalize of the bool type, the default value is true, an identifier that indicates whether the kernel is normalized by its area.

(7) The borderType of int type is used to infer a certain border mode of the external pixels of the image. There is a default value of BORDER_DEFAULT.

Code

#include<opencv2\opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main()
{
    
    
	Mat src, dst;
	src = imread("C:/Users/86176/Pictures/pics/house.jpg");
	if (!src.data)
	{
    
    
		cout << "could not load image !";
		return -1;
	}
	imshow("src", src);

	//方框滤波
	boxFilter(src, dst, -1, Size(5, 5));
	imshow("boxFilter_src", dst);

	waitKey(0);
	return 0;
}

effect

Insert picture description here

Mean filtering-blur

void blur(
    InputArray src, 
    OutputArray dst, 
    Size ksize, 
    Point anchor=Point(-1,-1), 
    int borderType=BORDER_DEFAULT 
);

(1) src of InputArray type, input image. This function processes the channels independently and can process pictures of any number of channels, but it should be noted that the depth of the picture to be processed should be one of CV_8U, CV_16U, CV_16S, CV_32F and CV_64F.

(2) The dst of the OutputArray type, that is, the target image, has the same size and type as the input image.

(3) ksize of the Size type (explained later on the Size type), the size of the kernel. Generally write Size( w, h) to represent the size of the kernel (where w is the pixel width and h is the pixel height). Size(3,3) means 3x3 core size, Size(5,5) means 5x5 core size

(4) The anchor of the Point type represents the anchor point (that is, the point to be smoothed). Note that it has the default value Point (-1, -1). If the coordinate of this point is negative, it means that the center of the core is the anchor point, so the default value Point(-1,-1) means that the anchor point is at the center of the core.

(5) The borderType of int type is used to infer a certain border mode of the external pixels of the image. There is a default value of BORDER_DEFAULT.

Code

#include<opencv2\opencv.hpp>
#include

using namespace std;
using namespace cv;

int main()
{
Mat src, dst;
src = imread(“E:/image/Girl2.png”);
if (!src.data)
{
cout << “could not load image !”;
return -1;
}
imshow(“src”, src);

//均值滤波
blur(src, dst, Size(3, 3), Point(-1, -1));
imshow("blur_src", dst);

waitKey(0);
return 0;

}

effect

Insert picture description here

Gaussian filtering-GaussianBlur

void GaussianBlur(
    InputArray src,
    OutputArray dst, 
    Size ksize, 
    double sigmaX, 
    double sigmaY=0, 
    intborderType=BORDER_DEFAULT 
);

(1) src of InputArray type, input image. This function processes the channels independently and can process pictures of any number of channels, but it should be noted that the depth of the picture to be processed should be one of CV_8U, CV_16U, CV_16S, CV_32F and CV_64F.

(2) The dst of the OutputArray type, that is, the target image, has the same size and type as the input image.

(3) ksize of the Size type (explained later on the Size type), the size of the kernel. Generally write Size( w, h) to represent the size of the kernel (where w is the pixel width and h is the pixel height). Size(3,3) means 3x3 core size, Size(5,5) means 5x5 core size

(4) sigmaX of double type represents the standard deviation of the Gaussian kernel function in the X direction.

(5) sigmaY of the double type represents the standard deviation of the Gaussian kernel function in the Y direction. If sigmaY is zero, set it to sigmaX. If sigmaX and sigmaY are both 0, then it is calculated by ksize.width and ksize.height.

(6) The borderType of int type is used to infer a certain border mode of the external pixels of the image. There is a default value of BORDER_DEFAULT.

Code

#include<opencv2\opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main()
{
    
    
	Mat src, dst;
	src = imread("C:/Users/86176/Pictures/pics/house.jpg");
	if (!src.data)
	{
    
    
		cout << "could not load image !";
		return -1;
	}
	imshow("src", src);

	//高斯滤波
	GaussianBlur(src, dst, Size(3, 3), 0, 0);
	imshow("GaussianBlur_src", dst);



	waitKey(0);
	return 0;
}

effect

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_28258885/article/details/112606498