写出图像实现低通滤波、高通、带通、带阻和同态滤波的程序代码。观察滤波结果,并加以解释
时间: 2023-07-10 12:06:38 浏览: 167
【老生谈算法】matlab实现对图像进行高通、低通、带通滤波算法.doc
5星 · 资源好评率100%
下面是Python实现的图像滤波代码,包括低通滤波、高通滤波、带通滤波、带阻滤波和同态滤波。
```python
import cv2
import numpy as np
from matplotlib import pyplot as plt
# 读取图像
img = cv2.imread('image.jpg', 0)
# 傅里叶变换
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
# 低通滤波
rows, cols = img.shape
crow, ccol = int(rows/2), int(cols/2)
mask = np.zeros((rows, cols), np.uint8)
r = 60
cv2.circle(mask, (ccol, crow), r, 255, -1)
fshift = fshift * mask
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)
plt.subplot(2, 3, 1), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 3, 2), plt.imshow(mask, cmap='gray')
plt.title('Low Pass Filter'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 3, 3), plt.imshow(img_back, cmap='gray')
plt.title('Low Pass Filter Result'), plt.xticks([]), plt.yticks([])
# 高通滤波
rows, cols = img.shape
crow, ccol = int(rows/2), int(cols/2)
mask = np.ones((rows, cols), np.uint8)
r = 30
cv2.circle(mask, (ccol, crow), r, 0, -1)
fshift = fshift * mask
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)
plt.subplot(2, 3, 4), plt.imshow(mask, cmap='gray')
plt.title('High Pass Filter'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 3, 5), plt.imshow(img_back, cmap='gray')
plt.title('High Pass Filter Result'), plt.xticks([]), plt.yticks([])
# 带通滤波
rows, cols = img.shape
crow, ccol = int(rows/2), int(cols/2)
mask = np.zeros((rows, cols), np.uint8)
r_out = 60
r_in = 30
cv2.circle(mask, (ccol, crow), r_out, 255, -1)
cv2.circle(mask, (ccol, crow), r_in, 0, -1)
fshift = fshift * mask
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)
plt.subplot(2, 3, 6), plt.imshow(img_back, cmap='gray')
plt.title('Band Pass Filter Result'), plt.xticks([]), plt.yticks([])
# 带阻滤波
rows, cols = img.shape
crow, ccol = int(rows/2), int(cols/2)
mask = np.ones((rows, cols), np.uint8)
r_out = 60
r_in = 30
cv2.circle(mask, (ccol, crow), r_out, 0, -1)
cv2.circle(mask, (ccol, crow), r_in, 255, -1)
fshift = fshift * mask
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)
plt.figure()
plt.subplot(1, 2, 1), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(1, 2, 2), plt.imshow(img_back, cmap='gray')
plt.title('Band Reject Filter Result'), plt.xticks([]), plt.yticks([])
# 同态滤波
img = cv2.imread('image.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rows, cols = img.shape
crow, ccol = int(rows/2), int(cols/2)
# 频域中心化
img_float32 = np.float32(img)
dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
# 构建同态滤波器
a, b = 0.5, 2.0
gamma_l, gamma_h = 0.5, 1.5
H = np.zeros((rows, cols), dtype=complex)
for u in range(rows):
for v in range(cols):
d = np.sqrt((u - crow)**2 + (v - ccol)**2)
H[u, v] = (b - a) * (1 - np.exp(-gamma_h * d**2)) + a * np.exp(-gamma_l * d**2)
H[u, v] = 1 / H[u, v]
# 滤波
F = H * dft_shift
f_ishift = np.fft.ifftshift(F)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
plt.figure()
plt.subplot(1, 2, 1), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(1, 2, 2), plt.imshow(img_back, cmap='gray')
plt.title('Homomorphic Filter Result'), plt.xticks([]), plt.yticks([])
plt.show()
```
上述代码实现了以下五种滤波:
- 低通滤波:保留低频成分,去除高频成分。结果图像模糊化,细节丢失。
- 高通滤波:保留高频成分,去除低频成分。结果图像增强了边缘和细节。
- 带通滤波:保留某个频率范围内的成分,去除其他频率成分。结果图像只保留某些特定的细节或纹理。
- 带阻滤波:去除某个频率范围内的成分,保留其他频率成分。结果图像去除了某些特定的细节或纹理。
- 同态滤波:去除图像的低频和高频成分,增强中频成分。结果图像亮度和对比度得到了调整,同时也去除了噪声。
需要注意的是,上述代码中使用的是傅里叶变换进行频域滤波。实际上,还有其他的频域滤波方法,如小波变换等。选择何种方法取决于具体应用场景和需求。
阅读全文