利用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实现保存网页到本地示例
Mar 16 Python
web.py中调用文件夹内模板的方法
Aug 26 Python
python基于urllib实现按照百度音乐分类下载mp3的方法
May 25 Python
itchat-python搭建微信机器人(附示例)
Jun 11 Python
Python3 venv搭建轻量级虚拟环境的步骤(图文)
Aug 09 Python
用Python实现二叉树、二叉树非递归遍历及绘制的例子
Aug 09 Python
python 读txt文件,按‘,’分割每行数据操作
Jul 05 Python
Python 在 VSCode 中使用 IPython Kernel 的方法详解
Sep 05 Python
python实现简单猜单词游戏
Dec 24 Python
Python中全局变量和局部变量的理解与区别
Feb 07 Python
python urllib和urllib3知识点总结
Feb 08 Python
python中time tzset()函数实例用法
Feb 18 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
oracle资料库函式库
2006/10/09 PHP
php getimagesize 上传图片的长度和宽度检测代码
2010/05/15 PHP
php引用计数器进行垃圾收集机制介绍
2012/09/19 PHP
PHP ? EasyUI DataGrid 资料存的方式介绍
2012/11/07 PHP
PHP类型约束用法示例
2016/09/28 PHP
基于jQuery的表格操作插件
2010/04/22 Javascript
模拟select的代码
2011/10/19 Javascript
解析prototype,JQuery中跳出each循环的方法
2013/12/12 Javascript
jQuery浏览器CSS3特写兼容实例
2015/01/19 Javascript
JQuery点击事件回到页面顶部效果的实现代码
2016/05/24 Javascript
jQuery实现的右下角广告窗体跟随效果示例
2016/09/16 Javascript
适用于手机端的jQuery图片滑块动画
2016/12/09 Javascript
javascript 数据存储的常用函数总结
2017/06/01 Javascript
微信小程序下拉框组件使用方法详解
2018/12/28 Javascript
JS自定义对象创建与简单使用方法示例
2020/01/15 Javascript
JS函数本身的作用域实例分析
2020/03/16 Javascript
React实现阿里云OSS上传文件的示例
2020/08/10 Javascript
[04:51]TI10典藏宝瓶Ⅱ外观视频展示
2020/08/15 DOTA
Python编写登陆接口的方法
2017/07/10 Python
基于Python socket的端口扫描程序实例代码
2018/02/09 Python
python中pip的安装与使用教程
2018/08/10 Python
python实现自动解数独小程序
2019/01/21 Python
python爬虫之快速对js内容进行破解
2019/07/09 Python
python 动态迁移solr数据过程解析
2019/09/04 Python
Python 转换RGB颜色值的示例代码
2019/10/13 Python
python 解决flask 图片在线浏览或者直接下载的问题
2020/01/09 Python
Python实现队列的方法示例小结【数组,链表】
2020/02/22 Python
Pandas将列表(List)转换为数据框(Dataframe)
2020/04/24 Python
解决python和pycharm安装gmpy2 出现ERROR的问题
2020/08/28 Python
洛佩桑酒店官方网站:Lopesan Hotels
2019/04/15 全球购物
两道JAVA笔试题
2016/09/14 面试题
计算机数据库专业职业生涯规划书
2014/02/08 职场文书
民族团结先进个人事迹材料
2014/06/02 职场文书
运动会广播稿200字
2014/10/18 职场文书
2014财务部年度工作总结
2014/12/08 职场文书
2015年信息技术教研组工作总结
2015/07/22 职场文书