这是我想做的:

我定期用网络摄像头拍照。就像时间流逝一样。然而,如果没有什么真正的改变,也就是说,图片看起来几乎相同,我不想存储最新的快照。

我想有某种方法可以量化这种差异,我必须根据经验确定一个阈值。

我追求的是简单而不是完美。 我用的是python。


当前回答

下面是我写的一个函数,它以2个图像(文件路径)作为参数,并返回两个图像“像素”组件之间的平均差值。这对我确定视觉上“相等”的图像(当它们不==相等时)非常有效。

(我发现8个是判断图像本质上是否相同的一个很好的限制。)

(如果不添加预处理,图像必须具有相同的尺寸。)

from PIL import Image

def imagesDifference( imageA, imageB ):
    A = list(Image.open(imageA, r'r').convert(r'RGB').getdata())
    B = list(Image.open(imageB, r'r').convert(r'RGB').getdata())
    if (len(A) != len(B)): return -1
    diff = []
    for i in range(0, len(A)):
        diff += [abs(A[i][0] - B[i][0]), abs(A[i][1] - B[i][1]), abs(A[i][2] - B[i][2])]
    return (sum(diff) / len(diff))

其他回答

一个简单的解决方案:

将图像编码为jpeg格式,并寻找文件大小的实质性变化。

我曾经用视频缩略图实现过类似的东西,并且取得了很大的成功和可伸缩性。

我认为你可以简单地计算两幅图像亮度之间的欧几里得距离(即平方根(像素对像素的差异平方和)),如果这低于某个经验阈值,就认为它们相等。你最好包装一个C函数。

你见过寻找相似图像的算法问题吗?请查看相关建议。

我建议对你的框架进行小波变换(我已经写了一个使用Haar变换的C扩展);然后,比较两张图片之间最大(比例)小波因子的索引,你应该得到一个数值相似近似。

I have been having a lot of luck with jpg images taken with the same camera on a tripod by (1) simplifying greatly (like going from 3000 pixels wide to 100 pixels wide or even fewer) (2) flattening each jpg array into a single vector (3) pairwise correlating sequential images with a simple correlate algorithm to get correlation coefficient (4) squaring correlation coefficient to get r-square (i.e fraction of variability in one image explained by variation in the next) (5) generally in my application if r-square < 0.9, I say the two images are different and something happened in between.

这是强大的和快速的在我的实现(Mathematica 7)

这是值得玩转的部分,你感兴趣的图像,并通过裁剪所有的图像到那个小区域,否则一个远离相机但重要的变化将被错过。

我不知道如何使用Python,但我确信它也有相关性,不是吗?

您可以使用PIL中的函数来比较两个图像。

import Image
import ImageChops

im1 = Image.open("splash.png")
im2 = Image.open("splash2.png")

diff = ImageChops.difference(im2, im1)

diff对象是一幅图像,其中每个像素都是第二幅图像中该像素的颜色值与第一张图像相减的结果。使用差异图像你可以做几件事。最简单的是diff.getbbox()函数。它会告诉你包含两幅图像之间所有变化的最小矩形。

您也可以使用来自PIL的函数实现这里提到的其他东西的近似。