1 前备知识
中值滤波本质上是统计排序滤波器的一种,中值滤波对图像特定噪声类型(椒盐噪声)会取得比较好的去噪效果,也是常见的图像去噪声与增强的方法之一,相对的还有最小值滤波,最大值滤波。中值滤波也是窗口在图像上移动,其覆盖的对应ROI区域下,所有像素值排序,取中值作为中心像素点的输出值,与均值滤波相对比,该滤波方法不用做卷积(点乘相加)。具体操作方法如下图:


2 所用到的主要OpenCv API
/** @brief Blurs an image using the median filter.
The function smoothes an image using the median filter with the \f$\texttt{ksize} \times
\texttt{ksize}\f$ aperture. Each channel of a multi-channel image is processed independently.
In-place operation is supported.
@note The median filter uses #BORDER_REPLICATE internally to cope with border pixels, see #BorderTypes
@param src input 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.
@param dst destination array of the same size and type as src.
@param ksize aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 ...
@sa bilateralFilter, blur, boxFilter, GaussianBlur
*/
CV_EXPORTS_W void medianBlur( InputArray src, OutputArray dst, int ksize );
3 程序代码
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int artc, char** argv) {
Mat src = imread("images/sp_noise.png");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
Mat dst;
medianBlur(src, dst, 5);//5为滤波使用的kernel size,必须为奇数
imshow("medianblur ksize=5", dst);
waitKey(0);
return 0;
}
4 运行结果


5 扩展及注意事项
none
6*目前只做大概了解,知道有这一算法,后续具体使用再做具体分析