Python 官方文档:入门教程 => 点击学习
背景 SSIM:结构相似度PSNR: 峰值信噪比SSIM和PSNR是图像质量评价领域非常经典的全参考图像质量评价方法。SSIM和PSNR在图像去雾、图像去模糊等领域是常用的性能指标。 代码(详细注释)
以图像去雾作为例子,给定待去雾图像和清晰图像的图片路径,运行以下代码即可实现这个图像对的SSIM和PSNR的计算。
import osimport cv2import numpy as npfrom PIL import Imagefrom skimage.metrics import structural_similarityfrom skimage.metrics import peak_signal_noise_ratioimport warningswarnings.filterwarnings('ignore')clear_img_path = "images/clear/clear_1.png" # 清晰图像路径hazy_img_path = "images/hazy/hazy_1.png" # 待去雾图像路径clear_img = cv2.imread(clear_img_path) # 注意图片路径不要含中文hazy_img = cv2.imread(hazy_img_path)if clear_img.shape[0] != hazy_img.shape[0] or clear_img.shape[1] != hazy_img.shape[1]:pil_img = Image.fromarray(hazy_img)pil_img = pil_img.resize((clear_img.shape[1], clear_img.shape[0])) # 和clear_img的宽和高保持一致hazy_img = np.array(pil_img)# 计算PSNR# PSNR越大,代表着图像质量越好。PSNR = peak_signal_noise_ratio(clear_img, hazy_img)print('PSNR: ', PSNR)# 计算SSIMSSIM = structural_similarity(clear_img, hazy_img, multichannel=True)print('SSIM: ', SSIM)
import osimport cv2import numpy as npfrom PIL import Imagefrom skimage.metrics import structural_similarityfrom skimage.metrics import peak_signal_noise_ratioimport warningswarnings.filterwarnings('ignore')SSIM_list = []PSNR_LIST = []clear_img_path = "images/clear/" # 清晰图像文件夹路径hazy_img_path = "images/hazy/" # 待去雾图像文件夹路径clear_img_names = os.listdir(clear_img_path) # 获取所有的清晰图像文件名hazy_img_names = []# 遍历清晰图像文件名列表,找到对应的待去雾图像文件名for name in clear_img_names: hazy_img_names.append(name[:4] + ".jpg")for i in range(len(clear_img_names)): clear_img = cv2.imread(os.path.join(clear_img_path, clear_img_names[i])) hazy_img = cv2.imread(os.path.join(hazy_img_path, hazy_img_names[i]))if clear_img.shape[0] != hazy_img.shape[0] or clear_img.shape[1] != hazy_img.shape[1]:pil_img = Image.fromarray(hazy_img)pil_img = pil_img.resize((clear_img.shape[1], clear_img.shape[0])) # 和clear_img的宽和高保持一致hazy_img = np.array(pil_img) # 计算PSNR # PSNR越大,代表着图像质量越好。 PSNR = peak_signal_noise_ratio(clear_img, hazy_img) print(i+1, 'PSNR: ', PSNR) PSNR_LIST.append(PSNR) # 计算SSIMSSIM = structural_similarity(clear_img, hazy_img, multichannel=True) print(i+1, 'SSIM: ', SSIM) SSIM_list.append(SSIM)print("average SSIM", sum(SSIM_list)/ len(SSIM_list))print("average PSNR", sum(PSNR_LIST)/ len(PSNR_LIST))
来源地址:https://blog.csdn.net/qq_41813454/article/details/131335270
--结束END--
本文标题: 基于Python:计算两幅图像的SSIM和PSNR(附代码)
本文链接: https://lsjlt.com/news/420753.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0