写出ssim的python代码
时间: 2023-09-01 19:10:24 浏览: 120
SSIM的Python实现
5星 · 资源好评率100%
以下是使用Python实现SSIM的示例代码:
```python
import numpy as np
import cv2
def SSIM(img1, img2):
# Compute the mean of the two input images
mu1 = np.mean(img1)
mu2 = np.mean(img2)
# Compute the variance of the two input images
var1 = np.var(img1)
var2 = np.var(img2)
# Compute the covariance of the two input images
cov = np.mean((img1 - mu1) * (img2 - mu2))
# Compute the SSIM value
c1 = (0.01 * 255) ** 2
c2 = (0.03 * 255) ** 2
ssim = ((2 * mu1 * mu2 + c1) * (2 * cov + c2)) / ((mu1 ** 2 + mu2 ** 2 + c1) * (var1 + var2 + c2))
return ssim
# Load two images
img1 = cv2.imread("image1.jpg")
img2 = cv2.imread("image2.jpg")
# Convert the images to gray scale
img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# Compute the SSIM value of the two images
ssim_value = SSIM(img1_gray, img2_gray)
print("The SSIM value of the two images is:", ssim_value)
```
请注意,这只是一个简单的示例代码,并且可能需要根据您的实际情况进行修改。此外,您需要确保已安装必要的Python库,例如OpenCV和NumPy。
阅读全文