利用OpenCV和Python实现查找图片差异
作者:flyfish1986 发布时间:2023-01-06 07:22:22
标签:OpenCV,Python,图片差异
使用OpenCV和Python查找图片差异
flyfish
方法1 均方误差的算法(Mean Squared Error , MSE)
下面的一些表达与《TensorFlow - 协方差矩阵》式子表达式一样的
拟合 误差平方和( 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表达的是一个意思
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
原理
根据参数读取两张图片并转换为灰度:
使用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”)
来源:https://blog.csdn.net/flyfish1986/article/details/79541417
0
投稿
猜你喜欢
- 在python显示图象时,我们用matplotlib模块时会遇到图像色彩失真问题,究竟是什么原因呢,下面就来看看究竟。待显示图像为:impo
- 什么是PHPPHP代表了"超文本处理器",这意味着你必须知道,它是一种服务器端的处理语言,且以HTML的形式出现。它最常
- 一、背景 今天闲着无事,写了一个小小的Python脚本程序,然后给同学炫耀的时候,发现每次都得拉着其他人过来看着自己的电脑屏幕,感觉不是很爽
- Python编程中raise可以实现报出错误的功能,而报错的条件可以由程序员自己去定制。在面向对象编程中,可以先预留一个方法接口不实现,在其
- 接触了Vue模块化开发才发现JavaScript组件化开发的演变一直在继续,以前也没有特别在意这一块内容,写着代码能调试运行不报错就可以了,
- 星期五写了个分类信息的小东东!在数据库里只有ip地址,一般访客不太清楚IP地址来源于哪个城市.如果在表里多一个列保存城市又没有真实性可言.如
- 前提条件:本地已经安装好oracle单实例,能使用plsql developer连接,或者能使用TNS连接串远程连接到oracle集群读取e
- 这篇文章主要介绍了Python类如何定义私有变量,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以
- Select CONCAT( 'drop table ', table_name, ';' ) FROM i
- 在我供职的公司不仅仅拥有Oracle数据库,同时还拥有SQL Server数据库,所以我经常遇见人们向我提两种问题。 第一种通常都是以&qu
- 这一篇复习一下ECMAScript规范中的基础语法,英文好的朋友可以直接阅读官方文档。JavaScript本质上也是一种类C语言,熟悉C语言
- torch.nn.CrossEntropyLoss交叉熵损失本文只考虑基本情况,未考虑加权。torch.nnCrossEntropyLoss
- 核心代码:#!/usr/bin/python#Filename:friendbook.pyimport cPickle as pimport
- 一、打开、关闭文件 语法为open (filevar, filename),其中filevar为文件句柄,或者说是程序中用来代表某文件的代号
- 如下所示:# coding=utf-8# 用来处理数字,大于上限的数字置零f = open("/home/chuwei/桌面/tr
- 在IE6中背景属性加 a 与 a:hover 两者的伪类结合,在正常逻辑下为何不起作用?测试这问题存在IE6及以下浏览器,这问题我经常遇到在
- django在admin后台注册自己创建的数据库表,这样我们就可以在admin后台看到表结构信息,我们就可以在admin后台快速录入表记录信
- 前言什么算是高层的文件操作呢?普通的文件操作,我们一般只涉及创建文件,文件夹以及写入文件等等。假如我现在需要复制一个文件的内容到另一个文件之
- 使用memcache来同步session是还是不错的,当然也可以通过redis来保存session,可以php开启并将Session存储到R
- 一、必备插件🌾Chinese(中文)安装后,按快捷键Ctrl+Shift+P,输入configure languageSettings Sy