在python中计算ssim的方法(与Matlab结果一致)
作者:larryli007 时间:2023-08-19 03:33:21
如下代码可以计算输入的两张图像的结构相似度(SSIM),结果与matlab计算结果一致
// An highlighted block
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("Test2_HR.bmp", 0)
img2 = cv2.imread("Test2_LR2.bmp", 0)
ss = calculate_ssim(img1, img2)
print(ss)
总结
以上所述是小编给大家介绍的在python中计算ssim的方法(与Matlab结果一致)网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://blog.csdn.net/larryli007/article/details/103578420
标签:python,计算,ssim
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现简单tftp(基于udp协议)
2021-01-30 15:01:23
![](https://img.aspxhome.com/file/2023/9/114599_0s.jpg)
python中 logging的使用详解
2023-01-11 21:31:14
PyCharm专业最新版2019.1安装步骤(含激活码)
2022-10-21 19:56:06
![](https://img.aspxhome.com/file/2023/7/89217_0s.png)
简介二分查找算法与相关的Python实现示例
2023-05-13 12:16:45
python框架Django实战商城项目之工程搭建过程图文详解
2022-12-16 16:25:57
![](https://img.aspxhome.com/file/2023/6/72046_0s.jpg)
python 矢量数据转栅格数据代码实例
2022-06-12 03:40:14
在Django中动态地过滤查询集的实现
2023-06-15 12:20:15
smarty简单分页的实现方法
2023-11-21 08:59:48
python如何提取xml指定内容
2021-07-02 01:21:10
![](https://img.aspxhome.com/file/2023/9/68899_0s.png)
pytorch fine-tune 预训练的模型操作
2023-05-02 01:05:25
![](https://img.aspxhome.com/file/2023/2/87892_0s.png)
Python 连连看连接算法
2023-10-28 09:12:35
python简单贪吃蛇开发
2021-04-24 18:47:56
jQuery 让人恋恋不舍的秘密
2010-01-20 10:09:00
python常见字符串处理函数与用法汇总
2023-10-19 08:07:11
微信小程序跳转到其他网页(外部链接)的实现方法
2024-04-17 09:56:17
![](https://img.aspxhome.com/file/2023/1/136461_0s.png)
JS+CSS模拟可以无刷新显示内容的留言板实例
2024-04-19 10:17:13
![](https://img.aspxhome.com/file/2023/3/135863_0s.jpg)
Python中函数的参数定义和可变参数用法实例分析
2023-10-04 00:26:30
Go语言二进制文件的读写操作
2023-06-23 09:40:08
Python+pyplot绘制带文本标注的柱状图方法
2023-06-26 16:09:11
![](https://img.aspxhome.com/file/2023/7/118597_0s.jpg)
python为Django项目上的每个应用程序创建不同的自定义404页面(最佳答案)
2022-07-26 19:08:11