梯形高通滤波
梯形高通滤波器(TLPF)的传递函数如下:
$$
H(u,v) = 1 - \begin{cases}
1 & D(u,v) \lt D_0 \\
\frac{D(u,v) - D_1}{D_0-D_1} & D_0 \lt D(u,v) \leq D_1 \\
0 &D(u,v) > D_1
\end{cases}
\tag{18-1}
$$
Python语言实现的代码如下:
def trapezoidal_low_pass_kernel(img,D0=5,D1=10):
assert img.ndim == 2
r,c = img.shape[1],img.shape[0]
u = np.arange(r)
v = np.arange(c)
u, v = np.meshgrid(u, v)
low_pass = np.sqrt( (u-r/2)**2 + (v-c/2)**2 )
idx = low_pass < D0
idx2 = (low_pass >= D0) & (low_pass <= D1)
idx3 = low_pass > D1
low_pass[idx] = 1
low_pass[idx2] = (low_pass[idx2] - D1) / (D1 - D0)
low_pass[idx3] = 0
return low_pass
def trapezoidal_high_pass_filter(img,D0=5,D1=15):
assert img.ndim == 2
gray = np.float64(img)
kernel = 1 - trapezoidal_low_pass_kernel(img,D0,D1)
gray_fft = np.fft.fft2(gray)
gray_fftshift = np.fft.fftshift(gray_fft)
dst = np.zeros_like(gray_fftshift)
dst_filtered = kernel * gray_fftshift
dst_ifftshift = np.fft.ifftshift(dst_filtered)
dst_ifft = np.fft.ifft2(dst_ifftshift)
dst = np.abs(np.real(dst_ifft))
dst = np.clip(dst,0,255)
return np.uint8(dst)
程序运行结果: