锐化(高通)空间滤波器
- 平滑通过称为低通滤波
- 类似于积分运算
- 锐化通常称为高通滤波
- 微分运算
- 高过(负责细节的)高频,衰减或抑制低频
使用一阶导数锐化图像-梯度
在图像处理中,一阶导数是用梯度幅度实现的,图像的梯度定义为二维列向量
∇
f
≡
grad
(
f
)
=
[
g
x
g
y
]
=
[
∂
f
/
∂
x
∂
f
/
∂
y
]
(3.57)
\nabla f \equiv \text{grad}(f) = \begin{bmatrix} g_x \\ g_y \end{bmatrix} = \begin{bmatrix} \partial f /\partial x \\ \partial f /\partial y \ \end{bmatrix} \tag{3.57}
∇f≡grad(f)=[gxgy]=[∂f/∂x∂f/∂y ](3.57)
向量
∇
f
\nabla f
∇f的幅度表示为
M
(
x
,
y
)
M(x, y)
M(x,y),也经常使用向量范数
∥
∇
f
∥
\lVert\nabla f \rVert
∥∇f∥
M
(
x
,
y
)
=
∥
f
∥
=
mag
(
∇
f
)
=
g
x
2
+
g
y
2
(3.58)
M(x, y) = \lVert f \rVert = \text{mag}(\nabla f) = \sqrt{g_x^2 + g_y^2} \tag{3.58}
M(x,y)=∥f∥=mag(∇f)=gx2+gy2(3.58)
是梯度向量方向的变化率在 ( x , y ) (x, y) (x,y)处的值,是与原图像大小相同的图像,通常称为梯度图像
在某些实现中,使用绝对值来近似平方运算和平方根运算更合适:
M
(
x
,
y
)
≈
∣
g
x
∣
+
∣
g
y
∣
(3.59)
M(x,y) \approx |g_x| + |g_y| \tag{3.59}
M(x,y)≈∣gx∣+∣gy∣(3.59)
这个表达式通常会损失各向同性。
最简近似的一阶导数是 g x = ( z 8 − z 5 ) g_x = (z_8 - z_5) gx=(z8−z5)和 g y = ( z 6 − z 5 ) g_y = (z_6 - z_5) gy=(z6−z5)
罗伯特交叉梯度算子,早期的图像处理使用交叉差值
g
x
=
(
z
9
−
z
5
)
和
g
y
=
(
z
8
−
z
6
)
(3.60)
g_x = (z_9 - z_5)和g_y = (z_8 - z_6) \tag{3.60}
gx=(z9−z5)和gy=(z8−z6)(3.60)
梯度图像计算为:
M
(
x
,
y
)
=
[
(
z
9
−
z
5
)
2
+
(
z
8
−
z
6
)
2
]
1
/
2
(3.61)
M(x, y) = \Big[(z_9 - z_5)^2 + (z_8 - z_6)^2 \Big]^{1/2} \tag{3.61}
M(x,y)=[(z9−z5)2+(z8−z6)2]1/2(3.61)
M
(
x
,
y
)
≈
∣
z
9
−
z
5
∣
+
∣
z
8
−
z
6
∣
(3.62)
M(x, y) \approx |z_9 - z_5| + |z_8 - z_6| \tag{3.62}
M(x,y)≈∣z9−z5∣+∣z8−z6∣(3.62)
3
×
3
3\times 3
3×3的核
g
x
=
∂
f
/
∂
x
=
(
z
7
+
2
z
8
+
z
9
)
−
(
z
1
+
2
z
2
+
z
3
)
(3.63)
g_x = \partial f/ \partial x = (z_7 + 2z_8 + z_9) - (z_1 +2z_2 + z_3) \tag{3.63}
gx=∂f/∂x=(z7+2z8+z9)−(z1+2z2+z3)(3.63)
g
y
=
∂
f
/
∂
y
=
(
z
3
+
2
z
6
+
z
9
)
−
(
z
1
+
2
z
4
+
z
7
)
(3.64)
g_y = \partial f/ \partial y = (z_3 + 2z_6 + z_9) - (z_1 +2z_4 + z_7) \tag{3.64}
gy=∂f/∂y=(z3+2z6+z9)−(z1+2z4+z7)(3.64)
M
(
x
,
y
)
=
[
g
x
2
+
g
y
2
]
1
/
2
=
[
[
(
z
7
+
2
z
8
+
z
9
)
−
(
z
1
+
2
z
2
+
z
3
)
]
2
+
[
(
z
3
+
2
z
6
+
z
9
)
−
(
z
1
+
2
z
4
+
z
7
)
]
2
]
1
/
2
(3.65)
M(x, y) = [g_x^2 + g_y^2]^{1/2} = \Big[[(z_7 + 2z_8 + z_9) - (z_1 +2z_2 + z_3)]^2 + [(z_3 + 2z_6 + z_9) - (z_1 +2z_4 + z_7)]^2\Big]^{1/2} \tag{3.65}
M(x,y)=[gx2+gy2]1/2=[[(z7+2z8+z9)−(z1+2z2+z3)]2+[(z3+2z6+z9)−(z1+2z4+z7)]2]1/2(3.65)
def visualize_show_annot(img_show, img_annot, ax, string='img_annot'):
"""
add annotation to the image, values of each pixel
param: img: input image
param: ax: axes of the matplotlib
"""
height, width = img_annot.shape
img_show = img_show[:height, :width]
ax.imshow(img_show, cmap='gray', vmin=0, vmax=255)
thresh = 10 #img_show.max()/2.5
for x in range(height):
for y in range(width):
if string == 'img_annot':
ax.annotate(str(round(img_annot[x][y],2)), xy=(y,x),
horizontalalignment='center',
verticalalignment='center',
color='white' if img_annot[x][y]>thresh else 'black')
else:
ax.annotate(string + str(x + y + 1), xy=(y,x),
horizontalalignment='center',
verticalalignment='center',
color='white' if img_annot[x][y]>thresh else 'black')
# 一阶导数算子,罗伯特交叉梯度算子,Sobel算子
height, width = 3, 3
img_show = np.ones([height, width], dtype=np.uint8) * 250
img_ori = np.zeros([height, width], dtype=np.uint8)
fig = plt.figure(figsize=(8, 6))
ax1 = fig.add_subplot(2, 3, 1)
ax1.set_title('3x3 Region'), visualize_show_annot(img_show, img_ori, ax1, string='z'), plt.xticks([]), plt.yticks([])
img_ori = np.zeros([2, 2], np.int)
img_ori[0, 0] = -1
img_ori[1, 1] = 1
ax2 = fig.add_subplot(2, 3, 2)
ax2.set_title('Robert operator'), visualize_show_annot(img_show, img_ori, ax2), plt.xticks([]), plt.yticks([])
img_ori = np.zeros([2, 2], np.int)
img_ori[0, 1] = -1
img_ori[1, 0] = 1
ax3 = fig.add_subplot(2, 3, 3)
ax3.set_title('Robert operator'), visualize_show_annot(img_show, img_ori, ax3), plt.xticks([]), plt.yticks([])
img_ori = np.zeros([3, 3], np.int)
img_ori[0, :] = np.array([-1, -2, -1])
img_ori[2, :] = np.array([1, 2, 1])
ax4 = fig.add_subplot(2, 3, 4)
ax4.set_title('Sobel operator'), visualize_show_annot(img_show, img_ori, ax4), plt.xticks([]), plt.yticks([])
img_ori = np.zeros([3, 3], np.int)
img_ori[:, 0] = np.array([-1, -2, -1])
img_ori[:, 2] = np.array([1, 2, 1])
ax5 = fig.add_subplot(2, 3, 5)
ax5.set_title('Sobel operator'), visualize_show_annot(img_show, img_ori, ax5), plt.xticks([]), plt.yticks([])
plt.tight_layout()
plt.show()
# Sobel梯度增强边缘
img_ori = cv2.imread("DIP_Figures/DIP3E_Original_Images_CH03/Fig0342(a)(contact_lens_original).tif", 0)
sobel_x = np.zeros([3, 3], np.int)
sobel_x[0, :] = np.array([-1, -2, -1])
sobel_x[2, :] = np.array([1, 2, 1])
sobel_y = np.zeros([3, 3], np.int)
sobel_y[:, 0] = np.array([-1, -2, -1])
sobel_y[:, 2] = np.array([1, 2, 1])
# gx = separate_kernel_conv2D(img_ori, kernel=sobel_x)
# gy = separate_kernel_conv2D(img_ori, kernel=sobel_y)
gx = cv2.filter2D(img_ori, ddepth=-1, kernel=sobel_x)
gy = cv2.filter2D(img_ori, ddepth=-1, kernel=sobel_y)
gx = np.where(gx >= 100, gx, 0)
gx = np.where(gx < 100, gx, 1)
gy = np.where(gy >= 100, gy, 0)
gy = np.where(gy < 100, gy, 1)
# 先对gx gy做二值化处理再应用下面的公式
# img_sobel = np.sqrt(gx**2 + gy**2)
# 二值化后,平方根的效果与绝对值很接近
img_sobel = abs(gx) + abs(gy)
img_sobel = np.uint8(normalize(img_sobel) * 255)
plt.figure(figsize=(15, 12))
plt.subplot(1, 2, 1), plt.imshow(img_ori, 'gray', vmax=255), plt.title("Original"), plt.xticks([]), plt.yticks([])
plt.subplot(1, 2, 2), plt.imshow(img_sobel, 'gray', vmax=255), plt.title("Sobel"), plt.xticks([]), plt.yticks([])
plt.tight_layout()
plt.show()
梯度还可用来突出灰度级图像中很难看到的小尺度图像(如异物、保护液中的气泡或镜片中的微小缺陷)。在平坦的灰度场中增强小的不连续的能力是梯度的呬个重要特征