python 图片相似度 SSIM

import cv2
import numpy as np
from skimage.metrics import structural_similarity
import imutils


def image_similarity(img1, img2):
    """
    :param img1: 图片1
    :param img2: 图片2
    :return: 返回图片1 2结构相似度
    """
    # load image
    image1 = cv2.imdecode(np.fromfile(img1, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
    image2 = cv2.imdecode(np.fromfile(img2, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
    h1, w1 = image1.shape[:2]
    h2, w2 = image2.shape[:2]
    if h1 != h2 or w1 != w2:
        image2 = cv2.resize(image2, (w1, h1))
    # convert the images to grayscale
    gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
    gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
    # compute the Structural Similarity Index (SSIM) between the two
    # images, ensuring that the difference image is returned
    (score, diff) = structural_similarity(gray1, gray2, full=True)
    # diff = (diff * 255).astype("uint8")
    return score

posted @ 2023-02-01 15:42  不能说的秘密  阅读(808)  评论(0编辑  收藏  举报