【OpenCV 完整例程】89. 带阻滤波器的传递函数
5.1 带阻与带通
空间域和频率域线性滤波器可以分为四类:低通滤波器、高通滤波器、带通滤波器和带阻滤波器。高通滤波和低通滤波都是在整个频率矩形上操作,带通滤波和带阻滤波则是对特定频带处理,属于选择性滤波。
带阻滤波器(bandstop filters,简称BSF)是指能通过大多数频率分量、但将某些范围的频率分量衰减到极低水平的滤波器。带通滤波器(band-pass filter)是一个允许特定频段的波通过同时屏蔽其他频段的设备。比如RLC振荡回路就是一个模拟带通滤波器。
频率域的高通滤波器可以由低通滤波器推导而来。类似地,频率域中的带通和带阻滤波器的传递函数,可以通过低通滤波器和高通滤波器的组合来构建。
理想带阻滤波器(IBRF) 的传递函数为:
H
(
u
,
v
)
=
{
0
,
(
C
0
−
W
/
2
)
≤
D
(
u
,
v
)
≤
(
C
0
+
W
/
2
)
1
,
e
l
s
e
H(u,v)=\begin{cases} 0,\ (C_0-W/2) \leq D(u,v) \leq (C_0+W/2)\\ 1,\ else \end{cases}
H(u,v)={0, (C0−W/2)≤D(u,v)≤(C0+W/2)1, else
高斯带阻滤波器(GBRF) 的传递函数为:
H
(
u
,
v
)
=
1
−
e
−
[
D
2
(
u
,
v
)
−
C
0
2
D
(
u
,
v
)
W
]
2
H(u,v)=1-e^{-[ \frac {D^2(u,v) - C_0^2} {D(u,v)W}]^2}
H(u,v)=1−e−[D(u,v)WD2(u,v)−C02]2
巴特沃斯带阻滤波器(BBRF) 的传递函数为:
H ( u , v ) = 1 1 + [ D ( u , v ) W D 2 ( u , v ) − C 0 2 ] 2 n H(u,v)= \frac {1} {1 +[ \frac {D(u,v)W} {D^2(u,v) - C_0^2}]^{2n}} H(u,v)=1+[D2(u,v)−C02D(u,v)W]2n1
例程 8.28 带阻滤波器的传递函数
# OpenCVdemo08.py
# Demo08 of OpenCV
# 8. 图像的频率域滤波
# Copyright 2021 Youcans, XUPT
# Crated:2021-12-30
# 例程 8.28 带阻滤波器的传递函数
def ideaBondResistFilter(shape, radius=10, w=5): # 理想带阻滤波器
u, v = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]))
D = np.sqrt((u - shape[1]//2)**2 + (v - shape[0]//2)**2)
D0 = radius
halfW = w/2
kernel = np.piecewise(D, [D<=D0+halfW, D<=D0-halfW], [1, 0])
kernel = 1 - kernel # 带阻
return kernel
def gaussBondResistFilter(shape, radius=10, w=5): # 高斯带阻滤波器
# 高斯滤波器:# Gauss = 1/(2*pi*s2) * exp(-(x**2+y**2)/(2*s2))
u, v = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]))
D = np.sqrt((u - shape[1]//2)**2 + (v - shape[0]//2)**2)
C0 = radius
kernel = 1 - np.exp(-(D-C0)**2 / (w**2))
return kernel
def butterworthBondResistFilter(shape, radius=10, w=5, n=1): # 巴特沃斯带阻滤波
u, v = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]))
D = np.sqrt((u - shape[1]//2)**2 + (v - shape[0]//2)**2)
C0 = radius
epsilon = 1e-8 # 防止被 0 除
kernel = 1.0 / (1.0 + np.power(D*w/(D**2-C0**2+epsilon), 2*n))
return kernel
# 理想、高斯、巴特沃斯带阻滤波器传递函数
shape = [128, 128]
radius = 32
IBRF = ideaBondResistFilter(shape, radius=radius)
GBRF = gaussBondResistFilter(shape, radius=radius)
BBRF = butterworthBondResistFilter(shape, radius=radius)
filters = ["IBRF", "GBRF", "BBRF"]
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-2-1