[Notes] Learning opencv nonlinear filter 010 of the operation principle of an image (median filtering, bilateral filtering)

I. Introduction

Indeed, for a long time I did not write a blog, back fill the hole, but opencv4 has been out for a long time, of course I still lingered 3. As the basis for completely sufficient, first foundation pit filled out it is indeed a very tangled own research, I do not know, or learn it, I think too much, as a good school.

If you want to see other content related to learning about OpenCV presentation, learning tutorials, code practical, common error and solutions, you can directly see my OpenCV Category:

【OpenCV系列】:https://blog.csdn.net/shuiyixin/article/category/7581855

If you want to learn more about computer vision, OpenCV, machine learning, deep learning and other related technologies, to communicate with more chiefs, then scan the next Fanger Wei code to join us now!

 

Second, the linear filtering and nonlinear filtering

1 Review

1. Category

Last time I talked about it linear filtering and nonlinear filtering, we talk about three important linear filtering. Also mentioned two kinds of nonlinear filtering. Linear and nonlinear filtering used as follows:

(1) linear filtering : block filtering, mean filtering, Gaussian filtering;

(2) nonlinear filtering : median filtering, bilateral filtering

 

2. Classification linear filter

We also mentioned the class classification linear filter, often used linear filter input signal frequency reject unwanted frequencies or a choice from a number of desired frequencies. Several common linear filter as follows:

(1) low-pass filter: by allowing a low frequency wave.

(2) high-pass filter: a high frequency wave allowed to pass.

(3) band-pass filter: wave allows through a range of frequencies.

(4) bandstop filter: blocking by a range of frequency waves and by allowing other frequency wave.

(5) all pass filter: allows all wave frequency by changing only the phase relation of the wave.

(6) a notch filter: a narrow stop band rejection filter specific frequency range to pass.

 

2, compared to

1. linear filter

Raw data filtering result is a linear filter arithmetic operation, i.e. implemented by arithmetic operation and the like, such as mean filter (average value of gradation values ​​of pixels within the template), Gaussian filter (Gaussian weighted average), etc. . Since the linear filter is an arithmetic operation, a fixed template, so the transfer function of the filter can be determined and is the only (i.e., the transfer function of the Fourier transform of the template).

2. nonlinear filter

Raw data filtering result is a nonlinear filter logic, i.e. logic operation to achieve such a maximum value filter, minimum value filter, a median filter or the like, by comparing the gradation value of a certain neighborhood size achieved, there is no fixed template and, therefore, no specific transfer function (because there is no template Fourier transform), Further, dilation and erosion are the maximum value, minimum value filter implementation.

Three, OpenCV non-linear filtering operation

opencv the two most common nonlinear filtering is median filtering and bilateral filtering.

1, median filtering function --medianBlur

Median filtering is very simple, that is, the value of an area inside the pixel values of all the gray values in the gray value of each pixel is set to the point of a neighborhood window .. This area is a square area, i.e., the number of rows side length and area are square and odd columns. Values are in good inhibition salt and pepper noise.

E.g:

In the figure is a square area, in ascending order:

14,15,17,28,31,53,65,72,84

Median: 31.

1.API

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

Function parameters are as follows:

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

(2) OutputArray type dst, i.e. the target image, the same size and type of the input image.

(3) int type ksize, linear aperture size; it must be odd and greater than 1, for example: 3,5,7 ....

2. The code shows

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

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

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

	waitKey(0);
	return 0;
}

3. Perform results

Artwork
ksiz = 3
ksiz A = 7

2, bilateral filtering function --bilateralFilter

双边滤波是结合图像的空间邻近度和像素值相似度的一种折中处理,同时考虑空域信息和灰度相似性,达到保边去噪的目的。具有简单、非迭代、局部的特点。双边滤波器的好处是可以做边缘保存,一般过去用的维纳滤波或者高斯滤波去降噪,都会较明显地模糊边缘,对于高频细节的保护效果并不明显。

双边滤波器比高斯滤波多了一个高斯方差sigma-d,它是基于空间分布的高斯滤波函数,所以在边缘附近,离的较远的像素不会太多影响到边缘上的像素值,这样就保证了边缘附近像素值的保存。但是由于保存了过多的高频信息,对于彩色图像里的高频噪声,双边滤波器不能够干净的滤掉,只能够对于低频信息进行较好的滤波。

1.API

void bilateralFilter(
    InputArray src, 
    OutputArray dst, 
    int d,
    double sigmaColor, 
    double sigmaSpace,
    int borderType = BORDER_DEFAULT  
);

函数参数含义如下:

(1)InputArray类型的src,输入图像。该函数对通道是独立处理的,且可以处理任意通道数的图片,但需要注意,待处理的图片深度应该为CV_8U, CV_16U, CV_16S, CV_32F 以及 CV_64F之一。

(2)OutputArray类型的dst,即目标图像,与输入图像有相同的尺寸和类型。

(3)int类型的d,滤波过程中使用的每个像素邻域的直径。如果是非正的,则从sigmaSpace计算。

(4)double类型的sigmaColor ,颜色空间中过滤器sigma。参数值越大,意味着像素邻域(参见sigmaSpace)中的更多颜色将混合在一起,从而产生更大的半等色区域。

(5)double类型的sigmaSpace,在坐标空间中过滤器sigma。参数值越大,意味着更远的像素将相互影响,只要它们的颜色足够接近(参见sigmaColor)。当d大于0时,它指定邻域大小,而不考虑sigmaSpace。否则,d与sigmaSpace成比例。

(6)int类型的borderType,用于在图像外部外推像素的边框模式,具体请参见cv::BorderTypes。

 

2.代码展示

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

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


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


	waitKey(0);
	return 0;
}

3.执行结果

双边滤波后的图像

 

好啦今天的内容就讲到这里啦,希望大家能够多多练习,才能真正学懂啊!

发布了244 篇原创文章 · 获赞 500 · 访问量 50万+

Guess you like

Origin blog.csdn.net/shuiyixin/article/details/104345878