比较Python中的图像 [英] Compare Images in Python

查看:122
本文介绍了比较Python中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较两个图像,它们是该软件的屏幕截图.我想检查两个图像是否相同,包括图像中显示的数字和字母.如何做到这一点?

I need to compare two images that are screenshots of a software. I want to check if the two images are identical, including the numbers and letters displayed in the images. How can this be accomplished?

推荐答案

有以下几种方法可以进行正确的比较.

There are following ways to do the proper comparison.

  • 首先是均方根差#

要衡量两个图像的相似程度,可以计算图像之间差异的均方根(RMS)值.如果图像完全相同,则该值为零.下面的函数使用差分函数,然后从所得图像的直方图计算RMS值.

To get a measure of how similar two images are, you can calculate the root-mean-square (RMS) value of the difference between the images. If the images are exactly identical, this value is zero. The following function uses the difference function, and then calculates the RMS value from the histogram of the resulting image.

# Example: File: imagediff.py

import ImageChops
import math, operator

def rmsdiff(im1, im2):
    "Calculate the root-mean-square difference between two images"

    h = ImageChops.difference(im1, im2).histogram()

    # calculate rms
    return math.sqrt(reduce(operator.add,
        map(lambda h, i: h*(i**2), h, range(256))
    ) / (float(im1.size[0]) * im1.size[1]))

  • 另一个是精确比较#
    • Another is Exact Comparison #
    • 确定两个图像是否具有完全相同的内容的最快方法是获取两个图像之间的差,然后计算该图像中非零区域的边界框.如果图像相同,则差异图像中的所有像素均为零,并且边界框函数将返回None.

      The quickest way to determine if two images have exactly the same contents is to get the difference between the two images, and then calculate the bounding box of the non-zero regions in this image. If the images are identical, all pixels in the difference image are zero, and the bounding box function returns None.

      import ImageChops
      
      def equal(im1, im2):
          return ImageChops.difference(im1, im2).getbbox() is None
      

      这篇关于比较Python中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆