利用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标准库os.path包、glob包使用实例
Nov 25 Python
Python 3.x 新特性及10大变化
Jun 12 Python
Python基于回溯法子集树模板解决找零问题示例
Sep 11 Python
python获取多线程及子线程的返回值
Nov 15 Python
Python实现时钟显示效果思路详解
Apr 11 Python
Python中的单继承与多继承实例分析
May 10 Python
flask框架视图函数用法示例
Jul 19 Python
浅谈Python 多进程默认不能共享全局变量的问题
Jan 11 Python
Python numpy线性代数用法实例解析
Nov 15 Python
python爬虫爬取笔趣网小说网站过程图解
Nov 18 Python
TensorFlow的自动求导原理分析
May 26 Python
Python 装饰器(decorator)常用的创建方式及解析
Apr 24 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 数组的指针操作实现代码
2011/02/08 PHP
DISCUZ在win2003环境下 Unable to access ./include/common.inc.php in... 的问题终极解决方案
2011/11/21 PHP
简单实用的.net DataTable导出Execl
2013/10/28 PHP
php用户注册时常用的检验函数实例总结
2014/12/22 PHP
php通过curl模拟登陆DZ论坛
2015/05/11 PHP
php生成txt文件实例代码介绍
2016/04/28 PHP
yii2.0数据库迁移教程【多个数据库同时同步数据】
2016/10/08 PHP
php自动加载代码实例详解
2021/02/26 PHP
用JQuery 实现的自定义对话框
2007/03/24 Javascript
JQuery this 和 $(this) 的区别
2009/08/23 Javascript
jquery ready函数、css函数及text()使用示例
2013/09/27 Javascript
JavaScript截取字符串的Slice、Substring、Substr函数详解和比较
2014/03/20 Javascript
js实现iframe跨页面调用函数的方法
2014/12/13 Javascript
JAVASCRIPT代码编写俄罗斯方块网页版
2015/11/26 Javascript
js简单实现图片延迟加载的方法
2016/07/19 Javascript
Angular实现较为复杂的表格过滤,删除功能示例
2017/12/23 Javascript
Spring Boot/VUE中路由传递参数的实现代码
2018/03/02 Javascript
javascript中undefined的本质解析
2019/07/31 Javascript
mpvue实现小程序签到金币掉落动画(api实现)
2019/10/17 Javascript
微信小程序实现Swiper轮播图效果
2019/11/22 Javascript
Python跨文件全局变量的实现方法示例
2017/12/10 Python
Python lambda函数基本用法实例分析
2018/03/16 Python
Django1.9 加载通过ImageField上传的图片方法
2018/05/25 Python
Python Unittest根据不同测试环境跳过用例的方法
2018/12/16 Python
Python实现html转换为pdf报告(生成pdf报告)功能示例
2019/05/04 Python
python time.sleep()是睡眠线程还是进程
2019/07/09 Python
python 动态调用函数实例解析
2019/10/21 Python
基于Python脚本实现邮件报警功能
2020/05/20 Python
如何利用Python识别图片中的文字
2020/05/31 Python
tensorflow基于CNN实战mnist手写识别(小白必看)
2020/07/20 Python
北美女性服装零售连锁店:maurices
2019/06/12 全球购物
体育专业个人求职信范文
2013/12/27 职场文书
不假外出检讨书
2014/01/27 职场文书
环保标语大全
2014/06/12 职场文书
十八大宣传标语
2014/10/09 职场文书
员工工作心得体会
2019/05/07 职场文书