比较两个图像/图片,并标记差异 [英] Comparing two images/pictures, and mark the difference

查看:497
本文介绍了比较两个图像/图片,并标记差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习比较两个图像/图片.我发现帖子用python/linux方式比较两个图像非常有用,我对这项技术有一些疑问.

I am learning to compare two images/pictures. I found the post Compare two images the python/linux way is very useful and I have some questions regarding the technique.

问题1:

该帖子显示了比较2张图片/图像的方法.可能最简单的方法是:

The post shows ways to compare 2 pictures/images. Probably the easiest way is:

from PIL import Image
from PIL import ImageChops

im1 = Image.open("file1.jpg")
im2 = Image.open("file2.jpg")

diff = ImageChops.difference(im2, im1).getbbox()

print diff

当我有两张相似的图片并在上面运行时,它会给出结果:

when I have 2 look alike pictures and run above, it give result:

(389, 415, 394, 420)

这是图片上两张图片之间存在差异的位置.所以我的问题是,是否可以在图片上标出差异(例如画一个圆)?

It’s the position on the picture where the difference in 2 pictures lies. So my question is, would it be possible to mark the difference on the picture (for example, draw a circle)?

问题2:

import math, operator
from PIL import Image
def compare(file1, file2):
    image1 = Image.open(file1)
    image2 = Image.open(file2)
    h1 = Image.open("image1").histogram()
    h2 = Image.open("image2").histogram()

    rms = math.sqrt(reduce(operator.add, map(lambda a,b: (a-b)**2, h1, h2))/len(h1))

if __name__=='__main__':
    import sys
    file1 = ('c:\\a.jpg')        # added line
    file2 = ('c:\\b.jpg')        # added line

    file1, file2 = sys.argv[1:]
    print compare(file1, file2)

当我在上面跑步时,出现错误"ValueError:需要多个0值才能解压",问题出在这一行:

When I run above, it gives an error "ValueError: need more than 0 values to unpack", and the problem lies in this line:

file1, file2 = sys.argv[1:]

如何纠正它?我在下面尝试了都没用.

How can I have it corrected? and I tried below it neither works.

    print compare('c:\\a.jpg', 'c:\\b.jpg')

更新

在Matt的帮助下添加了问题.

Update

Added question following Matt's help.

它可以绘制一个矩形来标记两个图像/图片上的差异.当两张图像/图片看上去大体相同时,却散布了一些小斑点.它绘制了一个大矩形,标记了包含所有斑点差异的大区域.有没有办法分别标记差异?

It can draw a rectangle to mark the difference on the two images/pictures. When the two images/pictures looked general the same but there are small spots differences spread. It draws a big rectangle marking the big area include all the spots differences. Is there a way to identically mark the differences individually?

推荐答案

关于第一个问题:

import ImageDraw
draw = ImageDraw.Draw(im2)
draw.rectangle(diff)
im2.show()

关于第二个问题:

错误指出sys.argv包含的值不足,无法分配给file1file2.您需要将要比较的两个文件的名称传递给python脚本(变量sys.arv包含脚本的名称和所有命令行参数):

The error states, that sys.argv does not contain enough values to be assigned to file1 and file2. You need to pass the names of the two files you want to compare to you python script (the variable sys.arv contains the name of your script and all the command line parameters):

python name_of_your_script.py file1.jpg file2.jpg

这篇关于比较两个图像/图片,并标记差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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