用Python基于小波分解实现图像高频和低频分离的代码
时间: 2024-05-01 16:19:38 浏览: 270
以下为Python基于小波分解实现图像高频和低频分离的代码,供您参考:
```python
import numpy as np
import pywt #需要安装PyWavelets库
#高频和低频分离函数
def wavelet_transform(image, mode='haar', level=1):
coeffs = pywt.wavedec2(image, mode, level=level)
cA, (cH, cV, cD) = coeffs[0], coeffs[1:]
return cA, cH, cV, cD
#图像重构函数
def inverse_wavelet_transform(coeffs, mode='haar'):
image = pywt.waverec2(coeffs, mode=mode)
return image
#测试
import cv2
image = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE)
cA, cH, cV, cD = wavelet_transform(image, mode='haar', level=2)
#低频分量图像
cv2.imshow('cA', cA.astype(np.uint8))
#高频分量图像
cv2.imshow('cH', cH.astype(np.uint8))
cv2.imshow('cV', cV.astype(np.uint8))
cv2.imshow('cD', cD.astype(np.uint8))
#重构图像
image_reconstructed = inverse_wavelet_transform((cA, (cH, cV, cD)), mode='haar')
cv2.imshow('image_reconstructed', image_reconstructed.astype(np.uint8))
cv2.waitKey(0)
```
请注意:该代码仅供参考,实际应用中需根据具体情况进行优化和改进。
阅读全文