输入图片的数据类型是np.uint8。其中ssim()函数是计算单通道的两幅图片的ssim值。

import cv2 import numpy as np def ssim(img1, img2): C1 = (0.01 * 255) ** 2 C2
= (0.03 * 255) ** 2 img1 = img1.astype(np.float64) img2 =
img2.astype(np.float64) kernel = cv2.getGaussianKernel(11, 1.5) window =
np.outer(kernel, kernel.transpose()) mu1 = cv2.filter2D(img1, -1, window)[5:-5,
5:-5] # valid mu2 = cv2.filter2D(img2, -1, window)[5:-5, 5:-5] mu1_sq = mu1 **
2 mu2_sq = mu2 ** 2 mu1_mu2 = mu1 * mu2 sigma1_sq = cv2.filter2D(img1 ** 2, -1,
window)[5:-5, 5:-5] - mu1_sq sigma2_sq = cv2.filter2D(img2 ** 2, -1,
window)[5:-5, 5:-5] - mu2_sq sigma12 = cv2.filter2D(img1 * img2, -1,
window)[5:-5, 5:-5] - mu1_mu2 ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 +
C2)) / ((mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2)) return
ssim_map.mean() def calculate_ssim(img1, img2): '''calculate SSIM the same
outputs as MATLAB's img1, img2: [0, 255] ''' if not img1.shape == img2.shape:
raise ValueError('Input images must have the same dimensions.') if img1.ndim ==
2: return ssim(img1, img2) elif img1.ndim == 3: if img1.shape[2] == 3: ssims =
[] for i in range(3): ssims.append(ssim(img1, img2)) return
np.array(ssims).mean() elif img1.shape[2] == 1: return ssim(np.squeeze(img1),
np.squeeze(img2)) else: raise ValueError('Wrong input image dimensions.') img1
= cv2.imread("1.png", 0) img2 = cv2.imread("2.jpg", 0) img1 = np.array(img1)
img2 = np.array(img2) ss = calculate_ssim(img2, img1) print(ss)
直接调用skimage中的函数
from skimage.measure import compare_ssim print(compare_ssim(img1,
img2,data_range=255,multichannel=True)) from skimage.measure import
compare_ssim,compare_psnr psnr = compare_psnr(img1, img2, 255)

输入图片的格式是tensor时,ssim的计算代码:暂时不会

补充。可以直接图片PIL格式进行计算
def ssim(img1, img2): C1 = (0.01 * 255) ** 2 C2 = (0.03 * 255) ** 2 img1 =
img1.astype(np.float64) img2 = img2.astype(np.float64) kernel =
cv2.getGaussianKernel(11, 1.5) window = np.outer(kernel, kernel.transpose())
mu1 = cv2.filter2D(img1, -1, window)[5:-5, 5:-5] # valid mu2 =
cv2.filter2D(img2, -1, window)[5:-5, 5:-5] mu1_sq = mu1 ** 2 mu2_sq = mu2 ** 2
mu1_mu2 = mu1 * mu2 sigma1_sq = cv2.filter2D(img1 ** 2, -1, window)[5:-5, 5:-5]
- mu1_sq sigma2_sq = cv2.filter2D(img2 ** 2, -1, window)[5:-5, 5:-5] - mu2_sq
sigma12 = cv2.filter2D(img1 * img2, -1, window)[5:-5, 5:-5] - mu1_mu2 ssim_map
= ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) *
(sigma1_sq + sigma2_sq + C2)) return ssim_map.mean() def calculate_ssim(img1,
img2): '''calculate SSIM the same outputs as MATLAB's img1, img2: [0, 255] '''
img1=np.array(img1) img2=np.array(img2) if not img1.shape == img2.shape: raise
ValueError('Input images must have the same dimensions.') if img1.ndim == 2:
return ssim(img1, img2) elif img1.ndim == 3: if img1.shape[2] == 3: ssims = []
for i in range(3): ssims.append(ssim(img1, img2)) return np.array(ssims).mean()
elif img1.shape[2] == 1: return ssim(np.squeeze(img1), np.squeeze(img2)) else:
raise ValueError('Wrong input image dimensions.') if __name__ == "__main__":
img1=Image.open('./fig/baby_bicubic_x2.png')
img2=Image.open('./fig/baby_hr_x2.png') print(calculate_ssim(img1,img2))
 

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信