源码:
#include
#include
#include
#include
using namespace cv;
using namespace std;
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_nMedianBlurBlurValue = 10;
int g_nBilateralFiterValue = 10;
static void on_BoxFilter(int, void*);
static void on_MeanBulr(int, void*);
static void on_GaussianBulr(int, void*);
static void on_MedianBlur(int, void*);
static void on_BilateralFiter(int, void*);
int main()
{
g_srcImage = imread("1.jpg",1);
if (!g_srcImage.data)
{
printf("图片载入失败!\n");
return false;
}
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("原图",1);
imshow("原图", g_srcImage);
namedWindow("方框滤波",1);
createTrackbar("内核值:", "方框滤波", &g_nBoxFilterValue, 50, on_BoxFilter);
on_BoxFilter(g_nBoxFilterValue, 0);
imshow("方框滤波", g_dstImage1);
namedWindow("均值滤波",1);
createTrackbar("内核值:", "均值滤波", &g_nMeanBlurValue, 50, on_MeanBulr);
on_MeanBulr(g_nMeanBlurValue, 0);
imshow("均值滤波", g_dstImage2);
namedWindow("高斯滤波",1);
createTrackbar("内核值:", "高斯滤波", &g_nGaussianBlurValue, 50, on_GaussianBulr);
on_GaussianBulr(g_nGaussianBlurValue, 0);
imshow("高斯滤波", g_dstImage3);
namedWindow("中值滤波",1);
createTrackbar("内核值:", "中值滤波", &g_nMedianBlurBlurValue, 50, on_MedianBlur);
on_MedianBlur(g_nMedianBlurBlurValue, 0);
imshow("中值滤波", g_dstImage4);
namedWindow("双边滤波",1);
createTrackbar("内核值:", "双边滤波", &g_nBilateralFiterValue, 50, on_BilateralFiter);
on_BilateralFiter(g_nBilateralFiterValue, 0);
imshow("双边滤波", g_dstImage5);
waitKey(0);
return 0;
}
static void on_BoxFilter(int, void*)
{
boxFilter(g_srcImage, g_dstImage1, -1, Size(g_nBoxFilterValue + 1, g_nBoxFilterValue + 1));
imshow("方框滤波", g_dstImage1);
}
static void on_MeanBulr(int, void*)
{
blur(g_srcImage, g_dstImage2, Size(g_nMeanBlurValue + 1, g_nMeanBlurValue + 1));
imshow("均值滤波", g_dstImage2);
}
static void on_GaussianBulr(int, void*)
{
GaussianBlur(g_srcImage, g_dstImage3, Size(g_nGaussianBlurValue * 2 + 1, g_nGaussianBlurValue * 2 + 1),0,0);
imshow("高斯滤波", g_dstImage3);
}
static void on_MedianBlur(int, void*)
{
medianBlur(g_srcImage, g_dstImage4, g_nMedianBlurBlurValue * 2 + 1);
imshow("中值滤波", g_dstImage4);
}
static void on_BilateralFiter(int, void*)
{
bilateralFilter(g_srcImage, g_dstImage5, g_nBilateralFiterValue, g_nBilateralFiterValue * 2, g_nBilateralFiterValue / 2);
imshow("双边滤波", g_dstImage5);
}
效果图:
观察效果图可以发现,方框滤波和均值滤波效果很相似,中值滤波对原图颠覆很大,而双边滤波和原图差别不大。