<图像处理> 空间滤波基础二

空间滤波基础二:锐化

锐化的作用的突出灰度中的过渡。图像锐化通过空间微分 来实现,微分将增强边缘和其他不连续(噪声),不强化灰度变化缓慢的区域。图像锐化也叫做高通滤波,通过高频,抑制低频。

1、二阶导数锐化

f ( x ) f(x) f(x)的二阶导数定义为差分
∂ 2 f ∂ x 2 = f ( x + 1 ) + f ( x − 1 ) − 2 f ( x ) \frac{\partial^2f}{\partial x^2}=f(x+1)+f(x-1)-2f(x) ∂x2∂2f=f(x+1)+f(x−1)−2f(x)

拉普拉斯滤波器

最简单的各向同性二阶导数核是拉普拉斯核,对于图像 f ( x , y ) f(x,y) f(x,y),其定义如下:
∇ 2 f = ∂ 2 f ∂ x 2 + ∂ 2 f ∂ y 2 \nabla^2 f=\frac{\partial^2f}{\partial x^2}+\frac{\partial^2f}{\partial y^2} ∇2f=∂x2∂2f+∂y2∂2f

由于任意阶的导数都是线性算子,所以拉普拉斯也是线性 算子。如下是对上式的 x 、 y x、y x、y两个方向的离散化:
x x x方向:
∂ 2 f ∂ x 2 = f ( x + 1 , y ) + f ( x − 1 , y ) − 2 f ( x , y ) \frac{\partial^2f}{\partial x^2}=f(x+1,y)+f(x-1,y)-2f(x,y) ∂x2∂2f=f(x+1,y)+f(x−1,y)−2f(x,y)
y y y方向:
∂ 2 f ∂ y 2 = f ( x , y + 1 ) + f ( x , y − 1 ) − 2 f ( x , y ) \frac{\partial^2f}{\partial y^2}=f(x,y+1)+f(x,y-1)-2f(x,y) ∂y2∂2f=f(x,y+1)+f(x,y−1)−2f(x,y)

由上面三个公式可得,
∇ 2 f = f ( x + 1 , y ) + f ( x − 1 , y ) + f ( x , y + 1 ) + f ( x , y − 1 ) − 4 f ( x , y ) \nabla^2 f=f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1)-4f(x,y) ∇2f=f(x+1,y)+f(x−1,y)+f(x,y+1)+f(x,y−1)−4f(x,y)

由上式可得其卷积核如下:
[ 0 1 0 1 − 4 1 0 1 0 ] \begin{bmatrix} 0 & 1&0\\ 1 & -4&1 \\0 &1&0\\ \end{bmatrix} 0101−41010

更多变体:

(1) [ 1 1 1 1 − 8 1 1 1 1 ] \begin{bmatrix} 1 & 1&1\\ 1 & -8&1 \\1 &1&1\\ \end{bmatrix} 1111−81111 ,(2) [ 0 − 1 0 − 1 4 − 1 0 − 1 0 ] \begin{bmatrix} 0 & -1&0\\ -1 & 4&-1 \\0 &-1&0\\ \end{bmatrix} 0−10−14−10−10 ,(3) [ − 1 − 1 − 1 − 1 8 − 1 − 1 − 1 − 1 ] \begin{bmatrix} -1 & -1&-1\\ -1 & 8&-1 \\-1 &-1&-1\\ \end{bmatrix} −1−1−1−18−1−1−1−1

综上,卷积核(1)增加了±45°方向,(2)&(3)仅为符号的差异,产生的效果相同,但是需要注意当拉普拉斯滤波过后的图像与原图进行加减操作时的符号差异

OpenCV函数:

cpp 复制代码
void cv::Laplacian(InputArray src, OutputArray dst, int ddepth, int ksize = 1, double scale = 1, double delta = 0, int borderType = BORDER_DEFAULT)	

Parameters
src				Source image.
dst				Destination image of the same size and the same number of channels as src .
ddepth			Desired depth of the destination image, see combinations.
ksize			Aperture size used to compute the second-derivative filters. See getDerivKernels for details. The size must be positive and odd.
scale			Optional scale factor for the computed Laplacian values. By default, no scaling is applied. See getDerivKernels for details.
delta			Optional delta value that is added to the results prior to storing them in dst .
borderType		Pixel extrapolation method, see BorderTypes. BORDER_WRAP is not supported.

2、 一阶导数锐化

f ( x ) f(x) f(x)的一阶导数定义为差分
∂ f ∂ x = f ( x + 1 ) − f ( x ) \frac{\partial f}{\partial x}=f(x+1)-f(x) ∂x∂f=f(x+1)−f(x)

在图像处理中,一阶导数是用梯度幅度实现的。图像f在(x,y)处的梯度定位为二维列向量
∇ f = g r a d ( f ) = [ g x g y ] = [ ∂ f ∂ x ∂ f ∂ y ] \nabla f=grad(f)=\begin{bmatrix}g_x\\ g_y \end{bmatrix}=\begin{bmatrix} \frac {\partial f}{\partial x} \\ \\\frac {\partial f} {\partial y} \end{bmatrix} ∇f=grad(f)=[gxgy]= ∂x∂f∂y∂f

向量 ∇ f \nabla f ∇f的幅度表示为 M ( x , y ) M(x,y) M(x,y),其中
M ( x , y ) = ∥ f ∥ = m a g ( ∇ f ) = g x 2 + g y 2 M(x,y)=\parallel f \parallel=mag(\nabla f)=\sqrt{g_x^2+g_y^2} M(x,y)=∥f∥=mag(∇f)=gx2+gy2

在某些实现中,使用绝对值来近似平方运算和平方根的运算,
M ( x , y ) ≈ ∣ g x ∣ + ∣ g y ∣ M(x,y)\approx |g_x|+|g_y| M(x,y)≈∣gx∣+∣gy∣
M ( x , y ) M(x,y) M(x,y)图像与原图尺寸大小相同,对应原图所有像素位置上的变化,该图像又叫做梯度图像

离散的一阶导数卷积核有如下:

1、Roberts滤波器

Roberts算子的原理是通过计算对角方向相邻两个像素之差 来计算图像的梯度大小和方向。梯度大小表示边缘的强度,梯度方向与边缘的走向垂直。

Roberts算法在边缘定位方面较为准确,但对噪声比较敏感,无法有效抑制噪声的影响。因此,它常被用于检测边缘明显、亮度差异较大的低噪声图像。

计算方式:

计算45°方向: g x ( x , y ) = f ( x + 1 , y ) − f ( x , y + 1 ) g_x(x,y)=f(x+1,y)-f(x,y+1) gx(x,y)=f(x+1,y)−f(x,y+1),滤波核如下:
M x = [ 0 1 − 1 0 ] M_x=\begin{bmatrix} 0 & 1\\ -1 & 0 \\ \end{bmatrix} Mx=[0−110]

计算135°方向, g y ( x , y ) = f ( x , y ) − f ( x + 1 , y + 1 ) g_y(x,y)=f(x,y)-f(x+1,y+1) gy(x,y)=f(x,y)−f(x+1,y+1),滤波核如下:
M y = [ 1 0 0 − 1 ] M_y=\begin{bmatrix} 1 & 0\\ 0 & -1 \\ \end{bmatrix} My=[100−1]

2、Prewitt滤波器

Prewitt算子利用像素点上下左右邻点的灰度差,在边缘处达到极值来检测边缘,对噪声具有平滑的作用。Prewitt算子同样也是一种基于局部差分计算的算法,由两个 3 ∗ 3 3*3 3∗3的模板组成,一个模板用于计算水平方向的梯度,另一个用于计算垂直方向的梯度。与Roberts算子相比,Prewitt算子的检测效果更加准确,仅稍微增加一些计算量就可以抑制噪音的影响。但是相较于Sobel算子和Laplacian算子,Prewitt算子的边缘检测效果较差。
M x = [ − 1 0 1 − 1 0 1 − 1 0 1 ] M_x=\begin{bmatrix} -1 & 0&1\\ -1 & 0&1 \\-1&0&1\\ \end{bmatrix} Mx= −1−1−1000111
M y = [ 1 1 1 0 0 0 − 1 − 1 1 ] M_y=\begin{bmatrix} 1 & 1&1\\ 0 & 0&0 \\-1&-1&1\\ \end{bmatrix} My= 10−110−1101

3、Sobel滤波器

Sobel算子可以在边缘检测时可以提供较为精确的边缘方向信息,对于噪声也具有一定的平滑作用。Sobel算子可以通过像素点上下、左右邻点灰度加权差 ,在边缘处达到极值这一现象检测边缘,是高斯平滑和微分求导的联合运算,抗噪声能力强。考虑了距离对权值的影响,距离越远的像素的影响越小。可以通过快速卷积实现,简单有效,应用广泛。
M x = [ − 1 0 1 − 2 0 2 − 1 0 1 ] M_x=\begin{bmatrix} -1 & 0&1\\ -2 & 0&2 \\-1&0&1\\ \end{bmatrix} Mx= −1−2−1000121
M y = [ 1 2 1 0 0 0 − 1 − 2 1 ] M_y=\begin{bmatrix} 1 & 2&1\\ 0 & 0&0 \\-1 &-2&1\\ \end{bmatrix} My= 10−120−2101

cpp 复制代码
void cv::Sobel(InputArray src, OutputArray dst, int ddepth, int dx, int dy, int ksize = 3, double scale = 1, double delta = 0, int borderType = BORDER_DEFAULT)	

Parameters
src				input image.
dst				output image of the same size and the same number of channels as src .
ddepth			output image depth, see combinations; in the case of 8-bit input images it will result in truncated derivatives.
dx				order of the derivative x.
dy				order of the derivative y.
ksize			size of the extended Sobel kernel; it must be 1, 3, 5, or 7.
scale			optional scale factor for the computed derivative values; by default, no scaling is applied (see getDerivKernels for details).
delta			optional delta value that is added to the results prior to storing them in dst.
borderType		pixel extrapolation method, see BorderTypes. BORDER_WRAP is not supported.

4、Scharr滤波器

Scharr算子是对Sobel算法的改进和增强。Scharr算子和Sobel算子在边缘检测原理和使用方式上基本相同。Scharr算子使用 3 ∗ 3 3*3 3∗3的滤波器,通过增大像素值间的差异来检测图像的边缘。它在X方向和Y方向上都有对应的边缘检测算子。

相比于Sobel算子,Scharr算子在权重系数上有所调整,使得其在图像梯度的计算中更为敏感。具体来说,Scharr算子使用的权重系数相对Sobel算子更大,以增强梯度的响应。这使得Scharr算子在对边缘进行检测时具有更好的精度和效果。

M x = [ − 3 0 3 − 10 0 10 − 3 0 3 ] M_x=\begin{bmatrix} -3 & 0&3\\ -10 & 0&10 \\-3&0&3\\ \end{bmatrix} Mx= −3−10−30003103
M y = [ 3 10 3 0 0 0 − 3 − 10 3 ] M_y=\begin{bmatrix} 3 & 10&3\\ 0 & 0&0 \\-3 &-10&3\\ \end{bmatrix} My= 30−3100−10303

cpp 复制代码
void cv::Scharr	(InputArray src, OutputArray dst, int ddepth, int dx, int dy, double scale = 1, double delta = 0, int borderType = BORDER_DEFAULT)	

Parameters
src				input image.
dst				output image of the same size and the same number of channels as src.
ddepth			output image depth, see combinations
dx				order of the derivative x.
dy				order of the derivative y.
scale			optional scale factor for the computed derivative values; by default, no scaling is applied (see getDerivKernels for details).
delta			optional delta value that is added to the results prior to storing them in dst.
borderType		pixel extrapolation method, see BorderTypes. BORDER_WRAP is not supported.
相关推荐
xiandong201 天前
240929-CGAN条件生成对抗网络
图像处理·人工智能·深度学习·神经网络·生成对抗网络·计算机视觉
x-cmd2 天前
[241005] 14 款最佳免费开源图像处理库 | PostgreSQL 17 正式发布
数据库·图像处理·sql·安全·postgresql·开源·json
白拾2 天前
使用Scikit-image进行图像处理入门
图像处理·人工智能
xiandong202 天前
240925-GAN生成对抗网络
图像处理·人工智能·深度学习·神经网络·生成对抗网络·php·gan
psdadd2 天前
Light Image Resizer v7.0.7 解锁版下载及安装方法(图片大小调整工具)
图像处理·贴图
杳戢2 天前
技术美术百人计划 | 《5.1.3 PBR-基于物理的灯光》笔记
图像处理·人工智能·笔记·算法·计算机视觉·技术美术
qq_15321452643 天前
【2022工业3D异常检测文献】AST: 基于归一化流的双射性产生不对称学生-教师异常检测方法
图像处理·深度学习·神经网络·机器学习·计算机视觉·3d·视觉检测
qq_15321452643 天前
【2023工业3D异常检测文献】CPMF: 基于手工制作PCD描述符和深度学习IAD结合的AD方法
图像处理·深度学习·神经网络·机器学习·计算机视觉·3d·视觉检测
sensor_WU3 天前
【图像处理】多幅不同焦距的同一个物体的平面图象,合成一幅具有立体效果的单幅图像原理(一)
图像处理·人工智能·计算机视觉
Supreme_Sir4 天前
Java 图片合成
java·图像处理·文本·二维码·图片合成·jai