【卷积滤波】
邻域算子值利用给定像素周围像素的值决定此像素的最终输出。如图左边图像与中间图像卷积禅城右边图像。目标图像中绿色的像素由原图像中蓝色标记的像素计算得到。
通用线性邻域滤波是一种常用的邻域算子,输入像素加权得到输出像素:
其中权重核 为“滤波系数”。上面的式子可以简记为:
【方框滤波】
最简单的线性滤波是移动平均或方框滤波,用
窗口中的像素值平均后输出,核函数为:
![](https://img-my.csdn.net/uploads/201207/25/1343205025_1196.png)
![](https://img-my.csdn.net/uploads/201207/25/1343205109_7080.png)
其实等价于图像与全部元素值为1的核函数进行卷积再进行尺度缩放。
代码
OpenCV中的 blur函数是进行标准方框滤波:
- void cv::blur( InputArray src, OutputArray dst,
- Size ksize, Point anchor, int borderType )
- {
- boxFilter( src, dst, -1, ksize, anchor, true, borderType );
- }
- cv::Ptr<cv::FilterEngine> cv::createBoxFilter( int srcType, int dstType, Size ksize,
- Point anchor, bool normalize, int borderType )
- {
- int sdepth = CV_MAT_DEPTH(srcType);
- int cn = CV_MAT_CN(srcType), sumType = CV_64F;
- if( sdepth <= CV_32S && (!normalize ||
- ksize.width*ksize.height <= (sdepth == CV_8U ? (1<<23) :
- sdepth == CV_16U ? (1 << 15) : (1 << 16))) )
- sumType = CV_32S;
- sumType = CV_MAKETYPE( sumType, cn );
- Ptr<BaseRowFilter> rowFilter = getRowSumFilter(srcType, sumType, ksize.width, anchor.x );
- Ptr<BaseColumnFilter> columnFilter = getColumnSumFilter(sumType,
- dstType, ksize.height, anchor.y, normalize ? 1./(ksize.width*ksize.height) : 1);
- return Ptr<FilterEngine>(new FilterEngine(Ptr<BaseFilter>(0), rowFilter, columnFilter,
- srcType, dstType, sumType, borderType ));
- }
![](https://img-my.csdn.net/uploads/201207/26/1343265108_1497.png)
其中,
![](https://img-my.csdn.net/uploads/201207/26/1343265218_9194.png)
- blur( src, dst, Size( 1, 1 ), Point(-1,-1));
- blur( src, dst, Size( 4, 4 ), Point(-1,-1));
- blur( src, dst, Size( 8, 8 ), Point(-1,-1));
- blur( src, dst, Size( 16, 16 ), Point(-1,-1));
【高斯滤波】
高斯滤波器是一类根据高斯函数的形状来选择权值的线性平滑滤波器。它对去除服从正态分布的噪声很有效。
常用的零均值离散高斯滤波器函数:
常用的零均值离散高斯滤波器函数: