利用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中for循环的使用方法
May 14 Python
使用Python中的tkinter模块作图的方法
Feb 07 Python
使用Python & Flask 实现RESTful Web API的实例
Sep 19 Python
Python实现的将文件每一列写入列表功能示例【测试可用】
Mar 19 Python
正确理解Python中if __name__ == '__main__'
Jan 24 Python
对Python中class和instance以及self的用法详解
Jun 26 Python
python 已知三条边求三角形的角度案例
Apr 12 Python
利用Python实现斐波那契数列的方法实例
Jul 26 Python
Python web框架(django,flask)实现mysql数据库读写分离的示例
Nov 18 Python
Python中json.load()和json.loads()有哪些区别
Jun 07 Python
Python游戏开发实例之graphics实现AI五子棋
Nov 01 Python
Python常遇到的错误和异常
Nov 02 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
深入解析php模板技术原理【一】
2008/01/10 PHP
php checkbox复选框值的获取与checkbox默认值输出方法
2010/05/15 PHP
使用Smarty 获取当前日期时间和格式化日期时间的方法详解
2013/06/18 PHP
php读取大文件示例分享(文件操作类)
2014/04/13 PHP
Laravel 5框架学习之日期,Mutator 和 Scope
2015/04/08 PHP
layui数据表格自定义每页条数limit设置
2019/10/26 PHP
JavaScript在IE中“意外地调用了方法或属性访问”
2008/11/19 Javascript
最简单的js图片切换效果实现代码
2011/09/24 Javascript
Js日期选择器并自动加入到输入框中示例代码
2013/08/02 Javascript
JS动态修改iframe高度和宽度的方法
2015/04/01 Javascript
js时间戳转为日期格式的方法
2015/12/28 Javascript
JavaScript中的操作符类型转换示例总结
2016/05/30 Javascript
JS判断日期格式是否合法的简单实例
2016/07/11 Javascript
利用Js+Css实现折纸动态导航效果实例源码
2017/01/25 Javascript
JavaScript手风琴页面制作
2017/05/17 Javascript
JavaScript防止全局变量污染的方法总结
2018/08/02 Javascript
jQuery实现点击图标div循环放大缩小功能
2018/09/30 jQuery
Vue实现的父组件向子组件传值功能示例
2019/01/19 Javascript
python list转dict示例分享
2014/01/28 Python
python爬虫教程之爬取百度贴吧并下载的示例
2014/03/07 Python
Python闭包的两个注意事项(推荐)
2017/03/20 Python
Python 获得13位unix时间戳的方法
2017/10/20 Python
浅析Python语言自带的数据结构有哪些
2019/08/27 Python
PyCharm使用之配置SSH Interpreter的方法步骤
2019/12/26 Python
Python文件操作基础流程解析
2020/03/19 Python
spyder 在控制台(console)执行python文件,debug python程序方式
2020/04/20 Python
keras实现图像预处理并生成一个generator的案例
2020/06/17 Python
Keras模型转成tensorflow的.pb操作
2020/07/06 Python
Python中正则表达式对单个字符,多个字符和匹配边界等使用
2021/01/27 Python
js正则匹配markdown里的图片标签的实现
2021/03/24 Javascript
经典的毕业生自荐信范文
2014/04/14 职场文书
产品开发计划书
2014/04/27 职场文书
搞笑的获奖感言
2014/08/16 职场文书
校园新闻广播稿5篇
2014/10/10 职场文书
小学生节水倡议书
2015/04/29 职场文书
jquery插件实现悬浮的菜单
2021/04/24 jQuery