利用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实例一个类背后发生了什么
Feb 09 Python
Python 错误和异常代码详解
Jan 29 Python
Python3爬取英雄联盟英雄皮肤大图实例代码
Nov 14 Python
Python面向对象之类和对象属性的增删改查操作示例
Dec 14 Python
Python3 max()函数基础用法
Feb 19 Python
Python中正则表达式的用法总结
Feb 22 Python
python中用logging实现日志滚动和过期日志删除功能
Aug 20 Python
django项目中新增app的2种实现方法
Apr 01 Python
Python 字符串池化的前提
Jul 03 Python
opencv 图像加法与图像融合的实现代码
Jul 08 Python
Python实现文件压缩和解压的示例代码
Aug 12 Python
如何用python清洗文件中的数据
Jun 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
mac下安装nginx和php
2013/11/04 PHP
Zend Framework实现具有基本功能的留言本(附demo源码下载)
2016/03/22 PHP
详解PHP中mb_strpos的使用
2018/02/04 PHP
php5.6.x到php7.0.x特性小结
2019/08/17 PHP
Aster vs KG BO3 第一场2.18
2021/03/10 DOTA
Javascript load Page,load css,load js实现代码
2010/03/31 Javascript
Javascript实现关联数据(Linked Data)查询及注意细节
2013/02/22 Javascript
控制页面按钮在后台执行期间不重复提交的JS方法
2013/06/24 Javascript
Jquery插件easyUi表单验证提交(示例代码)
2013/12/30 Javascript
javascript页面渲染速度测试脚本分享
2014/04/15 Javascript
js随机生成网页背景颜色的方法
2015/02/26 Javascript
javascript实现状态栏中文字动态显示的方法
2015/10/20 Javascript
jquery实现网页的页面平滑滚动效果代码
2015/11/02 Javascript
基于JavaScript实现购物车功能
2017/02/07 Javascript
在javaScript中检测数据类型的几种方式小结
2017/03/04 Javascript
js弹出窗口简单实现代码
2017/03/22 Javascript
详解nodejs的express如何自动生成项目框架
2017/07/12 NodeJs
Parcel.js + Vue 2.x 极速零配置打包体验教程
2017/12/24 Javascript
前端Vue项目详解--初始化及导航栏
2019/06/24 Javascript
详解vue组件之间的通信
2020/08/30 Javascript
[36:41]完美世界DOTA2联赛循环赛FTD vs Magma第一场 10月30日
2020/10/31 DOTA
python中实现定制类的特殊方法总结
2014/09/28 Python
python实现发送和获取手机短信验证码
2016/01/15 Python
Python正则表达式知识汇总
2017/09/22 Python
python创建列表和向列表添加元素的实现方法
2017/12/25 Python
Python基于高斯消元法计算线性方程组示例
2018/01/17 Python
python实现嵌套列表平铺的两种方法
2018/11/08 Python
解决Tensorboard可视化错误:不显示数据 No scalar data was found
2020/02/15 Python
Python调用接口合并Excel表代码实例
2020/03/31 Python
基于FME使用Python过程图解
2020/05/13 Python
L*SPACE官网:比基尼、泳装和度假服装
2019/03/18 全球购物
英国在线滑雪板和冲浪商店:The Board Basement
2020/01/11 全球购物
Electric官网:美国高级眼镜和配件品牌
2020/06/04 全球购物
“九一八事变纪念日”国旗下讲话稿
2014/09/14 职场文书
工作年限证明模板
2014/11/01 职场文书
JS函数式编程实现XDM一
2022/06/16 Javascript