帮我用python写一个程序,测文件夹低光照增强后所有图片的panr和ssim
时间: 2024-09-08 08:03:47 浏览: 50
要用Python编写一个程序来测量一个文件夹中所有图片的峰值信噪比(PSNR)和结构相似性指数(SSIM),我们可以使用`skimage`和`scikit-image`库中的相关函数。以下是一个简单的示例脚本:
```python
from skimage.metrics import structural_similarity as ssim
from skimage import io
import numpy as np
import os
def calculate_psnr_ssim(original_folder, enhanced_folder):
# 确保两个文件夹中有相同数量的图片
original_images = [os.path.join(original_folder, img) for img in os.listdir(original_folder)]
enhanced_images = [os.path.join(enhanced_folder, img) for img in os.listdir(enhanced_folder)]
if len(original_images) != len(enhanced_images):
print("Error: The number of original and enhanced images do not match.")
return
# 初始化PSNR和SSIM的值
psnrs = []
ssims = []
# 遍历图片,计算PSNR和SSIM
for orig, enh in zip(original_images, enhanced_images):
original = io.imread(orig)
enhanced = io.imread(enh)
# 对于彩色图片,需要转换为灰度图像
if len(original.shape) == 3:
original = original.mean(axis=2)
enhanced = enhanced.mean(axis=2)
# 计算PSNR
mse = np.mean((original - enhanced) ** 2)
if mse == 0:
psnr = 100
else:
psnr = 10 * np.log10(255 ** 2 / mse)
psnrs.append(psnr)
# 计算SSIM
ssim_val = ssim(original, enhanced, multichannel=False)
ssims.append(ssim_val)
# 输出结果
print("PSNR and SSIM values:")
for i in range(len(psnrs)):
print(f"Image {i+1}: PSNR={psnrs[i]:.2f} dB, SSIM={ssims[i]:.2f}")
# 使用示例
calculate_psnr_ssim('path_to_original_folder', 'path_to_enhanced_folder')
```
在这个脚本中,你需要将`'path_to_original_folder'`和`'path_to_enhanced_folder'`替换为你的原始低光照图片文件夹和增强后的图片文件夹路径。脚本会读取两个文件夹中所有的图片文件,并计算它们的PSNR和SSIM值。
请注意,为了确保对比效果,增强后的图片应该和对应的原始图片是同一张图片。此外,如果图片是彩色的,我们会先将它们转换成灰度图像来进行计算,因为PSNR和SSIM通常是针对灰度图像定义的。
在运行这段代码之前,请确保你已经安装了`skimage`和`numpy`库。
阅读全文