利用OpenCV和Python实现查找图片差异


Posted in Python onDecember 19, 2019

使用OpenCV和Python查找图片差异

flyfish

方法1 均方误差的算法(Mean Squared Error , MSE)

利用OpenCV和Python实现查找图片差异

下面的一些表达与《TensorFlow - 协方差矩阵》式子表达式一样的

利用OpenCV和Python实现查找图片差异

拟合 误差平方和( sum of squared errors)

residual sum of squares (RSS), also known as the sum of squared residuals (SSR) or the sum of squared errors of prediction (SSE),
also known as 就我们所说的
RSS, SSR ,SSE表达的是一个意思

利用OpenCV和Python实现查找图片差异

def mse(imageA, imageB):
 # the 'Mean Squared Error' between the two images is the
 # sum of the squared difference between the two images;
 # NOTE: the two images must have the same dimension
 err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
 err /= float(imageA.shape[0] * imageA.shape[1])

 # return the MSE, the lower the error, the more "similar"
 # the two images are
 return err

方法2 SSIM

​structural similarity index measurement (SSIM) system

一种衡量两幅图像结构相似度的新指标,其值越大越好,最大为1。

新建一个Python文件,命名为 image_diff.py

原文

Image Difference with OpenCV and Python

原理

利用OpenCV和Python实现查找图片差异

根据参数读取两张图片并转换为灰度:

使用SSIM计算两个图像之间的差异,这种方法已经在scikit-image 库中实现

在两个图像之间的不同部分绘制矩形边界框。

代码如下 已编译通过

from skimage.measure import compare_ssim
#~ import skimage as ssim
import argparse
import imutils
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--first", required=True,
 help="first input image")
ap.add_argument("-s", "--second", required=True,
 help="second")
args = vars(ap.parse_args())
# load the two input images
imageA = cv2.imread(args["first"])
imageB = cv2.imread(args["second"])
'''
imageA = cv2.imread("E:\\1.png")
imageB = cv2.imread("E:\\2.png")
'''
# convert the images to grayscale
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)

# compute the Structural Similarity Index (SSIM) between the two
# images, ensuring that the difference image is returned
#​structural similarity index measurement (SSIM) system一种衡量两幅图像结构相似度的新指标,其值越大越好,最大为1。

(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM: {}".format(score))

# threshold the difference image, followed by finding contours to
# obtain the regions of the two input images that differ
thresh = cv2.threshold(diff, 0, 255,
 cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
 cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]

# loop over the contours
for c in cnts:
 # compute the bounding box of the contour and then draw the
 # bounding box on both input images to represent where the two
 # images differ
 (x, y, w, h) = cv2.boundingRect(c)
 cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 0, 255), 2)
 cv2.rectangle(imageB, (x, y), (x + w, y + h), (0, 0, 255), 2)

# show the output images
cv2.imshow("Original", imageA)
cv2.imshow("Modified", imageB)
cv2.imshow("Diff", diff)
cv2.imshow("Thresh", thresh)
cv2.waitKey(0)

使用方法

python image_diff.py ?first original.png ?second images/modified.png

如果不想使用参数将参数代码部分直接变成

imageA = cv2.imread(“E:\1.png”) 
imageB = cv2.imread(“E:\2.png”)

以上这篇利用OpenCV和Python实现查找图片差异就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
在Python中使用全局日志时需要注意的问题
May 06 Python
Python基于pygame实现的font游戏字体(附源码)
Nov 11 Python
Python实现控制台进度条功能
Jan 04 Python
剖析Python的Twisted框架的核心特性
May 25 Python
利用python获取当前日期前后N天或N月日期的方法示例
Jul 30 Python
python安装Scrapy图文教程
Aug 14 Python
python正则实现计算器功能
Dec 14 Python
python DataFrame获取行数、列数、索引及第几行第几列的值方法
Apr 08 Python
Django1.9 加载通过ImageField上传的图片方法
May 25 Python
详解Python的数据库操作(pymysql)
Apr 04 Python
python 还原梯度下降算法实现一维线性回归
Oct 22 Python
弄清Pytorch显存的分配机制
Dec 10 Python
Python文本处理简单易懂方法解析
Dec 19 #Python
python类中super() 的使用解析
Dec 19 #Python
在python中计算ssim的方法(与Matlab结果一致)
Dec 19 #Python
用openCV和Python 实现图片对比,并标识出不同点的方式
Dec 19 #Python
Python命令行click参数用法解析
Dec 19 #Python
python3 常见解密加密算法实例分析【base64、MD5等】
Dec 19 #Python
Python定义函数时参数有默认值问题解决
Dec 19 #Python
You might like
WINDOWS服务器安装多套PHP的另类解决方案
2006/10/09 PHP
php抽奖小程序的实现代码
2013/06/18 PHP
PHP中的替代语法简介
2014/08/22 PHP
php将access数据库转换到mysql数据库的方法
2014/12/24 PHP
PHP使用递归方式列出当前目录下所有文件的方法
2015/06/02 PHP
PHP代码判断设备是手机还是平板电脑(两种方法)
2015/10/19 PHP
php用户名的密码加密更安全的方法
2019/06/21 PHP
laravel 模型查询按照whereIn排序的示例
2019/10/16 PHP
javascript add event remove event
2008/04/07 Javascript
让页面上两个div中的滚动条(滑块)同步运动示例
2013/08/07 Javascript
JavaScript中的函数嵌套使用
2015/06/04 Javascript
jQuery根据用户电脑是mac还是pc加载对应样式的方法
2015/06/26 Javascript
js实现温度计时间样式代码分享
2015/08/21 Javascript
常用的Javascript设计模式小结
2015/12/09 Javascript
使用Ajax与服务器(JSON)通信实例
2016/11/04 Javascript
基于jQuery实现照片墙自动播放特效
2017/01/12 Javascript
解析NodeJS异步I/O的实现
2017/04/13 NodeJs
js判断节假日实例代码
2017/12/27 Javascript
使用vue制作探探滑动堆叠组件的实例代码
2018/03/07 Javascript
浅谈Node.js 沙箱环境
2018/05/15 Javascript
vue-router判断页面未登录自动跳转到登录页的方法示例
2018/11/04 Javascript
Node登录权限验证token验证实现的方法示例
2020/05/25 Javascript
[01:20]2018DOTA2亚洲邀请赛总决赛战队Mineski晋级之路
2018/04/07 DOTA
[36:20]完美世界DOTA2联赛PWL S3 access vs Rebirth 第一场 12.17
2020/12/18 DOTA
零基础写python爬虫之使用Scrapy框架编写爬虫
2014/11/07 Python
python实现文件路径和url相互转换的方法
2015/07/06 Python
Python中字符串的常见操作技巧总结
2016/07/28 Python
浅谈Python在pycharm中的调试(debug)
2018/11/29 Python
Python八皇后问题解答过程详解
2019/07/29 Python
python requests更换代理适用于IP频率限制的方法
2019/08/21 Python
Python使用pymysql模块操作mysql增删改查实例分析
2019/12/19 Python
Python 防止死锁的方法
2020/07/29 Python
爱心捐款活动总结
2015/05/09 职场文书
教师研修随笔感言
2015/11/18 职场文书
传单、海报早OUT了,另类传单营销方案送给你!
2019/07/15 职场文书
JS前端使用Canvas快速实现手势解锁特效
2022/09/23 Javascript