批量计算两个文件夹中的PSNR和SSIM
时间: 2024-05-16 19:13:03 浏览: 210
要批量计算两个文件夹中的PSNR和SSIM,需要编写一个脚本来自动化这个过程。这里给出一个Python的示例脚本,假设你已经安装了Python和OpenCV库。
```
import cv2
import os
def calculate_psnr_ssim(img1_path, img2_path):
img1 = cv2.imread(img1_path)
img2 = cv2.imread(img2_path)
psnr = cv2.PSNR(img1, img2)
ssim = cv2.SSIM(img1, img2)
return psnr, ssim
def batch_calculate_psnr_ssim(folder1, folder2):
img1_paths = sorted([os.path.join(folder1, filename) for filename in os.listdir(folder1)])
img2_paths = sorted([os.path.join(folder2, filename) for filename in os.listdir(folder2)])
assert len(img1_paths) == len(img2_paths), "Number of images in two folders must be the same"
psnr_sum = 0
ssim_sum = 0
for img1_path, img2_path in zip(img1_paths, img2_paths):
psnr, ssim = calculate_psnr_ssim(img1_path, img2_path)
psnr_sum += psnr
ssim_sum += ssim
psnr_avg = psnr_sum / len(img1_paths)
ssim_avg = ssim_sum / len(img1_paths)
return psnr_avg, ssim_avg
if __name__ == '__main__':
folder1 = 'path/to/folder1'
folder2 = 'path/to/folder2'
psnr_avg, ssim_avg = batch_calculate_psnr_ssim(folder1, folder2)
print("Average PSNR: {:.2f}".format(psnr_avg))
print("Average SSIM: {:.2f}".format(ssim_avg))
```
你需要将`path/to/folder1`和`path/to/folder2`分别替换为两个包含图像文件的文件夹的实际路径。然后运行脚本,它将自动计算两个文件夹中所有相同文件名的图像的平均PSNR和SSIM,并将结果输出到控制台。
阅读全文