本次我使用的巴特沃斯低通滤波器,当然也可以直接调用opencv中的函数
def butterworth_lowpass_filter(image, d, n):
image = np.pad(image, ((0, image.shape[0] % 2), (0, image.shape[1] % 2), (0, 0)), mode='constant')
rows, cols, channel = image.shape
crow, ccol = rows // 2, cols // 2
f = np.fft.fft2(image)
fshift = np.fft.fftshift(f)
# 修改部分:使用numpy.shape函数来获取图像的行数和列数
u = np.arange(-crow, crow, 1) * (2 / rows)
v = np.arange(-ccol, ccol, 1) * (2 / cols)
U, V = np.meshgrid(u, v)
D = np.sqrt(U ** 2 + V ** 2)
H = 1 / (1 + (D / d) ** (2 * n))
for i in range(channel):
fshift[:, :, i] = fshift[:, :, i] * H.T
f_ishift = np.fft.ifftshift(fshift)
image_filtered = np.fft.ifft2(f_ishift)
image_filtered = np.abs(image_filtered)
return image_filtered.astype(np.uint8)