利用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修改Excel数据的实例代码
Nov 01 Python
Python设计模式之命令模式简单示例
Jan 10 Python
Python实现识别手写数字 简易图片存储管理系统
Jan 29 Python
Python 在字符串中加入变量的实例讲解
May 02 Python
Flask框架实现给视图函数增加装饰器操作示例
Jul 16 Python
使用 Python 实现文件递归遍历的三种方式
Jul 18 Python
Python实现获取本地及远程图片大小的方法示例
Jul 21 Python
python最小生成树kruskal与prim算法详解
Jan 17 Python
python简单实现AES加密和解密
Mar 28 Python
Python代码使用 Pyftpdlib实现FTP服务器功能
Jul 22 Python
Python操作SQLite/MySQL/LMDB数据库的方法
Nov 07 Python
Python实现实时数据采集新型冠状病毒数据实例
Feb 04 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
zend framework框架中url大小写问题解决方法
2014/08/19 PHP
PHP AjaxForm提交图片上传并显示图片源码
2016/11/29 PHP
PHP实现Unicode编码相互转换的方法示例
2020/11/17 PHP
脚本吧 - 幻宇工作室用到js,超强推荐share.js
2006/12/23 Javascript
ModelDialog JavaScript模态对话框类代码
2011/04/17 Javascript
用jquery模仿的a的title属性的例子
2014/10/22 Javascript
JS实现网页顶部向下滑出的全国城市切换导航效果
2015/08/22 Javascript
封装的dialog插件 基于bootstrap模态对话框的简单扩展
2016/08/10 Javascript
jQuery实现手机版页面翻页效果的简单实例
2016/10/05 Javascript
JS实现Cookie读、写、删除操作工具类示例
2018/08/28 Javascript
使用electron实现百度网盘悬浮窗口功能的示例代码
2018/10/24 Javascript
详解在create-react-app使用less与antd按需加载
2018/12/06 Javascript
vue 使用async写数字动态加载效果案例
2020/07/18 Javascript
[52:03]Secret vs VG 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
Django基础之Model操作步骤(介绍)
2017/05/27 Python
python自动发邮件库yagmail的示例代码
2018/02/23 Python
opencv与numpy的图像基本操作
2019/03/08 Python
Django集成celery发送异步邮件实例
2019/12/17 Python
Jupyter 无法下载文件夹如何实现曲线救国
2020/04/22 Python
Python多线程正确用法实例解析
2020/05/30 Python
Python pandas对excel的操作实现示例
2020/07/21 Python
Python操作dict时避免出现KeyError的几种解决方法
2020/09/20 Python
python中四舍五入的正确打开方式
2021/01/18 Python
50个强大璀璨的CSS3/JS技术运用实例
2010/02/27 HTML / CSS
自我鉴定范文
2013/11/10 职场文书
校园门卫岗位职责
2013/12/09 职场文书
你懂得怎么写自荐信吗?
2013/12/27 职场文书
大学三年计划书范文
2014/04/30 职场文书
给校长的建议书100字
2014/05/16 职场文书
民警个人对照检查剖析材料
2014/09/17 职场文书
逃课检讨书
2015/01/26 职场文书
刑事上诉状范文
2015/05/22 职场文书
地雷战观后感
2015/06/09 职场文书
安全教育第一课观后感
2015/06/17 职场文书
Python turtle编写简单的球类小游戏
2022/03/31 Python
如何用六步教会你使用python爬虫爬取数据
2022/04/06 Python