利用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使用百度API上传文件到百度网盘代码分享
Nov 08 Python
完美解决在oj中Python的循环输入问题
Jun 25 Python
对python中两种列表元素去重函数性能的比较方法
Jun 29 Python
python实现二维数组的对角线遍历
Mar 02 Python
Django ORM 自定义 char 类型字段解析
Aug 09 Python
TensorFlow命名空间和TensorBoard图节点实例
Jan 23 Python
40个你可能不知道的Python技巧附代码
Jan 29 Python
opencv+python实现鼠标点击图像,输出该点的RGB和HSV值
Jun 02 Python
关于tf.matmul() 和tf.multiply() 的区别说明
Jun 18 Python
Python 必须了解的5种高级特征
Sep 10 Python
Python 列表反转显示的四种方法
Nov 16 Python
解决python存数据库速度太慢的问题
Apr 23 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入门
2006/10/09 PHP
php中利用explode函数分割字符串到数组
2014/02/08 PHP
php smarty模板引擎的6个小技巧
2014/04/24 PHP
PHP中开启gzip压缩的2种方法
2015/01/31 PHP
php文件上传后端处理小技巧
2016/05/22 PHP
laravel 5异常错误:FatalErrorException in Handler.php line 38的解决
2017/10/12 PHP
lib.utf.js
2007/08/21 Javascript
JQuery获取元素文档大小、偏移和位置和滚动条位置的方法集合
2010/01/12 Javascript
jQuery的实现原理的模拟代码 -4 重要的扩展函数 extend
2010/08/03 Javascript
jQuery代码优化 事件委托篇
2011/11/01 Javascript
javascript中Object使用详解
2015/01/26 Javascript
JS实现的网页倒计时数字时钟效果
2015/03/02 Javascript
IE6兼容透明背景图片及解决方案
2015/08/19 Javascript
js图片跟随鼠标移动代码
2015/11/26 Javascript
jQuery实现6位数字密码输入框
2016/12/29 Javascript
js html实现计算器功能
2018/11/13 Javascript
详解vue项目中使用token的身份验证的简单实践
2019/03/08 Javascript
js使用文件流下载csv文件的实现方法
2019/07/15 Javascript
小程序点餐界面添加购物车左右摆动动画
2020/09/23 Javascript
Python实现学校管理系统
2018/01/11 Python
Python实现修改文件内容的方法分析
2018/03/25 Python
对python中xlsx,csv以及json文件的相互转化方法详解
2018/12/25 Python
python 二维矩阵转三维矩阵示例
2019/11/30 Python
Python 读取WAV音频文件 画频谱的实例
2020/03/14 Python
Pytorch 使用CNN图像分类的实现
2020/06/16 Python
Python 解析简单的XML数据
2020/07/24 Python
Pytest如何使用skip跳过执行测试
2020/08/13 Python
在pycharm创建scrapy项目的实现步骤
2020/12/01 Python
HTML5拖拽文件到浏览器并实现文件上传下载功能代码
2013/06/06 HTML / CSS
美国著名童装品牌:OshKosh B’gosh
2016/08/05 全球购物
Ralph Lauren法国官网:美国高品味时装品牌
2017/12/08 全球购物
幼儿园毕业寄语
2014/04/03 职场文书
干部考核工作总结
2015/08/12 职场文书
小学语文继续教育研修日志
2015/11/13 职场文书
PHP控制循环操作的时间
2021/04/01 PHP
Python竟然能剪辑视频
2021/05/25 Python