图像模糊是图像处理中最简单和最基本的操作之一。图像模糊的目的是去除图像噪声,其原理是卷积计算。卷积算子是线性操作,又叫线性滤波。 g ( i , j ) = ∑ k , l f ( i + k , j + l ) h ( k , l ) g(i,j) = \sum_{k,l}f(i+k,j+l)h(k,l) g(i,j)=k,l∑f(i+k,j+l)h(k,l) 不同类型的模糊处理背后其实是对应不同的小方格,也就是俗称的滤波器或者卷积核。 1)均值滤波 均值滤波即取平均值。不能很好的保留图像细节,因为全用均值代替了。
// // 摘要: // Smoothes image using normalized box filter 均一化滤波器平滑图像 // // 参数: // src: // The source image // // dst: // The destination image; will have the same size and the same type as src // // ksize: // The smoothing kernel size 滤波核大小 // // anchor: // The anchor point. The default value Point(-1,-1) means that the anchor is at // the kernel center // // borderType: // The border mode used to extrapolate pixels outside of the image public static void Blur(InputArray src, OutputArray dst, Size ksize, Point? anchor = null, BorderTypes borderType = BorderTypes.Reflect101);2)高斯滤波 高斯滤波,是一种线性平滑滤波,适用于消除高斯噪声,广泛应用于图像处理的减噪过程。通俗的讲,滤波高斯就是对整幅图像进行加权平均的过程,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到。 G ( x , y ) = 1 2 π σ 2 e − ( x 2 + y 2 ) / 2 σ 2 G(x,y)=\frac{1}{2\pi\sigma ^{2}} e^{-(x^2+y^2)/2\sigma ^2} G(x,y)=2πσ21e−(x2+y2)/2σ2
// 摘要: // Blurs an image using a Gaussian filter. 高斯滤波 // // 参数: // src: // input image; the image can have any number of channels, which are processed independently, // but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F. // // dst: // output image of the same size and type as src. // // ksize: // Gaussian kernel size. ksize.width and ksize.height can differ but they both must // be positive and odd. Or, they can be zero’s and then they are computed from sigma* // . // // sigmaX: // Gaussian kernel standard deviation in X direction. X方向高斯核标准差。 // // sigmaY: // Gaussian kernel standard deviation in Y direction; if sigmaY is zero, it is set // to be equal to sigmaX, if both sigmas are zeros, they are computed from ksize.width // and ksize.height, respectively (see getGaussianKernel() for details); to fully // control the result regardless of possible future modifications of all this semantics, // it is recommended to specify all of ksize, sigmaX, and sigmaY. // Y方向高斯核标准差。 // borderType: // pixel extrapolation method public static void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY = 0, BorderTypes borderType = BorderTypes.Reflect101);3)中值滤波 中值滤波,中值,中间值,将数据从小到大排序后的中间值 。用 3×3 大小模板进行中值滤波。
// 摘要: // Smoothes image using median filter 中值滤波 // // 参数: // src: // The source 1-, 3- or 4-channel image. When ksize is 3 or 5, the image depth should // be CV_8U , CV_16U or CV_32F. For larger aperture sizes it can only be CV_8U // // dst: // The destination array; will have the same size and the same type as src // // ksize: 核大小必须是奇数 // The aperture linear size. It must be odd and more than 1, i.e. 3, 5, 7 ... public static void MedianBlur(InputArray src, OutputArray dst, int ksize);中值、均值、高斯滤波方法实现
Mat dst = new Mat(s2.Size(),s2.Type()); OpenCvSharp.Size Kernel = new OpenCvSharp.Size(3, 3); Cv2.Blur(s2, dst, Kernel);//均值滤波 Cv2.GaussianBlur(s2,dst,Kernel,11,11);//高斯滤波 Cv2.MedianBlur(s2,dst,3); //均值滤波