利用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 bsddb模块操作Berkeley DB数据库介绍
Apr 08 Python
python通过socket查询whois的方法
Jul 18 Python
python数据结构链表之单向链表(实例讲解)
Jul 25 Python
EM算法的python实现的方法步骤
Jan 02 Python
Zookeeper接口kazoo实例解析
Jan 22 Python
python中利用zfill方法自动给数字前面补0
Apr 10 Python
对Python中plt的画图函数详解
Nov 07 Python
python处理两种分隔符的数据集方法
Dec 12 Python
钉钉群自定义机器人消息Python封装的实例
Feb 20 Python
Python中dict和set的用法讲解
Mar 28 Python
python使用if语句实现一个猜拳游戏详解
Aug 27 Python
pycharm激活方法到2099年(激活流程)
Sep 22 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 escape URL编码
2008/12/10 PHP
php初始化对象和析构函数的简单实例
2014/03/11 PHP
浅析PHP中的 inet_pton 网络函数
2019/12/16 PHP
JavaScript 命名空间 使用介绍
2013/08/29 Javascript
node.js中格式化数字增加千位符的几种方法
2015/07/03 Javascript
javascript连续赋值问题
2015/07/08 Javascript
一个字符串中出现次数最多的字符 统计这个次数【实现代码】
2016/04/29 Javascript
BootStrap智能表单实战系列(九)表单图片上传的支持
2016/06/13 Javascript
Node.js 实现简单的接口服务器的实例代码
2017/05/23 Javascript
小发现之浅谈location.search与location.hash的问题
2017/06/23 Javascript
jQuery实现的点击按钮改变样式功能示例
2018/07/21 jQuery
js实现按钮开关单机下拉菜单效果
2018/11/22 Javascript
Vue自定义表单内容检查rules实例
2020/10/30 Javascript
Python根据文件名批量转移图片的方法
2018/10/21 Python
Django使用paginator插件实现翻页功能的实例
2018/10/24 Python
Python实现将字符串的首字母变为大写,其余都变为小写的方法
2019/06/11 Python
详解Django 时间与时区设置问题
2019/07/23 Python
Python3 使用pillow库生成随机验证码
2019/08/26 Python
pytorch 归一化与反归一化实例
2019/12/31 Python
Python读取VOC中的xml目标框实例
2020/03/10 Python
解决Python3.7.0 SSL低版本导致Pip无法使用问题
2020/09/03 Python
CSS3绘制有活力的链接下划线
2016/07/14 HTML / CSS
ASOS西班牙官网:英国在线时尚和美容零售商
2020/01/10 全球购物
Hobbs官方网站:英国奢华女性时尚服装
2020/02/22 全球购物
Piercing Pagoda官网:耳环、戒指、项链、手链等
2020/09/28 全球购物
美工的岗位职责
2013/11/14 职场文书
入党自我评价范文
2014/02/02 职场文书
一夜的工作教学反思
2014/02/08 职场文书
教师职业道德事迹材料
2014/08/18 职场文书
2015出纳试用期工作总结
2014/12/12 职场文书
教师考核表个人总结
2015/02/12 职场文书
药店营业员岗位职责
2015/04/14 职场文书
培训讲师开场白
2015/06/01 职场文书
关于拾金不昧的感谢信(五篇)
2019/10/18 职场文书
Python爬虫之爬取哔哩哔哩热门视频排行榜
2021/04/28 Python
Python词云的正确实现方法实例
2021/05/08 Python