图像的卷积计算除了可以完成我们前面介绍的模糊去噪、边缘检测等任务外,还可以实现图像锐化/增强的功能。
图像锐化是一种突出和加强图像中景物的边缘和轮廓的技术。在图像中,边缘可以看作是位于一阶导数较大的像素位置,因此可以通过求图像的一阶导数来加强图像的边缘。同样我们也可以通过求图像的二阶导数来完成图像锐化。一般也通过Laplacian滤波加原图权重像素叠加,关于Laplacian算子可以参考下面这篇博客:
https://blog.csdn.net/PecoHe/article/details/95080569
常用的Laplacian高通滤波算子有:
H 1 = [ − 1 − 1 − 1 − 1 c − 1 − 1 − 1 − 1 ] H_1= \left[ \begin{matrix} -1 & -1 & -1 \\ -1 & c & -1\\ -1 & -1& -1 \end{matrix} \right] H1=⎣⎡−1−1−1−1c−1−1−1−1⎦⎤
H 2 = [ 0 − 1 0 − 1 5 − 1 0 − 1 0 ] H_2= \left[ \begin{matrix} 0 & -1 & 0\\ -1 &5 & -1\\ 0 & -1& 0 \end{matrix} \right] H2=⎣⎡0−10−15−10−10⎦⎤
Mat h1_kernel = (Mat_<char>(3, 3) << -1, -1, -1, -1, 8, -1, -1, -1, -1);
Mat h2_kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
Mat h1_result,h2_result;
filter2D(input, h1_result, CV_32F, h1_kernel);
filter2D(input, h2_result, CV_32F, h2_kernel);
convertScaleAbs(h1_result, h1_result);
convertScaleAbs(h2_result, h2_result);
imshow("h1_result", h1_result);
imshow("h2_result", h2_result);
USM(Unshrpen Mask)算法是对原图像先进行一个高斯滤波,然后用原来的图像像素值减去一个系数乘以高斯滤波后的图像像素值,然后把得到的像素值归到0~255范围内。USM锐化增强算法可以去除一些细小的干扰细节和图像噪声,比一般直接使用卷积锐化算子得到的图像更可靠。
USM锐化公式如下:
原 图 像 − w ∗ 高 斯 滤 波 ( 原 图 像 ) / ( 1 − w ) 原图像-w*高斯滤波(原图像)/(1-w) 原图像−w∗高斯滤波(原图像)/(1−w)
其中w为上面所述的系数,取值范围为0.1~0.9,一般取0.6。
这里介绍一下OpenCV中的addWeighted函数:
void addWeighted(InputArray src1,
double alpha,
InputArray src2,
double beta,
double gamma,
OutputArray dst,
int dtype = -1);
其中src1,src2分别为两个输入图像,alpha是输入图像1的权重,beta是输入图像2的权重,gamma是输入输出图像作和后添加的数值,该值不要太大。
Mat blur,usm;
GaussianBlur(input, blur, Size(0, 0), 25);
addWeighted(input, 1.5, blur, -0.5, 0, usm);
imshow("usm", usm);