读取图像man.tiff,实现巴特沃斯高低通滤波及高频提升滤波,并显示原图及处理结果图像。 python代码
时间: 2024-10-05 09:01:31 浏览: 34
在Python中,我们可以使用`skimage`库来进行图像操作,包括读取、滤波等。这里是一个简单的例子,展示如何读取`man.tiff`图像文件,然后应用巴特沃斯滤波(低通或高通)以及高频提升滤波,并显示原始图像和处理后的结果。
```python
from skimage import io, filters
# 1. 读取图像
img = io.imread('man.tiff', as_gray=True) # 假设图片是灰度图像
print("Original Image Shape:", img.shape)
# 2. 巴特沃斯滤波
def apply_butterworth(img, lowcut=0.1, highcut=0.3, order=5):
nyquist_freq = 0.5 * img.shape[1] / img.shape[0]
low = lowcut * nyquist_freq
high = highcut * nyquist_freq
bandpass_filter = filters.butter(order, [low, high], btype='band')
filtered_img = np.real(filters.convolve2d(img, bandpass_filter))
return filtered_img
# 3. 高频提升滤波(可以理解为反低通)
def enhance_high_frequency(filtered_img, cutoff=0.2):
highpass_filter = filters.highpass(cutoff)
enhanced_img = np.abs(np.fft.ifft2(np.fft.fft2(filtered_img) * highpass_filter))
return enhanced_img
# 应用滤波并显示
filtered_img = apply_butterworth(img)
enhanced_img = enhance_high_frequency(filtered_img)
# 显示原图和处理后的图像
fig, axs = plt.subplots(1, 2, figsize=(8, 4))
axs[0].imshow(img, cmap='gray')
axs[0].set_title('Original Image')
axs[1].imshow(enhanced_img, cmap='gray')
axs[1].set_title('Enhanced Image with High Frequency Boost')
for ax in axs:
ax.axis('off')
plt.show()
阅读全文