把代码全部整合成一起给我
时间: 2024-11-04 07:10:47 浏览: 18
当然可以。这里是整个代码,包含了图像读取、灰度转换、DCT压缩、小波分解与融合,以及图像的显示和保存(如果你想要的话)。请注意,由于你提到的“脸”系列图片(`face1~face8`)的具体路径需要你自己提供,这里假设它们都存在当前工作目录下。另外,关于小波融合的部分,这里简单地使用了系数相加作为例子,你可以根据需要选择其他融合方法。
```python
import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import dct, idct
from pywt import wavedec2, waverec2
# 读取图像列表
image_files = ['face1.jpg', 'face2.jpg'] # 替换成你实际的脸图片文件名
images = [cv2.imread(file, cv2.IMREAD_GRAYSCALE) for file in image_files]
# 小波分解函数
def perform_wavelet_decomposition(image, wavelet_name='haar', level=2):
return wavedec2(image, wavelet_name, level=level)
# 融合小波系数函数,这里简单采用平均
def fuse_coefficients(coeffs_list):
fused_coeffs = np.mean([coeffs[0] for coeffs in coeffs_list], axis=0)
return fused_coeffs
# 用于组合DCT和小波的处理流程
def process_images(images, dct_threshold=60, wavelet_name='haar', level=2):
processed_images = []
for img in images:
# DCT处理
gray_in = img.copy()
h, w = gray_in.shape
DCT_I = dct(dct(gray_in.T, norm='ortho').T, norm='ortho')
# 压缩 DCT
FDCT_I = np.zeros((h, w))
FDCT_I[:dct_threshold, :dct_threshold] = DCT_I[:dct_threshold, :dct_threshold]
# 逆DCT
gray_out_dct = np.abs(idct(idct(FDCT_I.T, norm='ortho').T, norm='ortho'))
# 小波处理
coeffs = perform_wavelet_decomposition(gray_out_dct, wavelet_name, level)
# 融合系数
fused_coeffs = fuse_coefficients([coeffs])
# 重构图像
reconstructed_img = waverec2(fused_coeffs, wavelet_name)
processed_images.append(reconstructed_img)
return processed_images
# 执行处理
processed_images = process_images(images, dct_threshold=60, wavelet_name='haar', level=2)
# 显示原图和处理后的图像
fig, axs = plt.subplots(nrows=len(images), ncols=2, figsize=(10, len(images)*5))
for i, (img, proc_img) in enumerate(zip(images, processed_images)):
axs[i, 0].imshow(img, cmap='gray')
axs[i, 0].set_title(f"原图 {i+1}")
axs[i, 0].axis('off')
axs[i, 1].imshow(proc_img, cmap='gray')
axs[i, 1].set_title(f"DCT & 小波融合后的图 {i+1}")
axs[i, 1].axis('off')
plt.tight_layout()
plt.show()
# 保存结果
if save_images:
for i, proc_img in enumerate(processed_images):
cv2.imwrite(f'reconstructed_{i+1}.jpg', proc_img)
```
阅读全文