Python3通过OpenCV对比图片相似度

本文详细介绍了如何在Mac环境下使用Homebrew安装OpenCV,并通过Python进行图片对比,包括环境配置步骤、运行命令及使用skimage库进行SSIM计算的具体代码。

一、环境配置

参考:https://blog.youkuaiyun.com/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")

图片资源:在这里插入图片描述
在这里插入图片描述
测试结果:

SSIM: 0.36454305667390974
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值