Python对RGB图像进行同态滤波处理代码
时间: 2024-01-12 19:03:51 浏览: 166
opencv+python机器视觉图像处理之图像滤波
下面是一个示例代码,用Python对RGB图像进行同态滤波处理:
```python
import cv2
import numpy as np
def homomorphic_filter(image, a, b, cutoff):
# 转换为浮点型
image = np.float64(image)
# 将图像转换为对数域
log_image = np.log1p(image)
# 对数域图像进行傅里叶变换
dft = np.fft.fft2(log_image)
# 创建同态滤波器
rows, cols = image.shape
center_row, center_col = int(rows/2), int(cols/2)
filter = np.zeros((rows, cols), dtype=np.float64)
for i in range(rows):
for j in range(cols):
distance = np.sqrt((i - center_row)**2 + (j - center_col)**2)
filter[i, j] = (b - a) * (1 - np.exp(-cutoff * (distance**2))) + a
# 将滤波器应用于频域图像
filtered_dft = dft * filter
# 反傅里叶变换得到滤波后的图像
filtered_image = np.real(np.fft.ifft2(filtered_dft))
# 将对数域图像转换回原始域
filtered_image = np.expm1(filtered_image)
# 将像素值限制在0到255之间
filtered_image = np.clip(filtered_image, 0, 255)
# 将图像转换为8位无符号整
阅读全文