一、方框滤波
1 void boxFilter( InputArray src, OutputArray dst, int ddepth, 2 Size ksize, Point anchor = Point(-1,-1), 3 bool normalize = true, 4 int borderType = BORDER_DEFAULT );
二、均值滤波
1 void blur( InputArray src, OutputArray dst, 2 Size ksize, Point anchor = Point(-1,-1), 3 int borderType = BORDER_DEFAULT );
三、高斯滤波
1 void GaussianBlur( InputArray src, OutputArray dst, Size ksize, 2 double sigmaX, double sigmaY = 0, 3 int borderType = BORDER_DEFAULT );
1 #include<opencv2/opencv.hpp> 2 #include<iostream> 3 4 using namespace std; 5 using namespace cv; 6 7 Mat g_srcImage, g_dstImage1, g_dstImage2, g_dstImage3; 8 int g_nBoxFilterValue = 3; //方框滤波参数值 9 int g_nMeanBlurValue = 3; //均值滤波参数值 10 int g_nGaussianBlurValue = 3; //高斯滤波参数值 11 12 static void on_BoxFilter(int, void *); //方框滤波 13 static void on_MeanBlur(int, void *); //均值滤波 14 static void on_GaussianBlur(int, void *); //高斯滤波 15 16 int main() { 17 //改变console字体颜色 18 system("color 5E"); 19 20 //载入原图 21 g_srcImage = imread("C:\Users\Administrator\Pictures\Camera Roll\05.jpg"); 22 if (!g_srcImage.data) { 23 printf("读取srcIamge错误! "); 24 return false; 25 } 26 27 //复制原图到三个Mat类型中 28 g_dstImage1 = g_srcImage.clone(); 29 g_dstImage2 = g_srcImage.clone(); 30 g_dstImage3 = g_srcImage.clone(); 31 32 //显示原图 33 namedWindow("【原图窗口】", 1); 34 imshow("【原图窗口】", g_srcImage); 35 36 //=====【方框滤波】====== 37 //创建窗口 38 namedWindow("【方框滤波】",1); 39 //创建轨迹条 40 createTrackbar("内核值:", "【方框滤波】", &g_nBoxFilterValue, 40, on_BoxFilter); 41 on_BoxFilter(g_nBoxFilterValue, 0); 42 43 44 //=====【均值滤波】====== 45 //创建窗口 46 namedWindow("【均值滤波】", 1); 47 //创建轨迹条 48 createTrackbar("内核值:", "【均值滤波】", &g_nMeanBlurValue, 40, on_MeanBlur); 49 on_MeanBlur(g_nMeanBlurValue, 0); 50 51 52 //=====【高斯滤波】====== 53 //创建窗口 54 namedWindow("【高斯滤波】", 1); 55 //创建轨迹条 56 createTrackbar("内核值:", "【高斯滤波】", &g_nGaussianBlurValue, 40, on_GaussianBlur); 57 on_GaussianBlur(g_nGaussianBlurValue, 0); 58 59 while (char(waitKey(1) != 'q')) {} 60 61 return 0; 62 } 63 64 static void on_BoxFilter(int, void *) { //方框滤波 65 //方框滤波操作 66 boxFilter(g_srcImage, g_dstImage1, -1, Size(g_nBoxFilterValue + 1, g_nBoxFilterValue + 1)); 67 //显示窗口 68 imshow("【方框滤波】", g_dstImage1); 69 } 70 static void on_MeanBlur(int, void *) { //均值滤波 71 //均值滤波操作 72 blur(g_srcImage, g_dstImage2, Size(g_nMeanBlurValue + 1, g_nMeanBlurValue + 1), Point(-1, -1)); 73 //显示窗口 74 imshow("【均值滤波】", g_dstImage2); 75 } 76 static void on_GaussianBlur(int, void *) { //高斯滤波 77 //高斯滤波操作 78 GaussianBlur(g_srcImage, g_dstImage3, Size(g_nGaussianBlurValue * 2 + 1, g_nGaussianBlurValue * 2 + 1), 0, 0); 79 //显示窗口 80 imshow("【高斯滤波】", g_dstImage3); 81 }