Python3通过OpenCV对比图片相似度

一、环境配置

参考:https://blog.****.net/willduan1/article/details/53898440
环境:Python3.7
运行命令:

brew install opencv3 --with-python3 --c++11 --with-contrib

接着运行:

brew link --force opencv3

现在测试下,结果如下:

$ python3
Python 3.7.0 (default, Jul 23 2018, 20:22:55) 
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> 

二、图片对比

代码如下:

from skimage.measure import compare_ssim
import cv2

class CompareImage():

    def compare_image(self, path_image1, path_image2):

        imageA = cv2.imread(path_image1)
        imageB = cv2.imread(path_image2)

        grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
        grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)

        (score, diff) = compare_ssim(grayA, grayB, full=True)
        print("SSIM: {}".format(score))
        return score


compare_image = CompareImage()
compare_image.compare_image("1.jpg", "2.jpg")

图片资源:Python3通过OpenCV对比图片相似度
Python3通过OpenCV对比图片相似度
测试结果:

SSIM: 0.36454305667390974