我想要PSNR和SSIM的值,再加一个批处理功能
时间: 2024-04-15 20:29:51 浏览: 81
好的,我已经更新了代码以计算PSNR和SSIM的值,并添加了批处理功能,可以一次性处理多个红外图像。请注意,你需要安装`opencv-python`和`scikit-image`库来运行代码。以下是更新后的代码:
```python
import cv2
import numpy as np
from skimage.metrics import structural_similarity as ssim
def calculate_psnr(image1, image2):
mse = np.mean((image1 - image2) ** 2)
if mse == 0:
return float('inf')
return 20 * np.log10(255.0 / np.sqrt(mse))
def process_image(image_path):
# 读取红外图像
image = cv2.imread(image_path, 0)
# 中值滤波
median_filtered = cv2.medianBlur(image, 5)
# 均值滤波
mean_filtered = cv2.blur(image, (5, 5))
# 高斯滤波
gaussian_filtered = cv2.GaussianBlur(image, (5, 5), 0)
# 小波滤波
coeffs2 = pywt.dwt2(image, 'haar')
cA, (cH, cV, cD) = coeffs2
wavelet_filtered = pywt.idwt2((cA*0, (cH, cV, cD)), 'haar')
# 计算PSNR和SSIM的值
psnr_median = calculate_psnr(image, median_filtered)
psnr_mean = calculate_psnr(image, mean_filtered)
psnr_gaussian = calculate_psnr(image, gaussian_filtered)
psnr_wavelet = calculate_psnr(image, wavelet_filtered)
ssim_median = ssim(image, median_filtered)
ssim_mean = ssim(image, mean_filtered)
ssim_gaussian = ssim(image, gaussian_filtered)
ssim_wavelet = ssim(image, wavelet_filtered)
# 输出结果
print(f"Image: {image_path}")
print("PSNR Values:")
print(f"Median Filtered: {psnr_median:.2f}")
print(f"Mean Filtered: {psnr_mean:.2f}")
print(f"Gaussian Filtered: {psnr_gaussian:.2f}")
print(f"Wavelet Filtered: {psnr_wavelet:.2f}")
print("SSIM Values:")
print(f"Median Filtered: {ssim_median:.4f}")
print(f"Mean Filtered: {ssim_mean:.4f}")
print(f"Gaussian Filtered: {ssim_gaussian:.4f}")
print(f"Wavelet Filtered: {ssim_wavelet:.4f}")
print("")
# 批处理函数
def batch_process_images(image_folder):
import os
for filename in os.listdir(image_folder):
if filename.endswith(".jpg") or filename.endswith(".png"):
image_path = os.path.join(image_folder, filename)
process_image(image_path)
# 执行批处理
batch_process_images('images')
```
请将红外图像放在名为`images`的文件夹中,并确保它们的文件扩展名是`.jpg`或`.png`。代码将遍历文件夹中的所有图像,并对每个图像进行滤波和计算PSNR和SSIM的值。结果将打印到控制台中。希望这次满足你的需求!
阅读全文