5种滤波方法分别为:
3种线性滤波:方框滤波、均值滤波、高斯滤波
2种非线性滤波:中值滤波、双边滤波
简单来说,方框滤波就是邻域内取个平均值,均值滤波就是方框滤波再归一化一下,高斯滤波就是用正态分布去和图像做卷积。
中值滤波是用像素点邻域灰度值的中值来代替这个像素点的灰度值,而双边滤波像素点的灰度值是邻域像素值的加权组合,是一个基于空间分布的高斯滤波函数,可以很好的保留边缘附近的像素值。
从书上抄的代码:
#include
#include
#include
#include
#include
using namespace cv;
Mat g_srcImage,g_dstImage1,g_dstImage2,g_dstImage3,g_dstImage4,g_dstImage5;
int g_nBoxFilterValue=6;
int g_nMeanBlurValue=10;
int g_nGaussianBlurValue=6;
int g_nMedianBlurValue=10;
int g_nBilateralFilterValue=10;
static void on_BoxFilter(int,void *);
static void on_MeanBlur(int,void *);
static void on_GaussianBlur(int,void *);
static void on_MedianBlur(int,void *);
static void on_BilateralFilter(int,void *);
int main(){
//====================载入原图====================
g_srcImage=imread("/Users/hxh/Desktop/2.jpg",0);
if (!g_srcImage.data){
printf("no source image!\n");
return false;
}
//==============复制原图到5个Mat类型中==============
g_dstImage1=g_srcImage.clone();
g_dstImage2=g_srcImage.clone();
g_dstImage3=g_srcImage.clone();
g_dstImage4=g_srcImage.clone();
g_dstImage5=g_srcImage.clone();
//===================显示原图=====================
namedWindow("0:source image",WINDOW_AUTOSIZE);
imshow("0:source image",g_srcImage);
//================【<1>方框滤波】==================
namedWindow("1:image after box filter",WINDOW_AUTOSIZE);
createTrackbar("内核值:", "1:image after box filter", &g_nBoxFilterValue, 50,on_BoxFilter);
on_MeanBlur(g_nBoxFilterValue, 0);
//================【<2>均值滤波】==================
namedWindow("2:image after mean blur",WINDOW_AUTOSIZE);
createTrackbar("内核值:", "2:image after mean blur", &g_nMeanBlurValue, 50,on_MeanBlur);
on_MeanBlur(g_nMeanBlurValue,0);
//================【<3>高斯滤波】==================
namedWindow("3:image after gaussian blur",WINDOW_AUTOSIZE);
createTrackbar("内核值:", "3:image after gaussian blur", &g_nGaussianBlurValue, 50,on_GaussianBlur);
on_GaussianBlur(g_nGaussianBlurValue,0);
//================【<4>中值滤波】==================
namedWindow("4:image after median filter",WINDOW_AUTOSIZE);
createTrackbar("参数值:", "4:image after median filter", &g_nMedianBlurValue, 50,on_MedianBlur);
on_MedianBlur(g_nMedianBlurValue,0);
//================【<5>双边滤波】==================
namedWindow("5:image after bilateral filter",WINDOW_AUTOSIZE);
createTrackbar("参数值:","5:image after bilateral filter",&g_nBilateralFilterValue,50,on_BilateralFilter);
on_BilateralFilter(g_nBilateralFilterValue, 0);
//==================输入'q'结束===================
while (char(waitKey(1))!='q'){}
return 0;
}
//==============【on_BoxFilter()函数】================
static void on_BoxFilter(int,void *){
boxFilter(g_srcImage, g_dstImage1, -1, Size(g_nBoxFilterValue+1,g_nBoxFilterValue+1));
imshow("1:image after box filter",g_dstImage1);
}
//==============【on_MeanBlur()函数】=================
static void on_MeanBlur(int,void *){
blur(g_srcImage,g_dstImage2,Size(g_nMeanBlurValue+1,g_nMeanBlurValue+1),Point(-1,-1));
imshow("2:image after mean blur",g_dstImage2);
}
//============【on_GaussianBlur()函数】===============
static void on_GaussianBlur(int,void *){
GaussianBlur(g_srcImage, g_dstImage3, Size(g_nGaussianBlurValue*2+1,g_nGaussianBlurValue*2+1), 0, 0);
imshow("3:image after gaussian blur",g_dstImage3);
}
//==============【on_MedianBlur()函数】===============
static void on_MedianBlur(int,void *){
medianBlur(g_srcImage, g_dstImage4, g_nMedianBlurValue*2+1);
imshow("4:image after median filter",g_dstImage4);
}
//============【on_BilateralFilter()函数】============
static void on_BilateralFilter(int,void *){
bilateralFilter(g_srcImage, g_dstImage5, g_nBilateralFilterValue, g_nBilateralFilterValue*2, g_nBilateralFilterValue/2);
imshow("5:image after bilateral filter",g_dstImage5);
}