文章目录
【注】:本文用于总结图像滤波,参考书<<OPENCV4快速入门>>配套资源:https://github.com/fengzhenHIT/learnOpenCV4
图像卷积
【卷积步骤】
1 (如果卷积模板/卷积核不是中心对称的)将卷积模板旋转180°
2 将卷积模板中心放在原始图像中需要计算卷积的像素上,卷积模板其余部分对应在原始图像相应位置
3 用卷积模板的系数乘以图像中对应位置的像素数值,并对所有结果求和
4 将计算结果存放在原始图像和卷积模板中心对应点的像素处
5 将卷积模板在图像中从左到右,从上到下移动,结果如下
filter2D()函数原型
CV_EXPORTS_W void filter2D( InputArray src, OutputArray dst, int ddepth,
InputArray kernel, Point anchor = Point(-1,-1),
double delta = 0, int borderType = BORDER_DEFAULT );
@param src Source image.
输入图像
@param dst Destination image of the same size and the same number of channels as src .
输出图像(钰输入图像具有相同的尺寸和通道数)
@param ddepth Destination image depth, see @ref filter_depths "combinations"
输出图像的数据类型(深度)
-1: 输出图像的数据类型自动选择
@param kernel Coefficients for filtering .
卷积核,CV_32FC1类型矩阵
@param anchor Anchor position within the kernel. The default value \f$(-1,-1)\f$ means that the anchor
is at the kernel center.
内核的基准点,默认值(-1,-1)代表内
核基准点位于kernel中心位置
@param delta Value added to the filtered results before storing them.
偏值,计算结果加上偏值
@param borderType Pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
像素外推法选择标志
【示例】
Mat result;
filter2D(img, result, CV_32F, kernel, Point(-1, -1), 2, BORDER_CONSTANT);
结果如下
均值滤波
【原理】将滤波器内所有的像素值都看作中心像素的测量,将滤波器内所有的像素值的平均值作为滤波器中心处图像像素值
【优点】在像素值变换趋势一致的情况下,可以将受到噪声影响而突然变化的像素值修正为周围邻近像素值的平均值,去除噪声影响
【缺陷】会缩小像素值之间的差距,使得细节信息变得更加模糊,滤波器范围越大,变模糊的效果越明显
blur()函数原型
CV_EXPORTS_W void blur( InputArray src, OutputArray dst,
Size ksize, Point anchor = Point(-1,-1),
int borderType = BORDER_DEFAULT );
@param src input image.
@param dst output image of the same size and the same number of channels as src.
待滤波图像
@param ddepth desired depth of the destination image, see @ref filter_depths "combinations"
滤波后图像
@param kernel convolution kernel (or rather a correlation kernel), a single-channel floating point
matrix; if you want to apply different kernels to different channels, split the image into
separate color planes using split and process them individually.
卷积核尺寸
@param anchor anchor of the kernel that indicates the relative position of a filtered point within
the kernel; the anchor should lie within the kernel; default value (-1,-1) means that the anchor
is at the kernel center.
内核基准点
@param delta optional value added to the filtered pixels before storing them in dst.
@param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
像素外推法选择标志
【示例】
Mat result_3; //存放不含噪声滤波结果,后面数字代表滤波器尺寸
blur(equalLena, result_3, Size(3, 3));//调用均值滤波函数blur()进行滤波
【效果】
方框滤波
【原理】方框滤波是均值滤波一种形式,但是方框滤波可以选择不进行归一化,将所有像素和作为滤波结果
boxFilter()函数原型
CV_EXPORTS_W void boxFilter( InputArray src, OutputArray dst, int ddepth,
Size ksize, Point anchor = Point(-1,-1),
bool normalize = true,
int borderType = BORDER_DEFAULT );
@param src input image
@param dst output image of the same size and type as _src
@param ddepth the output image depth (-1 to use src.depth())
@param ksize kernel size
@param anchor kernel anchor point. The default value of Point(-1, -1) denotes that the anchor is at the kernel
center.
@param normalize flag, specifying whether the kernel is to be normalized by it's area or not.
是否将卷积核进行归一化,默认归一化
@param borderType border mode used to extrapolate pixels outside of the image, see #BorderTypes. #BORDER_WRAP is not supported.
sqrBoxFilter()函数原型
CV_EXPORTS_W void sqrBoxFilter( InputArray src, OutputArray dst, int ddepth,
Size ksize, Point anchor = Point(-1, -1),
bool normalize = true,
int borderType = BORDER_DEFAULT );
@param src input image; it 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.
@param dst output image of the same size and type as src.
@param ksize blurring kernel size.
@param anchor anchor point; default value Point(-1,-1) means that the anchor is at the kernel
center.
@param borderType border mode used to extrapolate pixels outside of the image, see #BorderTypes. #BORDER_WRAP is not supported.
【示例】
//方框滤波boxFilter()和sqrBoxFilter()
boxFilter(equalLena, resultNorm, -1, Size(3, 3), Point(-1, -1), true); //进行归一化
boxFilter(equalLena, result, -1, Size(3, 3), Point(-1, -1), false); //不进行归一化
sqrBoxFilter(data, dataSqrNorm, -1, Size(3, 3), Point(-1, -1),
true, BORDER_CONSTANT); //进行归一化
【效果】
高斯滤波
【原理】高斯滤波器考虑了像素离滤波器中心距离的影响,以滤波器中心位置为高斯分布的均值,根据高斯分布公式和每个像素离中心位置的距离计算出滤波器内每个位置的数值,下图:高斯滤波器的空间结构
GaussianBlur()函数原型
CV_EXPORTS_W void GaussianBlur( InputArray src, OutputArray dst, Size ksize,
double sigmaX, double sigmaY = 0,
int borderType = BORDER_DEFAULT );
work inplace.
@param
ksize:滤波器的尺寸,可以不为正方形,但是必须是正奇数,如果尺寸是0,那么由标准偏差计算尺寸
sigmaX:X方向的高斯滤波器的标准偏差
sigmaY:Y方向的高斯滤波器的标准偏差,如果输入0,那么设置为sigmaX,如果两个都为0,那么根据ksize计算
【示例】
//调用均值滤波函数blur()进行滤波
GaussianBlur(equalLena, result_5, Size(5, 5), 10, 20);
GaussianBlur(equalLena, result_9, Size(9, 9), 10, 20);
【效果】
getGaussianKernel()函数原型
该函数用来生成指定尺寸的高斯滤波器,生成的是
ksize*1的Mat类矩阵
CV_EXPORTS_W Mat getGaussianKernel( int ksize, double sigma, int ktype = CV_64F );
@param
ksize:高斯滤波器尺寸
sigma: 高斯滤波标准差
ktype:滤波器数据类型(CV_32F/CV_64F)
【示例】
Mat gaussx = getGaussianKernel(3,1);
可分离滤波
【原理】:可根据实际需求调整滤波模板,可输入两个方向滤波器
sepFilter2D()函数原型
cv::sepFilter2D(InputArray src, OutputArray dst, int ddepth, InputArray kernelX, InputArray kernelY, Point anchor,
double delta, int borderType)
@param
kernelX , kernelY:两个方向的滤波器
【示例】
Mat a = (Mat_<float>(3, 1) << -1, 3, -1);
Mat b = a.reshape(1, 1);
sepFilter2D(data, dataXY_sep, -1, b, b, Point(-1, -1), 0, BORDER_CONSTANT);
【效果】