如何计算两张图片的结构相似度,python代码实现
时间: 2024-05-03 14:20:18 浏览: 64
可以使用均方误差(MSE)或结构相似性(SSIM)来计算图片的结构相似度。以下是Python代码实现:
使用MSE:
```python
import cv2
def mse(imageA, imageB):
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])
return err
# load the images
imageA = cv2.imread("imageA.jpg")
imageB = cv2.imread("imageB.jpg")
# calculate the MSE between the two images
m = mse(imageA, imageB)
print("MSE: ", m)
```
使用SSIM:
```python
import cv2
from skimage.measure import compare_ssim
# load the images
imageA = cv2.imread("imageA.jpg")
imageB = cv2.imread("imageB.jpg")
# convert the images to grayscale
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
# calculate the SSIM between the two images
(score, diff) = compare_ssim(grayA, grayB, full=True)
print("SSIM: ", score)
```
其中,MSE值越小表示两张图片越相似,SSIM值越接近1表示两张图片越相似。
阅读全文