基于OpenCV的灰度图的图片相似度计算

from skimage.metrics import structural_similarity as ssim
import matplotlib.pyplot as plt
import cv2
def picture_recognization(imagname):

    # 读取两张图片
    image1 = cv2.imread('D:/AutoTest/PythonProject/standard_img/' + imagname)
    image2 = cv2.imread('D:/AutoTest/PythonProject/testcase_img/' + imagname)
    pixel_diff = cv2.absdiff(image1, image2)
    gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
    gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
    ssim_score = ssim(gray1, gray2)
    plt.subplot(1, 3, 1)
    plt.imshow(cv2.cvtColor(image1, cv2.COLOR_BGR2RGB))
    plt.title('Image 1')

    plt.subplot(1, 3, 2)
    plt.imshow(cv2.cvtColor(image2, cv2.COLOR_BGR2RGB))
    plt.title('Image 2')

    plt.subplot(1, 3, 3)
    plt.imshow(pixel_diff, cmap='gray')
    plt.title(f'Pixel Difference\nSSIM Score: {ssim_score:.2f}')
    print(ssim_score)
    plt.show()
    return ssim_score

你可能感兴趣的:(UI自动化测试,opencv,python,人工智能)