批量计算psnr和ssim
时间: 2023-09-02 11:11:39 浏览: 136
图像去噪RIDNet的Pytorch复现代码,包含计算PSNR/SSIM代码以及训练好的模型文件,可以直接用于真实图像去噪
5星 · 资源好评率100%
您可以使用Python中的OpenCV库来计算图像的PSNR和SSIM值。下面是一个示例代码:
```python
import cv2
import os
# 计算两张图像的PSNR和SSIM值
def calculate_psnr_ssim(img1_path, img2_path):
img1 = cv2.imread(img1_path)
img2 = cv2.imread(img2_path)
# 计算PSNR值
mse = cv2.meanSquaredError(img1, img2)
if mse == 0:
psnr = 100
else:
psnr = 20 * cv2.log10(255.0 / cv2.sqrt(mse))
# 计算SSIM值
ssim = cv2.SSIM(img1, img2)
return psnr, ssim
# 批量计算图像的PSNR和SSIM值
def batch_calculate_psnr_ssim(folder_path):
files = os.listdir(folder_path)
results = []
for i in range(len(files)):
for j in range(i + 1, len(files)):
img1_path = os.path.join(folder_path, files[i])
img2_path = os.path.join(folder_path, files[j])
psnr, ssim = calculate_psnr_ssim(img1_path, img2_path)
result = {
"img1": files[i],
"img2": files[j],
"psnr": psnr,
"ssim": ssim
}
results.append(result)
return results
# 示例:批量计算文件夹中所有图像的PSNR和SSIM值
folder_path = "/path/to/folder"
results = batch_calculate_psnr_ssim(folder_path)
print(results)
```
在上面的代码中,`calculate_psnr_ssim`函数计算两张图像的PSNR和SSIM值,`batch_calculate_psnr_ssim`函数批量计算文件夹中所有图像的PSNR和SSIM值。您可以根据自己的需求修改代码,比如修改文件夹路径、输出结果等。
阅读全文