利用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系列之从文件读取和保存数据
May 23 Python
Python脚本实现格式化css文件
Apr 08 Python
Python算法应用实战之栈详解
Feb 04 Python
Python处理菜单消息操作示例【基于win32ui模块】
May 09 Python
python使用turtle库与random库绘制雪花
Jun 22 Python
Python实现的建造者模式示例
Aug 06 Python
pandas 数据归一化以及行删除例程的方法
Nov 10 Python
Python Django中间件,中间件函数,全局异常处理操作示例
Nov 08 Python
Python模块的制作方法实例分析
Dec 21 Python
Django实现列表页商品数据返回教程
Apr 03 Python
Python unittest单元测试框架及断言方法
Apr 15 Python
pytorch实现手写数字图片识别
May 20 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
雄兵连:天使彦天使彦为爱折翼,彦和炙心同时念动的誓言!
2020/03/02 国漫
PHP+.htaccess实现全站静态HTML文件GZIP压缩传输(一)
2007/02/15 PHP
深入PHP操作MongoDB的技术总结
2013/06/02 PHP
PHP中的类型提示(type hinting)功能介绍
2015/07/01 PHP
Yii框架创建cronjob定时任务的方法分析
2017/05/23 PHP
PHP MVC框架中类的自动加载机制实例分析
2019/09/18 PHP
JavaScript 高级篇之闭包、模拟类,继承(五)
2012/04/07 Javascript
jQuery 数据缓存模块进化史详细介绍
2012/11/19 Javascript
onkeyup,onkeydown和onkeypress的区别介绍
2013/10/21 Javascript
关于function类中定义变量this的简单说明
2016/05/28 Javascript
Bootstrap Table使用方法详解
2016/08/01 Javascript
原生node.js案例--前后台交互
2017/02/20 Javascript
jQuery插件ContextMenu自定义图标
2017/03/15 Javascript
微信小程序 监听手势滑动切换页面实例详解
2017/06/15 Javascript
详解Vue-axios 设置请求头问题
2018/12/06 Javascript
如何写好一个vue组件,老夫的一年经验全在这了(推荐)
2019/05/18 Javascript
基于vue的video播放器的实现示例
2021/02/19 Vue.js
[48:31]完美世界DOTA2联赛PWL S3 DLG vs Phoenix 第二场 12.17
2020/12/19 DOTA
Python Mysql自动备份脚本
2008/07/14 Python
零基础写python爬虫之使用urllib2组件抓取网页内容
2014/11/04 Python
Python 常用的安装Module方式汇总
2017/05/06 Python
简单了解Django ContentType内置组件
2019/07/23 Python
pyqt5中动画的使用详解
2020/04/01 Python
Python3实现飞机大战游戏
2020/04/24 Python
python可迭代对象去重实例
2020/05/15 Python
eVitamins日本:在线购买折扣维生素、补品和草药
2019/04/04 全球购物
Currentbody澳大利亚:美容仪专家
2019/11/11 全球购物
阿联酋最好的手机、电子产品和家用电器网上商店:Eros Digital Home
2020/08/09 全球购物
测量实习生自我鉴定
2013/09/19 职场文书
新年爱情寄语
2014/04/08 职场文书
学雷锋月活动总结
2014/04/25 职场文书
学校副校长四风对照检查材料整改措施
2014/09/25 职场文书
2015新年寄语大全
2014/12/08 职场文书
英文自荐信范文
2015/03/25 职场文书
2015年新农合工作总结
2015/03/30 职场文书
七年级之开学家长寄语35句
2019/09/05 职场文书