利用OpenCV和Python实现查找图片差异

 更新时间:2019年12月19日 10:17:58   作者:flyfish1986  
今天小编就为大家分享一篇利用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”)

以上这篇利用OpenCV和Python实现查找图片差异就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • ptyhon实现sitemap生成示例

    ptyhon实现sitemap生成示例

    这篇文章主要介绍了ptyhon实现sitemap生成示例,需要的朋友可以参考下
    2014-03-03
  • Python写入MySQL数据库的三种方式详解

    Python写入MySQL数据库的三种方式详解

    Python 读取数据自动写入 MySQL 数据库,这个需求在工作中是非常普遍的,主要涉及到 python 操作数据库,读写更新等。本文总结了Python写入MySQL数据库的三种方式,需要的可以参考一下
    2022-06-06
  • python对一个数向上取整的实例方法

    python对一个数向上取整的实例方法

    在本篇文章中小编给大家整理了关于python对一个数向上取整的实例方法,需要的朋友们可以跟着学习下。
    2020-06-06
  • 从Pyspark UDF调用另一个自定义Python函数的方法步骤

    从Pyspark UDF调用另一个自定义Python函数的方法步骤

    PySpark,通常称为Apache Spark的Python API,是为分布式数据处理而创建的,使用UDF,可以扩展和定制 PySpark 的功能以满足某些需求,在本文中,我们将学习如何从Pyspark UDF调用另一个自定义Python函数,需要的朋友可以参考下
    2023-11-11
  • Python 图形绘制详细代码(一)

    Python 图形绘制详细代码(一)

    这篇文章主要介绍了Python 图形绘制详细代码,文章主要从最简单图像的开始,在同一图上绘制两条或多条线一些简单操作,想了解的小伙伴可以学习一下,希望对你的学习有所帮助
    2021-12-12
  • linecache模块加载和缓存文件内容详解

    linecache模块加载和缓存文件内容详解

    这篇文章主要介绍了linecache模块加载和缓存文件内容详解,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • Python编程如何在递归函数中使用迭代器

    Python编程如何在递归函数中使用迭代器

    今天下午想要复现一下学长的recursion file,想模仿源码里的精髓:迭代器遇到了bug,花了一两个小时才解决。现总结如下,有需要的朋友也可借鉴参考下
    2021-09-09
  • 使用Python实现MP4转GIF

    使用Python实现MP4转GIF

    在日常生活中,我们经常会遇到需要将 MP4 文件转换为 GIF 文件的需求,本文将介绍一种使用 Python 实现 MP4 转 GIF 程序的方法,这种方法简单易学,而且完全免费,需要的可以参考下
    2023-12-12
  • Python装饰器用法与知识点小结

    Python装饰器用法与知识点小结

    这篇文章主要介绍了Python装饰器用法与知识点,总结分析了Python 装饰器的基本概念、原理、用法与操作注意事项,需要的朋友可以参考下
    2020-03-03
  • Python数据读写之Python读写CSV文件

    Python数据读写之Python读写CSV文件

    这篇文章主要介绍了Python数据读写之Python读写CSV文件,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下
    2022-06-06

最新评论