编写一个程序,实现:输入一副图像(灰度图像),实现理想低通、巴特沃斯低通、高斯低通滤波、理想高通、巴特沃斯高通、高斯高通滤波,输出滤波结果图。
时间: 2023-06-15 07:03:43 浏览: 101
以下是Python的实现:
``` python
import cv2
import numpy as np
from matplotlib import pyplot as plt
# 读取图像
img = cv2.imread('input.jpg', 0)
# 傅里叶变换
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
# 幅度谱
magnitude_spectrum = 20*np.log(np.abs(fshift))
# 理想低通滤波器
def ideal_lowpass_filter(size, cutoff):
rows, cols = size
crow, ccol = rows/2, cols/2
mask = np.zeros((rows, cols), np.uint8)
mask[int(crow)-cutoff:int(crow)+cutoff, int(ccol)-cutoff:int(ccol)+cutoff] = 1
return mask
# 巴特沃斯低通滤波器
def butterworth_lowpass_filter(size, cutoff, n):
rows, cols = size
crow, ccol = rows/2, cols/2
mask = np.zeros((rows, cols), np.uint8)
for i in range(rows):
for j in range(cols):
distance = np.sqrt((i-crow)**2 + (j-ccol)**2)
mask[i, j] = 1/(1+(distance/cutoff)**(2*n))
return mask
# 高斯低通滤波器
def gaussian_lowpass_filter(size, cutoff):
rows, cols = size
crow, ccol = rows/2, cols/2
x = np.arange(cols)
y = np.arange(rows)
x, y = np.meshgrid(x, y)
mask = np.exp(-((x-ccol)**2+(y-crow)**2)/(2*cutoff**2))
return mask
# 理想高通滤波器
def ideal_highpass_filter(size, cutoff):
rows, cols = size
crow, ccol = rows/2, cols/2
mask = np.ones((rows, cols), np.uint8)
mask[int(crow)-cutoff:int(crow)+cutoff, int(ccol)-cutoff:int(ccol)+cutoff] = 0
return mask
# 巴特沃斯高通滤波器
def butterworth_highpass_filter(size, cutoff, n):
rows, cols = size
crow, ccol = rows/2, cols/2
mask = np.zeros((rows, cols), np.uint8)
for i in range(rows):
for j in range(cols):
distance = np.sqrt((i-crow)**2 + (j-ccol)**2)
mask[i, j] = 1/(1+(cutoff/distance)**(2*n))
return mask
# 高斯高通滤波器
def gaussian_highpass_filter(size, cutoff):
rows, cols = size
crow, ccol = rows/2, cols/2
x = np.arange(cols)
y = np.arange(rows)
x, y = np.meshgrid(x, y)
mask = 1 - np.exp(-((x-ccol)**2+(y-crow)**2)/(2*cutoff**2))
return mask
# 理想低通滤波
ideal_lp = fshift * ideal_lowpass_filter(img.shape, 30)
ideal_lp_img = np.abs(np.fft.ifft2(np.fft.ifftshift(ideal_lp)))
# 巴特沃斯低通滤波
butterworth_lp = fshift * butterworth_lowpass_filter(img.shape, 30, 2)
butterworth_lp_img = np.abs(np.fft.ifft2(np.fft.ifftshift(butterworth_lp)))
# 高斯低通滤波
gaussian_lp = fshift * gaussian_lowpass_filter(img.shape, 30)
gaussian_lp_img = np.abs(np.fft.ifft2(np.fft.ifftshift(gaussian_lp)))
# 理想高通滤波
ideal_hp = fshift * ideal_highpass_filter(img.shape, 30)
ideal_hp_img = np.abs(np.fft.ifft2(np.fft.ifftshift(ideal_hp)))
# 巴特沃斯高通滤波
butterworth_hp = fshift * butterworth_highpass_filter(img.shape, 30, 2)
butterworth_hp_img = np.abs(np.fft.ifft2(np.fft.ifftshift(butterworth_hp)))
# 高斯高通滤波
gaussian_hp = fshift * gaussian_highpass_filter(img.shape, 30)
gaussian_hp_img = np.abs(np.fft.ifft2(np.fft.ifftshift(gaussian_hp)))
# 显示结果
plt.subplot(331), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(332), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.subplot(333), plt.imshow(ideal_lp_img, cmap='gray')
plt.title('Ideal Lowpass Filter'), plt.xticks([]), plt.yticks([])
plt.subplot(334), plt.imshow(butterworth_lp_img, cmap='gray')
plt.title('Butterworth Lowpass Filter'), plt.xticks([]), plt.yticks([])
plt.subplot(335), plt.imshow(gaussian_lp_img, cmap='gray')
plt.title('Gaussian Lowpass Filter'), plt.xticks([]), plt.yticks([])
plt.subplot(336), plt.imshow(ideal_hp_img, cmap='gray')
plt.title('Ideal Highpass Filter'), plt.xticks([]), plt.yticks([])
plt.subplot(337), plt.imshow(butterworth_hp_img, cmap='gray')
plt.title('Butterworth Highpass Filter'), plt.xticks([]), plt.yticks([])
plt.subplot(338), plt.imshow(gaussian_hp_img, cmap='gray')
plt.title('Gaussian Highpass Filter'), plt.xticks([]), plt.yticks([])
plt.show()
```
其中,`ideal_lowpass_filter`、`butterworth_lowpass_filter`、`gaussian_lowpass_filter`、`ideal_highpass_filter`、`butterworth_highpass_filter`、`gaussian_highpass_filter` 分别为六种滤波器的实现函数。这里的例子中使用的是 30 作为截止频率,可以根据需要进行调整。最终的结果如下图所示:
![image](https://user-images.githubusercontent.com/44194626/128274797-2a62a5ec-e9f2-4c6e-9b7d-d6f7a71b4c0f.png)