利用OpenCV和Python实现查找图片差异
作者:flyfish1986 时间:2023-01-06 07:22:22
使用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
标签:OpenCV,Python,图片差异
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL数据库中设列的默认值为Now()的介绍
2009-03-06 17:40:00
Flask框架模板渲染操作简单示例
2023-06-07 00:02:45
MySql中表单输入数据出现中文乱码的解决方法
2024-01-22 20:34:13
Javascript脚本获取form和input内容的方法(两种方法)
2024-04-29 13:18:08
![](https://img.aspxhome.com/file/2023/6/132756_0s.png)
Mybatis实现分包定义数据库的原理与过程
2024-01-23 12:33:55
![](https://img.aspxhome.com/file/2023/3/97933_0s.png)
Python如何实现远程方法调用
2022-11-11 20:42:15
mysql8.0.11客户端无法登陆的解决方法
2024-01-17 18:57:52
![](https://img.aspxhome.com/file/2023/3/71133_0s.png)
asp如何在本地机器上创建缓存?
2010-06-18 19:27:00
使用python tkinter开发一个爬取B站直播弹幕工具的实现代码
2021-06-02 19:15:06
![](https://img.aspxhome.com/file/2023/5/81495_0s.png)
element跨分页操作选择详解
2023-07-02 16:38:47
深入MYSQL字符数字转换的详解
2024-01-18 04:20:11
Python Opencv实现图像轮廓识别功能
2023-02-27 12:32:40
![](https://img.aspxhome.com/file/2023/6/70696_0s.jpg)
python3连接mysql获取ansible动态inventory脚本
2024-01-19 23:13:54
如何利用Matplotlib库绘制动画及保存GIF图片
2021-06-08 17:15:40
![](https://img.aspxhome.com/file/2023/7/81677_0s.gif)
php获取qq用户昵称和在线状态(实例分析)
2023-11-14 11:19:22
pip安装提示Twisted错误问题(Python3.6.4安装Twisted错误)
2021-09-26 04:05:50
![](https://img.aspxhome.com/file/2023/7/128447_0s.png)
你是一个职业的页面重构工作者吗?
2008-09-29 12:07:00
MySQL密码忘了怎么办?MySQL重置root密码方法
2024-01-23 02:18:30
在python中将list分段并保存为array类型的方法
2023-11-15 10:18:00
Python三元运算与lambda表达式实例解析
2023-06-14 16:34:09
![](https://img.aspxhome.com/file/2023/9/59149_0s.png)