利用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实现查找图片差异就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python初学时购物车程序练习实例(推荐)

    Python初学时购物车程序练习实例(推荐)

    下面小编就为大家带来一篇Python初学时购物车程序练习实例(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Python文件与文件夹常见基本操作总结

    Python文件与文件夹常见基本操作总结

    这篇文章主要介绍了Python文件与文件夹常见基本操作,结合实例形式总结分析了Python针对文件与文件夹操作所涉及的常见函数与方法的使用技巧,需要的朋友可以参考下
    2016-09-09
  • pygame实现俄罗斯方块游戏(基础篇3)

    pygame实现俄罗斯方块游戏(基础篇3)

    这篇文章主要介绍了pygame实现俄罗斯方块游戏基础的第3篇,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • Python爬取百度春节祝福语并生成心形词云

    Python爬取百度春节祝福语并生成心形词云

    这篇文章主要介绍了利用Python爬虫爬取百度的春节祝福语,并将其生成心形词云,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起试试
    2022-01-01
  • 4个必学的Python自动化技巧分享

    4个必学的Python自动化技巧分享

    在当今快节奏的工作环境中,自动化是提升效率的重要手段,本文将介绍4个必学的Python自动化技巧,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-12-12
  • Python正则表达式re.sub()用法详解

    Python正则表达式re.sub()用法详解

    re.sub用于替换字符串中的匹配项,下面这篇文章主要给大家介绍了关于Python正则表达式re.sub()用法的相关资料,文中通过实例代码以及图文介绍的非常详细,需要的朋友可以参考下
    2022-09-09
  • python基于pygame实现响应游戏中事件的方法(附源码)

    python基于pygame实现响应游戏中事件的方法(附源码)

    这篇文章主要介绍了python基于pygame实现响应游戏中事件的方法,实例分析了Python基于pygame针对键盘及鼠标事件的响应方法,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • python nonlocal的用法详解

    python nonlocal的用法详解

    这篇文章主要给大家介绍了关于python nonlocal用法的相关资料,最近在python学习中遇到了nonlocal关键字但是感到困惑,于是记录nonlocal关键字用法,需要的朋友可以参考下
    2023-10-10
  • tensorflow之变量初始化(tf.Variable)使用详解

    tensorflow之变量初始化(tf.Variable)使用详解

    今天小编就为大家分享一篇tensorflow之变量初始化(tf.Variable)使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • OpenCV图像缩放resize各种插值方式的比较实现

    OpenCV图像缩放resize各种插值方式的比较实现

    OpenCV提供了resize函数来改变图像的大小,本文主要介绍了OpenCV图像缩放resize各种插值方式的比较实现,分享给大家,感兴趣的可以了解一下
    2021-06-06

最新评论