Comparing two images that are not pixel perfect

流过昼夜 提交于 2019-12-11 17:42:39

问题


I am trying to compare two photos in python that are similar but not picture perfect. I am using the Pillow library (my code is below). The code checks to see how similar two photos are and return a percentage. Update: My code is not returning certain images that to the eye would be similar (example below). Could someone explain why?

Check image similarity

def imageSimilarity(self,i1,i2):
    pairs = zip(i1.getdata(), i2.getdata())
    if len(i1.getbands()) == 1:
        # for gray-scale jpegs
        dif = sum(abs(p1 - p2) for p1, p2 in pairs)
    else:
        dif = sum(abs(c1 - c2) for p1, p2 in pairs for c1, c2 in zip(p1, p2))

    ncomponents = i1.size[0] * i1.size[1] * 3
    return (dif / 255.0 * 100) / ncomponents

Compare images from left to right

  def leftRightReflected(self,figure1,figure2):
        image1 = Image.open(figure1.visualFilename)
        image2 = Image.open(figure2.visualFilename)
        left_right_mirror = image1.transpose(Image.FLIP_LEFT_RIGHT)
        return self.sameImage(left_right_mirror,image2) or self.imageSimilarity(image2,left_right_mirror) < 3

Compare images from top down

def topDownReflected(self,figure1,figure2):
    image1 = Image.open(figure1.visualFilename)
    image2 = Image.open(figure2.visualFilename)
    top_bottom_mirror = image1.transpose(Image.FLIP_TOP_BOTTOM)
    return self.sameImage(top_bottom_mirror,image2) or self.imageSimilarity(image2,top_bottom_mirror) < 3

[

来源:https://stackoverflow.com/questions/46231825/comparing-two-images-that-are-not-pixel-perfect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!