Function prototype
方框滤波 void boxFilter(InputArray src, OutputArray dst, int ddepth, Size ksize, Point anchor = Point(-1,-1), bool normalize = true, int borderType = BORDER_DEFAULT) 均值滤波 void blur(Input src, OutputArray dst, Size ksize, Point anchor = Point(-1,-1), int borderType = BORDER_DEFAULT) 高斯滤波 void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY = 0, int borderType = BORDER_DEFAULT) 中值滤波 void medianBlur(InputArray src, OutputArray dst, int ksize) 双边滤波 void bilateralFilter(InputArray src, OutputArray dst, int d, double sigmaColor, double sigmaSpace, int borderType = BORDER_DEFAULT)
Specific parameter parsing
InputArray src: input image (source image), Mat type object, image depth should be CV_8U, CV_16U, CV_16S, CV_32F, one CV_64F. For median filtering, if ksize is 3 or 5, the image depth must be CV_8U, CV_16U, one CV_32F, if the pore size is large, only CV_8U. OutputArray dst: an output image (target image), and the size and type of the source image are the same. int ddepth: the depth of an output image, - 1 represents the depth of the original image (i.e. src.depth ()). Size ksize: kernel size, by Size (w, h) to indicate, w and h respectively represent the width and height. Point anchor: the anchor, i.e. smoothed point, if the negative value indicates the center point coordinates is taken nucleus, so the default ( - . 1 - . 1 ) indicates the center of the anchor in the core. BOOL the normalize: identifier, when true indicates that the remaining region of the core is normalized (Normalized) a. int borderType: some boundary pixels outside the image mode is derived. double Sigmax: represents the standard deviation of the Gaussian kernel function of the X-direction. doublesigmaY: represents the standard deviation of the Gaussian kernel in the Y direction. When sigmaY is 0, it will be set Sigmax; if both are 0, and by ksize.with ksize.height calculated, so Gaussian filtering function, ksize of w and h must be positive and odd or 0, can be different. int ksize: linear space the aperture must be an odd number greater than 1. int D: diameter of the filter process each pixel neighborhood, if its value is not positive, the value calculated from sigmaSpace. Double sigmaColor: Sigma filter color space value, the larger the value of this parameter, it indicates that the pixel neighborhood has the wide color are mixed together to produce a large color area equal halves. Double sigmaSpace: marked variance sigma value coordinate space, the coordinates of the spatial filter. The larger its value, the farther the pixel mean affect each other, so that a larger area of the same color acquisition sufficiently similar color. When d when> 0, d developed independent of the size of the neighborhood sigmaSpace. Otherwise, d is proportional to the sigmaSpace.
代码如下:
#include <the iostream> #include <opencv2 \ Core \ core.hpp> #include <opencv2 \ HighGUI \ highgui.hpp> #include <opencv2 \ imgproc \ imgproc.hpp> the using namespace STD; the using namespace CV; Mat g_srcImage; / / global source image // corresponding global block filtering, the output image mean filtering, Gaussian filtering, median filtering, bilateral filtering kernel and the value / parameter values Mat g_dstImgBox, g_dstImgBlur, g_dstImgGaussian, g_dstImgMedian, g_dstImgBilateral; int = g_BoxFilterVal . 5; int g_BlurVal = 12 is; int g_GaussianBlurVal =. 5; int g_MedianBlurVal = 12 is; int g_BilateralFilterVal = 12 is; static void on_BoxFilter (int, void *); static void on_Blur (int, void *); static void on_GaussianBlur (int, void * ); static void on_MedianBlur (int, void *); void on_BilateralFilter static (int, void *); namedWindow ( "filter block []", 1); main int () { // read image to g_srcImage g_srcImage imread = ( "E: \\ \\ vs2015 VS2015Opencv Project \\ \\ \\ 06.jpg Picture"); IF (g_srcImage.data!) { the printf ( " the read image is not ...... \ n ") is present; return to false; } // cloned into the desired artwork image filter 5, are Mat type g_dstImgBox g_srcImage.clone = (); g_dstImgBlur = g_srcImage.clone (); g_dstImgGaussian g_srcImage.clone = (); g_dstImgMedian g_srcImage.clone = (); g_dstImgBilateral g_srcImage.clone = (); // display picture namedWindow ( "[picture]",. 1); imshow ( "[picture ] ", g_srcImage); // block filter ); on_BoxFilter(g_BoxFilterVal, 0); createTrackbar ( "core value", "[block filter]", & g_BoxFilterVal, 30, on_BoxFilter); namedWindow ( "[mean filter]",. 1); createTrackbar ( "core value", "[mean filter]", & g_BlurVal, 30, on_Blur); on_Blur (g_BlurVal, 0); namedWindow ( "[Gaussian filter]",. 1); createTrackbar ( "core value", "[Gaussian filter]", & g_GaussianBlurVal, 30, on_GaussianBlur); on_GaussianBlur (g_GaussianBlurVal, 0 ); namedWindow ( "[median filter]",. 1); createTrackbar ( "core value", "[median filter]", & g_MedianBlurVal, 30, on_MedianBlur); on_MedianBlur (g_MedianBlurVal, 0); namedWindow ( "[bilateral filtering ] ",. 1); createTrackbar (" core value "," bilateral filtering [] ", & g_BilateralFilterVal, 30, on_BilateralFilter ); on_BilateralFilter (g_BilateralFilterVal, 0); COUT << "Press the" q "key, the program exits ...... \ n"; ! the while (char (waitKey (. 1)) = 'Q') {} return 0; } static void on_BoxFilter(int, void *) { boxFilter (g_srcImage, g_dstImgBox, -1, Size (g_BoxFilterVal. 1 +, + g_BoxFilterVal. 1)); imshow ( "filter block []", g_dstImgBox); } static void on_Blur (int, void *) { Blur (g_srcImage, g_dstImgBlur , Size (g_BlurVal. 1 +, + g_BlurVal. 1), Point (-1, -1)); imshow ( "[] mean filter", g_dstImgBlur); } static void on_GaussianBlur (int, void *) { the Gaussian Blur (g_srcImage, g_dstImgGaussian , Size (g_GaussianBlurVal * 2 +. 1, g_GaussianBlurVal * 2 +. 1), 0, 0); imshow ( "[Gaussian filter]", g_dstImgGaussian); } static void on_MedianBlur (int, void *) { medianBlur (g_srcImage, g_dstImgMedian, g_MedianBlurVal * 2 +. 1); imshow ( "[median filter]", g_dstImgMedian); } Static void on_BilateralFilter (int, void *) { bilateralFilter (g_srcImage, g_dstImgBilateral, g_BilateralFilterVal, g_BilateralFilterVal * 2, g_BilateralFilterVal / 2); imshow ( "[] bilateral filtering", g_dstImgBilateral); }
Adding noise
#include <iostream> #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.hpp> #include<ctime> using namespace std; using namespace cv; Mat g_srcImage; // 全局的源图像 // 分别对应全局的方框滤波、均值滤波、高斯滤波、中值滤波、双边滤波的输出图像以及内核值/参数值 Mat g_dstImgBox, g_dstImgBlur, g_dstImgGaussian, g_dstImgMedian, g_dstImgBilateral; int g_BoxFilterVal = 5; int g_BlurVal = 12; int g_GaussianBlurVal = 5; int g_MedianBlurVal = 12; int g_BilateralFilterVal = 12; static void on_BoxFilter(int, void *); static void on_Blur(int, void *); static void on_GaussianBlur(int, void *); static void on_MedianBlur(int, void *); static void on_BilateralFilter(int, void*); //图像椒盐化 void salt(Mat &image, int num) { if (!image.data) return;//防止传入空图 int i, j; srand(time(NULL)); for (int x = 0; x < num; ++x) { i = rand() % image.rows; j = rand() % image.cols; image.at<Vec3b>(i, j)[0] = 255; image.at<Vec3b>(i, j)[1] = 255; image.at<Vec3b>(i, j)[2] = 255; } } int main() { // 读取图像到g_srcImage Mat image = imread("E:\\VS2015Opencv\\vs2015\\project\\picture\\06.jpg"); if (!image.data) { printf("读取的图片不存在…… \n"); return false; } image.copyTo(g_srcImage); salt(g_srcImage, 3000); // 分别克隆原图到5中滤波所需的图像中,均为Mat类型 g_dstImgBox = g_srcImage.clone(); g_dstImgBlur = g_srcImage.clone(); g_dstImgGaussian = g_srcImage.clone(); g_dstImgMedian = g_srcImage.clone(); g_dstImgBilateral = g_srcImage.clone(); // 显示原图 namedWindow("【原图】", 1); imshow("【原图】", g_srcImage); // 方框滤波 namedWindow("【方框滤波】", 1); createTrackbar("内核值", "【方框滤波】", &g_BoxFilterVal, 30, on_BoxFilter); on_BoxFilter(g_BoxFilterVal, 0); namedWindow("【均值滤波】", 1); createTrackbar("内核值", "【均值滤波】", &g_BlurVal, 30, on_Blur); on_Blur(g_BlurVal, 0); namedWindow("【高斯滤波】", 1); createTrackbar("内核值", "【高斯滤波】", &g_GaussianBlurVal, 30, on_GaussianBlur); on_GaussianBlur(g_GaussianBlurVal, 0); namedWindow("【中值滤波】", 1); createTrackbar("内核值", "【中值滤波】", &g_MedianBlurVal, 30, on_MedianBlur); on_MedianBlur(g_MedianBlurVal, 0); namedWindow("【双边滤波】", 1); createTrackbar("内核值", "【双边滤波】", &g_BilateralFilterVal, 30, on_BilateralFilter); on_BilateralFilter(g_BilateralFilterVal, 0); cout << "按下“q”键时,程序退出……\n"; while (char(waitKey(1)) != 'q') {} return 0; } static void on_BoxFilter(int, void *) { boxFilter(g_srcImage, g_dstImgBox, -1, Size(g_BoxFilterVal + 1, g_BoxFilterVal + 1)); imshow("【方框滤波】", g_dstImgBox); } static void on_Blur(int, void *) { blur(g_srcImage, g_dstImgBlur, Size(g_BlurVal + 1, g_BlurVal + 1), Point(-1, -1)); imshow("【均值滤波】", g_dstImgBlur); } static void on_GaussianBlur(int, void *) { GaussianBlur(g_srcImage, g_dstImgGaussian, Size(g_GaussianBlurVal * 2 + 1, g_GaussianBlurVal * 2 + 1), 0, 0); imshow("【高斯滤波】", g_dstImgGaussian); } static void on_MedianBlur(int, void *) { medianBlur(g_srcImage, g_dstImgMedian, g_MedianBlurVal * 2 + 1); imshow("【中值滤波】", g_dstImgMedian); } static void on_BilateralFilter(int, void*) { bilateralFilter(g_srcImage, g_dstImgBilateral, g_BilateralFilterVal, g_BilateralFilterVal * 2, g_BilateralFilterVal / 2); imshow("【双边滤波】", g_dstImgBilateral); }
参考博文:https://blog.csdn.net/i_chaoren/article/details/54562502