高斯滤波
高斯滤波是一种线性平滑滤波,适用于消除高斯噪声,广泛应用于图像处理的减噪过程。 [1] 通俗的讲,高斯滤波就是对整幅图像进行加权平均的过程,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到。高斯滤波的具体操作是:用一个模板(或称卷积、掩模)扫描图像中的每一个像素,用模板确定的邻域内像素的加权平均灰度值去替代模板中心像素点的值。
二维高斯函数:
5*5卷积核:
二维高斯函数公式如下:
二维高斯函数公式如下:
opencv提供高斯滤波函数GaussianBlur():
CV_EXPORTS_WvoidGaussianBlur( InputArraysrc, OutputArraydst, Sizeksize, doublesigmaX, doublesigmaY=0, intborderType=BORDER_DEFAULT );
C++实现:
usingnamespacestd; usingnamespacecv; staticvoidshowImgPara(Mat&img); staticvoidshowImgMinMaxMeansStdev(Mat&img); staticvoidkernelSizeCallBack(int, void*); staticvoidxSigmaCallBack(int, void*); staticvoidySigmaCallBack(int, void*); MatgImgOri, gImgGauss, gImgGaussFilter; intgKernelSize=11; intgXSigma=10; intgYSigma=10; intmain() { /* Original Image */gImgOri=imread("original.png", IMREAD_GRAYSCALE); if (gImgOri.empty()) { cout<<"Cannot find original picture!!"<<endl; returnfalse; } imshow("imgOri", gImgOri); //showImgPara(gImgOri);showImgMinMaxMeansStdev(gImgOri); /* Gauss Noise Picture Create */gImgGauss=imread(GAUSS_NOISE_FILE, IMREAD_GRAYSCALE); if (gImgGauss.empty()) { cout<<"Cannot find noise picture!!"<<endl; returnfalse; } imshow("imgGauss", gImgGauss); //showImgPara(gImgGauss);showImgMinMaxMeansStdev(gImgGauss); /* Gauss filter *///GaussianBlur(gImgGauss, gImgGaussFilter, Size(7, 7), 10, 10);GaussianBlur(gImgGauss, gImgGaussFilter, Size(gKernelSize, gKernelSize), gXSigma, gYSigma); imshow("Gauss Filter Out", gImgGaussFilter); showImgMinMaxMeansStdev(gImgGaussFilter); /* Add Bar */cv::createTrackbar("Kernel Size", "Gauss Filter Out", &gKernelSize, MAX_KERNEL_SIZE, kernelSizeCallBack); cv::createTrackbar("X Sigma", "Gauss Filter Out", &gXSigma, MAX_KERNEL_SIZE, xSigmaCallBack); cv::createTrackbar("Y Sigma", "Gauss Filter Out", &gYSigma, MAX_KERNEL_SIZE, ySigmaCallBack); waitKey(0); returntrue; } staticvoidshowImgPara(Mat&img) { cout<<"sizeof(img) is: "<<sizeof(img) <<", img size is: "<<img.size<<endl; cout<<"rows x cols: ("<<img.rows<<" x "<<img.cols<<")"<<endl; cout<<"dims: "<<img.dims<<endl; cout<<"channels: "<<img.channels() <<endl; cout<<"type: "<<img.type() <<endl; cout<<"depth:"<<img.depth() <<endl; cout<<"elemSize:"<<img.elemSize() <<" (Bytes per element)"<<endl; cout<<"elemSize1:"<<img.elemSize1() <<"(Bytes per channel)"<<endl; cout<<"step[0]: "<<img.step[0] <<" (Bytes per cows only when 2 dims)"<<endl; cout<<"step[1]: "<<img.step[1] <<" (Bytes per element only when 2 dims)"<<endl; cout<<"step1(0): "<<img.step1(0) <<", step1(1): "<<img.step1(1) <<" (step / elemSize1)"<<endl; cout<<"----showImgPara End----"<<endl; } staticvoidshowImgMinMaxMeansStdev(Mat&img) { /* Max and Min value and location */doubleminValue, maxValue; PointminLdx, maxLdx; minMaxLoc(img, &minValue, &maxValue, &minLdx, &maxLdx, Mat()); cout<<"Min: "<<minValue<<"["<<minLdx<<"]"<<endl; cout<<"Max: "<<maxValue<<"["<<maxLdx<<"]"<<endl; /* means and stdev*/Matmeans, stdev; meanStdDev(img, means, stdev); cout<<"Means: "<<means.at<double>(0, 0) <<endl; cout<<"Standard Deviationst: "<<stdev.at<double>(0, 0) <<endl; cout<<"----showImgMinMaxMeansStdev End----\n"<<endl; } staticvoidkernelSizeCallBack(int, void*) { if (gKernelSize%2==0) { gKernelSize=gKernelSize+1; } GaussianBlur(gImgGauss, gImgGaussFilter, Size(gKernelSize, gKernelSize), gXSigma, gYSigma); imshow("Gauss Filter Out", gImgGaussFilter); showImgMinMaxMeansStdev(gImgGaussFilter); } staticvoidxSigmaCallBack(int, void*) { GaussianBlur(gImgGauss, gImgGaussFilter, Size(gKernelSize, gKernelSize), gXSigma, gYSigma); imshow("Gauss Filter Out", gImgGaussFilter); showImgMinMaxMeansStdev(gImgGaussFilter); } staticvoidySigmaCallBack(int, void*) { GaussianBlur(gImgGauss, gImgGaussFilter, Size(gKernelSize, gKernelSize), gXSigma, gYSigma); imshow("Gauss Filter Out", gImgGaussFilter); showImgMinMaxMeansStdev(gImgGaussFilter); }