ValueError: Unexpected detail coefficient type: <class 'tuple'>. Detail coefficients must be a 3-tuple of arrays as returned by wavedec2. If you are using pywt.array_to_coeffs or pywt.unravel_coeffs, please specify output_format='wavedec2'
时间: 2024-02-15 16:05:25 浏览: 435
Python 3.x 新特性及10大变化
非常抱歉,看来我在修改代码时出现了新的错误。出现 `ValueError: Unexpected detail coefficient type: <class 'tuple'>...` 的错误是因为在量化子带时,遇到了一个非预期的类型,这可能是因为前面的DWT分解没有使用正确的输出格式。请在DWT分解时,将输出格式设置为 'wavedec2',即:
```python
coeffs = pywt.wavedec2(img, wavelet, level=level, mode='symmetric', axis=(-2,-1), output_format='wavedec2')
```
这样就可以避免这个错误了。下面是修改后的完整代码:
```python
import numpy as np
import cv2
import pywt
def dwt_compress(img, wavelet='haar', level=1):
# 转换为浮点数类型
img = np.float32(img)
# DWT分解
coeffs = pywt.wavedec2(img, wavelet, level=level, mode='symmetric', axis=(-2,-1), output_format='wavedec2')
# 将每个子带进行量化
coeffs_list = list(coeffs)
for i in range(1, len(coeffs_list)):
coeffs_list[i] = (np.round(coeffs_list[i][0] / (2 ** (level - i + 1))) * (2 ** (level - i + 1)), coeffs_list[i][1], coeffs_list[i][2])
# DWT重构
img_compress = pywt.waverec2(coeffs_list, wavelet)
# 转换为整数类型
img_compress = np.uint8(img_compress)
return img_compress
# 读取原始图像
img = cv2.imread('test.jpg', cv2.IMREAD_COLOR)
# 对原始图像进行DWT压缩
img_compress = dwt_compress(img, wavelet='haar', level=1)
# 计算PSNR
psnr = cv2.PSNR(img, img_compress)
print('PSNR:', psnr)
```
希望这次能够解决您的问题,再次感谢您的指正!
阅读全文