锐化滤波器
主要应用
-
图像识别中,分割前的边缘提取
-
原始图像细节模糊,需要突出图像中的细节;
-
弥补扫描对图像的钝化;
-
超声探测成像,分辨率低,边缘模糊,通过锐化来改善;
-
处理曝光不足的图像
-
尖端武器的目标识别、定位
求邻域的灰度均值,可以产生钝化的效果,而均值与积分相似。所以可以用微分(一阶微分、二阶微分)产生相反效果,实现锐化。
边缘和细节都位于灰度突变的地方。图像微分突出了灰度突变的区域(边缘和其它突变如噪声),削弱了灰度变化缓慢的区域。最简单的锐化空间滤波器:基于掩模操作一阶和二阶微分算子。
一维
一维离散函数的微分(差分)算子:
- 一阶(前向)差分:
∂ f ∂ x ≈ Δ f Δ x = f ( x + 1 ) − f ( x ) \frac{\partial f}{\partial x}\approx \frac{\Delta f}{\Delta x} = f(x+1)-f(x) ∂x∂f≈ΔxΔf=f(x+1)−f(x)
- 二阶差分
∂ 2 f ∂ x 2 ≈ f ‘ ( x ) − f ‘ ( x − 1 ) = [ f ( x + 1 ) − f ( x ) ] − [ f ( x ) − f ( x − 1 ) ] = f ( x + 1 ) + f ( x − 1 ) − 2 f ( x ) \frac{\partial^2 f}{\partial x^2}\approx f^{`}(x)-f^`(x-1)\\ =[f(x+1)-f(x)]-[f(x)-f(x-1)]\\ =f(x+1)+f(x-1)-2f(x) ∂x2∂2f≈f‘(x)−f‘(x−1)=[f(x+1)−f(x)]−[f(x)−f(x−1)]=f(x+1)+f(x−1)−2f(x)
基于一阶微分的图像增强— 梯度算子:
∇ f = [ G x G y ] = [ ∂ f ∂ x ∂ f ∂ y ] \nabla f = \begin{bmatrix} G_x \\ G_y \\ \end{bmatrix}= \begin{bmatrix} \frac{\partial f}{\partial x} \\ \frac{\partial f}{\partial y} \\ \end{bmatrix} ∇f=[GxGy]=[∂x∂f∂y∂f]
在
(
x
,
y
)
(x,y)
(x,y)点处的梯度,方向是
f
(
x
,
y
)
f(x,y)
f(x,y) 在这点变化率最大的方向,其大小等于
f
(
x
,
y
)
f (x,y)
f(x,y) 的最大变化率:
∣
∇
f
(
x
,
y
)
∣
=
[
(
∂
f
∂
x
)
2
+
(
∂
f
∂
y
)
2
]
1
2
|\nabla f(x,y)|= \begin{bmatrix} (\frac{\partial f}{\partial x})^2+(\frac{\partial f}{\partial y})^2 \end{bmatrix}^\frac{1}{2}
∣∇f(x,y)∣=[(∂x∂f)2+(∂y∂f)2]21
二维
对于二维离散函数
f
(
i
,
j
)
f(i,j)
f(i,j),用有限差分作为梯度大小的近似:
∣
∇
f
(
i
,
j
)
∣
=
{
[
f
(
i
+
1
,
j
)
−
f
(
i
,
j
)
]
2
+
[
f
(
i
,
j
+
1
)
−
f
(
i
,
j
)
]
2
}
1
2
|\nabla f(i,j)|=\{[f(i+1,j)-f(i,j)]^2+[f(i,j+1)-f(i,j)]^2\}^\frac{1}{2}
∣∇f(i,j)∣={[f(i+1,j)−f(i,j)]2+[f(i,j+1)−f(i,j)]2}21
上式包含平方和开方,运算量大,使用绝对差分近似:
∣
∇
f
(
i
,
j
)
∣
=
∣
f
(
i
+
1
,
j
)
−
f
(
i
,
j
)
∣
+
∣
f
(
i
,
j
+
1
)
−
f
(
i
,
j
)
∣
|\nabla f(i,j)|=|f(i+1,j)-f(i,j)|+|f(i,j+1)-f(i,j)|
∣∇f(i,j)∣=∣f(i+1,j)−f(i,j)∣+∣f(i,j+1)−f(i,j)∣
实际使用中常采用另一种近似梯度—Robert交叉梯度
:
∣
∇
f
(
i
,
j
)
∣
=
∣
f
(
i
+
1
,
j
+
1
)
−
f
(
i
,
j
)
∣
+
∣
f
(
i
,
j
+
1
)
−
f
(
i
+
1
,
j
)
∣
|\nabla f(i,j)|=|f(i+1,j+1)-f(i,j)|+|f(i,j+1)-f(i+1,j)|
∣∇f(i,j)∣=∣f(i+1,j+1)−f(i,j)∣+∣f(i,j+1)−f(i+1,j)∣
Robert交叉梯度模板
w 1 = [ − 1 0 0 − 1 ] 对 45 ° 边 缘 有 较 强 响 应 , 但 是 对 − 45 ° 边 缘 没 有 响 应 w 2 = [ 0 − 1 − 1 0 ] 对 − 45 ° 边 缘 有 较 强 响 应 但 是 对 45 ° 边 缘 没 有 响 应 w1= \begin{bmatrix} -1&0\\ 0&-1\\ \end{bmatrix}对45°边缘有较强响应,但是对-45°边缘没有响应\\ w2= \begin{bmatrix} 0&-1\\ -1&0\\ \end{bmatrix}对-45°边缘有较强响应但是对45°边缘没有响应\\ w1=[−100−1]对45°边缘有较强响应,但是对−45°边缘没有响应w2=[0−1−10]对−45°边缘有较强响应但是对45°边缘没有响应
最终的Robert
模板锐化图像为
w
1
w1
w1 和
w
2
w2
w2 两个模板运算结果之和。
Code
import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv
# Robert 锐化
def robert(img,w):
"""
:param img: 输入灰度图像
:param w: Robert 模板
:return : 锐化图像
"""
cp_img = np.copy(img)
re_img = np.copy(img)
for i in range(cp_img.shape[0]-1):
for j in range(cp_img.shape[1]-1):
temp = w*cp_img[i:i+2,j:j+2]
re_img[i][j] = np.abs(temp[1][1]-temp[0][0])+np.abs(temp[1][0]-temp[0][1])
return re_img
def show(img,title,sub):
plt.subplot(sub[0],sub[1],sub[2])
plt.imshow(img,cmap="gray")
plt.axis('off') # 去掉坐标轴
plt.title(title)
if __name__=="__main__":
img = cv.imread('12.png')
img = cv.cvtColor(img,cv.COLOR_RGB2GRAY)
w1 = np.array([[-1,0],[0,1]]) # 对 45°边缘有较强响应
w2 = np.array([[0,-1],[1,0]]) # 对 -45°边缘有较强响应
w = w1+w2 # 交叉梯度模板
re_img1 = robert(img, w1)
re_img2 = robert(img, w2)
re_img3 = robert(img, w)
show(img, 'original img', (1, 4, 1))
show(re_img1, 'w1', (1, 4, 2))
show(re_img2, 'w2', (1, 4, 3))
show(re_img3, 'w', (1, 4, 4))
plt.show()
Sobel梯度模板
S
o
b
e
l
Sobel
Sobel 梯度定义为:
m
a
s
k
=
[
z
1
z
2
z
3
z
4
z
5
z
6
z
7
z
8
z
9
]
S
x
=
(
z
7
+
2
z
8
+
z
9
)
−
(
z
1
+
2
z
2
+
z
3
)
S
y
=
(
z
3
+
2
z
6
+
z
9
)
−
(
z
1
+
2
z
4
+
z
7
)
M
(
i
,
j
)
=
∣
S
x
∣
+
∣
S
y
∣
使
用
权
重
2
的
目
的
:
突
出
中
心
点
mask = \begin{bmatrix} z_1&z_2&z_3\\ z_4&z_5&z_6\\ z_7&z_8&z_9\\ \end{bmatrix}\\ S_x = (z_7+2z_8+z_9)-(z_1+2z_2+z_3)\\ S_y = (z_3+2z_6+z_9)-(z_1+2z_4+z_7)\\ M(i,j) = |S_x|+|S_y| 使用权重2的目的:突出中心点
mask=⎣⎡z1z4z7z2z5z8z3z6z9⎦⎤Sx=(z7+2z8+z9)−(z1+2z2+z3)Sy=(z3+2z6+z9)−(z1+2z4+z7)M(i,j)=∣Sx∣+∣Sy∣使用权重2的目的:突出中心点
S
x
=
[
−
1
−
2
−
1
0
0
0
1
2
1
]
S
y
=
[
−
1
0
1
−
2
0
2
−
1
0
1
]
S_x = \begin{bmatrix} -1&-2&-1\\ 0&0&0\\ 1&2&1\\ \end{bmatrix} S_y = \begin{bmatrix} -1&0&1\\ -2&0&2\\ -1&0&1\\ \end{bmatrix}
Sx=⎣⎡−101−202−101⎦⎤Sy=⎣⎡−1−2−1000121⎦⎤
S
x
S_x
Sx对水平边缘有较强响应,
S
y
S_y
Sy 对竖直边缘有较强响应。微分模板的所有系数之和为0,保证了灰度恒定区域的响应为0。
Code
# Sobel锐化滤波器
def sobel(img):
"""
:param img: 输入灰度图像
:return: 返回锐化后的图像
"""
cp_img = np.copy(img)
Sx = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
Sy = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
row, col = Sx.shape
# 边界补零处理
r = (row-1)//2
c = (col-1)//2
rb = np.zeros((r,cp_img.shape[1]))
temp = np.vstack([rb,cp_img,rb])
cb = np.zeros((temp.shape[0],c))
last = np.hstack([cb,temp,cb])
mem = np.zeros(cp_img.shape)
# 加权求和
for i in range(r,last.shape[0]-r):
for j in range(c,last.shape[1]-c):
mem[i-r][j-c] = np.abs(np.sum(Sx*last[i-r:i+r+1,j-c:j+c+1]))+ np.abs(np.sum(Sy*last[i-r:i+r+1,j-c:j+c+1]))
return mem
def show(img,title,sub):
plt.subplot(sub[0],sub[1],sub[2])
plt.imshow(img,cmap="gray")
plt.axis('off') # 去掉坐标轴
plt.title(title)
if __name__=="__main__":
img = cv.imread('16.jpg',0)
re_img = sobel(img)
sobelx = cv.Sobel(img, cv.CV_8U, 1, 0, ksize=3)
sobely = cv.Sobel(img, cv.CV_8U, 0, 1, ksize=3)
show(re_img, 'sobel', (1, 3, 1))
show(img, 'img', (1, 3, 2))
show((sobely+sobelx), 'cv.sobel', (1, 3, 3))
plt.show()
拉普拉斯算子
f
(
x
,
y
)
f(x,y)
f(x,y) 函数的拉普拉斯运算:
∇
2
f
=
∂
2
f
∂
x
2
+
∂
2
f
∂
y
2
\nabla ^2f = \frac{\partial ^2f}{\partial x^2}+\frac{\partial ^2f}{\partial y^2}
∇2f=∂x2∂2f+∂y2∂2f
差分形式为:
∂
2
f
∂
x
2
=
f
(
x
+
1
,
y
)
+
f
(
x
−
1
,
y
)
−
2
f
(
x
,
y
)
∂
2
f
∂
y
2
=
f
(
x
,
y
+
1
)
+
f
(
x
,
y
−
1
)
−
2
f
(
x
,
y
)
∇
2
f
=
[
f
(
x
+
1
,
y
)
+
f
(
x
−
1
,
y
)
+
f
(
x
,
y
+
1
)
+
f
(
x
,
y
−
1
)
]
−
4
f
(
x
,
y
)
\frac{\partial ^2f}{\partial x^2}=f(x+1,y)+f(x-1,y)-2f(x,y) \\ \frac{\partial ^2f}{\partial y^2}=f(x,y+1)+f(x,y-1)-2f(x,y) \\ \nabla ^2f = [f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1)]-4f(x,y)
∂x2∂2f=f(x+1,y)+f(x−1,y)−2f(x,y)∂y2∂2f=f(x,y+1)+f(x,y−1)−2f(x,y)∇2f=[f(x+1,y)+f(x−1,y)+f(x,y+1)+f(x,y−1)]−4f(x,y)
拉普拉斯模板:对水平和竖直边缘都有较强响应
L
a
p
l
a
c
e
=
[
0
1
0
1
−
4
1
0
1
0
]
或
[
0
−
1
0
−
1
4
−
1
0
−
1
0
]
Laplace = \begin{bmatrix} 0&1&0\\ 1&-4&1\\ 0&1&0\\ \end{bmatrix}或 \begin{bmatrix} 0&-1&0\\ -1&4&-1\\ 0&-1&0\\ \end{bmatrix}
Laplace=⎣⎡0101−41010⎦⎤或⎣⎡0−10−14−10−10⎦⎤
拉普拉斯锐化的特点:
-
对噪声敏感;
-
产生双边缘;
拉普拉斯算子的扩展:对角线邻域加入变换中使得对各方向边缘均有较强响应。
[
0
1
0
1
−
4
1
0
1
0
]
+
[
1
0
1
0
−
4
0
1
0
1
]
=
[
1
1
1
1
−
8
1
1
1
1
]
\begin{bmatrix} 0&1&0\\ 1&-4&1\\ 0&1&0\\ \end{bmatrix}+ \begin{bmatrix} 1&0&1\\ 0&-4&0\\ 1&0&1\\ \end{bmatrix}= \begin{bmatrix} 1&1&1\\ 1&-8&1\\ 1&1&1\\ \end{bmatrix}
⎣⎡0101−41010⎦⎤+⎣⎡1010−40101⎦⎤=⎣⎡1111−81111⎦⎤
拉普拉斯运算对图像增强的基本方法:将原始图像和拉普拉斯图像相叠加
g
(
x
,
y
)
=
{
f
(
x
,
y
)
−
∇
2
f
(
x
,
y
)
模
板
中
心
系
数
为
负
f
(
x
,
y
)
+
∇
2
f
(
x
,
y
)
模
板
中
心
系
数
为
正
g(x,y)=\begin{cases} f(x,y)-\nabla ^2f(x,y)&模板中心系数为负 \\ f(x,y)+\nabla ^2f(x,y)&模板中心系数为正 \\ \end{cases}
g(x,y)={f(x,y)−∇2f(x,y)f(x,y)+∇2f(x,y)模板中心系数为负模板中心系数为正
Code
import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv
# Laplace拉普拉斯锐化
def laplace(img):
cp_img = np.copy(img)
lap = np.array([[0,1,0],[1,-4,1],[0,1,0]]) # Laplace算子
m = np.array([[1,0,1],[0,-4,0],[1,0,1]]) # 对角领域
extend_lap = lap+m # 扩展Laplace算子
row, col = extend_lap.shape
# 边界补零处理
r = (row-1)//2
c = (col-1)//2
rb = np.zeros((r,cp_img.shape[1]))
temp = np.vstack([rb,cp_img,rb])
cb = np.zeros((temp.shape[0],c))
last = np.hstack([cb,temp,cb])
mem = np.zeros(cp_img.shape)
# 加权求和
for i in range(r,last.shape[0]-r):
for j in range(c,last.shape[1]-c):
mem[i-r][j-c] = np.sum(extend_lap*last[i-r:i+r+1,j-c:j+c+1])
return mem
def show(img,title,sub):
plt.subplot(sub[0],sub[1],sub[2])
plt.imshow(img,cmap="gray")
plt.axis('off') # 去掉坐标轴
plt.title(title)
if __name__=="__main__":
img = cv.imread('16.jpg',0)
re_img = laplace(img)
laplacian = cv.Laplacian(img, cv.CV_8U)
show(re_img, 'laplace', (2, 2, 1))
show((re_img * 255 / np.max(re_img)).astype(np.uint8)+img, 'normalization laplace', (2, 2, 2))
show(img, 'img', (2, 2, 3))
show(laplacian+img, 'cv.Laplacian', (2, 2, 4))
plt.show()
锐化滤波器:高频提升滤波模板:
高斯—拉普拉斯(Laplace of Gaussian,LoG)算子
锐化在增强边缘和细节的同时也增强了噪声。为了在取得好的锐化效果的同时把噪声的干扰降到最低, 可以先对带噪声的原始图像进行平滑,再进行锐化。
L o G LoG LoG 算子把高斯平滑和拉普拉斯锐化结合起来:
高斯函数:
h
(
x
,
y
)
=
−
e
x
p
(
−
x
2
+
y
2
2
σ
2
)
=
−
e
x
p
(
−
r
2
2
σ
2
)
(
σ
2
为
标
准
差
)
h(x,y) = -exp(-\frac{x^2+y^2}{2\sigma ^2})=-exp(-\frac{r^2}{2\sigma ^2})(\sigma^2为标准差)
h(x,y)=−exp(−2σ2x2+y2)=−exp(−2σ2r2)(σ2为标准差)
高斯-拉普拉斯算子:
∇
2
h
=
−
[
r
2
+
σ
2
σ
4
]
e
x
p
(
−
x
2
+
y
2
2
σ
2
)
\nabla ^2h = -[\frac{r^2+\sigma^2}{\sigma^4}]exp(-\frac{x^2+y^2}{2\sigma^2})
∇2h=−[σ4r2+σ2]exp(−2σ2x2+y2)
常用的
L
o
G
LoG
LoG 算子是
5
×
5
5×5
5×5 的模板:
L
o
G
=
[
−
2
−
4
−
4
−
4
−
2
−
4
0
8
0
−
4
−
4
8
24
8
−
4
−
4
0
8
0
−
4
−
2
−
4
−
4
−
4
−
2
]
LoG = \begin{bmatrix} -2&-4&-4&-4&-2\\ -4&0&8&0&-4\\ -4&8&24&8&-4\\ -4&0&8&0&-4\\ -2&-4&-4&-4&-2\\ \end{bmatrix}
LoG=⎣⎢⎢⎢⎢⎡−2−4−4−4−2−4080−4−48248−4−4080−4−2−4−4−4−2⎦⎥⎥⎥⎥⎤