【OpenCV 完整例程】84. 由低通滤波器得到高通滤波器
4. 频率域高通滤波器
图像边缘化其它灰度的急剧变化与高频分量有关,因此可以在频率域通过高通滤波实现图像锐化。高通滤波衰减傅里叶变换中的低频分量而不干扰高频信息。
4.1 由低通滤波器得到高通滤波器
简单地,在频率域中用 1 减去低通滤波器的传递函数,就可以得到相应的高通滤波器传递函数:
H
H
P
(
u
,
v
)
=
1
−
H
L
P
(
u
,
v
)
H_{HP}(u,v) = 1- H_{LP}(u,v)
HHP(u,v)=1−HLP(u,v)
式中,
H
H
P
(
u
,
v
)
H_{HP}(u,v)
HHP(u,v)、
H
L
P
(
u
,
v
)
H_{LP}(u,v)
HLP(u,v) 分别表示高通滤波器、低通滤波器的传递函数。
理想高通滤波器(IHPF)的传递函数为:
H
(
u
,
v
)
=
{
0
,
D
(
u
,
v
)
≤
D
0
1
,
D
(
u
,
v
)
>
D
0
H(u,v)=\begin{cases} 0,\ D(u,v) \leq D_0\\ 1,\ D(u,v)>D_0 \end{cases}
H(u,v)={0, D(u,v)≤D01, D(u,v)>D0
高斯高通滤波器(GHPF)的传递函数为:
H
(
u
,
v
)
=
1
−
e
−
D
2
(
u
,
v
)
/
2
D
0
2
H(u,v)=1-e^{-D^2 (u,v)/2D_0^2}
H(u,v)=1−e−D2(u,v)/2D02
巴特沃斯高通滤波器(BHPF)的传递函数为:
H
(
u
,
v
)
=
1
1
+
[
D
0
/
D
(
u
,
v
)
]
2
n
H(u,v)= \frac {1} {1 + [D_0/D(u,v)]^{2n}}
H(u,v)=1+[D0/D(u,v)]2n1
例程 8.23 由低通滤波器得到高通滤波器
# OpenCVdemo08.py
# Demo08 of OpenCV
# 8. 图像的频率域滤波
# Copyright 2021 Youcans, XUPT
# Crated:2021-12-15
# 8.23:频率域高通滤波器
def ideaHighPassFilter(shape, radius=10): # 理想高通滤波器
u, v = np.mgrid[-1:1:2.0/shape[0], -1:1:2.0/shape[1]]
D = np.sqrt(u**2 + v**2)
D0 = radius / shape[0]
kernel = np.ones(shape)
kernel[D <= D0] = 0 # 理想低通滤波 (Idea low pass filter)
return kernel
def gaussHighPassFilter(shape, radius=10): # 高斯高通滤波器
# 高斯滤波器:# Gauss = 1/(2*pi*s2) * exp(-(x**2+y**2)/(2*s2))
u, v = np.mgrid[-1:1:2.0/shape[0], -1:1:2.0/shape[1]]
D = np.sqrt(u**2 + v**2)
D0 = radius / shape[0]
kernel = 1 - np.exp(- (D ** 2) / (2 *D0**2))
return kernel
def butterworthHighPassFilter(shape, radius=10, n=2): # 巴特沃斯高通滤波
u, v = np.mgrid[-1:1:2.0/shape[0], -1:1:2.0/shape[1]]
epsilon = 1e-8
D = np.sqrt(u**2 + v**2)
D0 = radius / shape[0]
kernel = 1.0 / (1.0 + np.power(D0/(D + epsilon), 2*n))
return kernel
# 理想、高斯、巴特沃斯高通传递函数
shape = [128, 128]
radius = 32
IHPF = ideaHighPassFilter(shape, radius=radius)
GHPF = gaussHighPassFilter(shape, radius=radius)
BHPF = butterworthHighPassFilter(shape, radius=radius)
filters = ['IHPF', 'GHPF', 'BHPF']
u, v = np.mgrid[-1:1:2.0/shape[0], -1:1:2.0/shape[1]]
fig = plt.figure(figsize=(10, 8))
for i in range(3):
hpFilter = eval(filters[i]).copy()
ax1 = fig.add_subplot(3, 3, 3*i+1)
ax1.imshow(hpFilter, 'gray')
ax1.set_title(filters[i]), ax1.set_xticks([]), ax1.set_yticks([])
ax2 = plt.subplot(3,3,3*i+2, projection='3d')
ax2.set_title("transfer function")
ax2.plot_wireframe(u, v, hpFilter , rstride=2, linewidth=0.5, color='c')
ax2.set_xticks([]), ax2.set_yticks([]), ax2.set_zticks([])
ax3 = plt.subplot(3,3,3*i+3)
profile = hpFilter[shape[0]//2:, shape[1]//2]
ax3.plot(profile), ax3.set_title("profile"), ax3.set_xticks([]), ax3.set_yticks([])
plt.show()
(本节完)
版权声明:
youcans@xupt 原创作品,转载必须标注原文链接
Copyright 2021 youcans, XUPT
Crated:2022-1-25