opencv——non-linear filtering principle and operation

The two most commonly used nonlinear filters in opencv are median filter and bilateral filter

Median filtering-medianBulr

The median filter is very simple, that is , the median value in a certain area , and the gray value of each pixel is set to the median of the gray values ​​of all pixels in a certain neighborhood window of that point. This area is a square area, and the side length of the square, that is, the number of rows and columns of the area are all odd numbers. The median value has a good inhibitory effect on salt and pepper noise.
Insert picture description here

API

void medianBlur(
    InputArray src,
    OutputArray dst, 
    int ksize
);

(1) src of InputArray type, input image. Input 1, 3 or 4 channel images; when ksize is 3 or 5, the image depth should be CV_8U, CV_16U or CV_32F, for larger apertures, it can only be CV_8U.

(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 int type, linear aperture size; it must be an odd number and greater than 1, for example: 3, 5, 7 ….

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);

	//中值滤波
	medianBlur(src, dst, 7);
	imshow("medianBlur_src", dst);

	waitKey(0);
	return 0;
}

effect

Insert picture description here

Bilateral Filter-bilateralFilter

Bilateral filtering is a compromise processing that combines the spatial proximity of the image and the similarity of pixel values , while considering the spatial information and gray-scale similarity to achieve the purpose of edge preservation and denoising. It is simple, non-iterative, and partial. The advantage of the bilateral filter is that it can be used for edge preservation . Generally, the Wiener filter or Gaussian filter used in the past to denoise will blur the edges more obviously, and the protection effect for high-frequency details is not obvious.

Bilateral filter has one more Gaussian variance sigma-d than Gaussian filter. It is a Gaussian filter function based on spatial distribution. So near the edge, pixels farther away will not affect the pixel value on the edge too much. Ensure the preservation of pixel values ​​near the edge.
However, due to the preservation of too much high-frequency information, for the high-frequency noise in the color image, the bilateral filter cannot cleanly filter out, only the low-frequency information can be better filtered.

Insert picture description here
In the middle is the Gaussian distribution of image weights in space.
This image is composed of a spatial kernel and a range kernel, which is used as the operator of bilateral filtering.

  • Spatial core: In terms of space, the image is 3 x 3 or 5 x 5, each with weight
  • Range kernel: It is for the pixel value. If the pixel value difference is within a certain range, it will be blurred. If the gap is too large, it will not be blurred, so that the edges can be preserved.
  • Therefore, two parameters must be input for bilateral blur, one is the window size of the spatial domain and the window size of the value domain.

to sum up:

  • Mean blur can not overcome the defect of edge pixel information loss. The reason is that the mean filter is based on the mean weight

  • Gaussian blur partially overcomes this defect, but it cannot be completely avoided because the difference in pixel values ​​is not considered (maybe adjacent pixel values ​​are 0 and 255)

  • Gaussian bilateral blur-is a filtering method for edge preservation, which avoids the loss of edge information and preserves the image contour (the edge can be preserved by considering the threshold of the difference between adjacent pixels)

API

void bilateralFilter(
    InputArray src, 
    OutputArray dst, 
    int d,
    double sigmaColor, 
    double sigmaSpace,
    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) Int type d, the diameter of each pixel neighborhood used in the filtering process. If it is not positive, it is calculated from sigmaSpace.

(4) The
filter sigma in the double type sigmaColor color space. The larger the parameter value, it means that more colors in the pixel neighborhood (see sigmaSpace) will be mixed together, resulting in a larger semi-isochromatic area.

(5) Double type sigmaSpace, filter sigma in the coordinate space. The larger the parameter value, it means that pixels farther away will affect each other, as long as their colors are close enough (see sigmaColor). When d is greater than 0, it specifies the size of the neighborhood regardless of sigmaSpace. Otherwise, d is proportional to sigmaSpace.

(6) The borderType of int type is used to extrapolate the border mode of pixels outside the image. For details, see cv::BorderTypes.

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/house1.jpg");
	if (!src.data)
	{
    
    
		cout << "could not load image !";
		return -1;
	}
	imshow("src", src);

	//双边滤波
	bilateralFilter(src, dst, 15, 150, 3);
	imshow("bilateralFilter_src", dst);


	waitKey(0);
	return 0;
}

effect

Insert picture description here
As you can see, the edge retention is done very well

Guess you like

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