这是我想做的:
我定期用网络摄像头拍照。就像时间流逝一样。然而,如果没有什么真正的改变,也就是说,图片看起来几乎相同,我不想存储最新的快照。
我想有某种方法可以量化这种差异,我必须根据经验确定一个阈值。
我追求的是简单而不是完美。 我用的是python。
这是我想做的:
我定期用网络摄像头拍照。就像时间流逝一样。然而,如果没有什么真正的改变,也就是说,图片看起来几乎相同,我不想存储最新的快照。
我想有某种方法可以量化这种差异,我必须根据经验确定一个阈值。
我追求的是简单而不是完美。 我用的是python。
当前回答
如何计算这两幅图像的曼哈顿距离呢?得到n*n个值。然后你可以做一些事情,比如行平均,把值减少到n个,然后再用一个函数得到一个值。
其他回答
我特别要解决的问题是如何计算它们是否“足够不同”。我假设你能弄清楚如何一个一个地减去像素。
首先,我将取一堆没有任何变化的图像,并找出任何像素变化的最大量,仅仅是因为捕获的变化、成像系统中的噪声、JPEG压缩工件和照明的每时每刻的变化。也许你会发现,即使没有任何移动,1或2位的差异也是可以预期的。
对于“真实”测试,你需要一个这样的标准:
如果最多P个像素的差异不超过E,则相同。
所以,如果E = 0.02, P = 1000,这可能意味着(大约)如果任何单个像素改变超过5个单位(假设8位图像),或者如果超过1000个像素有任何错误,这将是“不同的”。
这主要是一种很好的“分类”技术,用于快速识别足够接近而不需要进一步检查的图像。“失败”的图像可能更多的是一种更复杂/昂贵的技术,例如,如果相机抖动,或者对光线变化更健壮,就不会产生假阳性。
I run an open source project, OpenImageIO, that contains a utility called "idiff" that compares differences with thresholds like this (even more elaborate, actually). Even if you don't want to use this software, you may want to look at the source to see how we did it. It's used commercially quite a bit and this thresholding technique was developed so that we could have a test suite for rendering and image processing software, with "reference images" that might have small differences from platform-to-platform or as we made minor tweaks to tha algorithms, so we wanted a "match within tolerance" operation.
一个简单的解决方案:
将图像编码为jpeg格式,并寻找文件大小的实质性变化。
我曾经用视频缩略图实现过类似的东西,并且取得了很大的成功和可伸缩性。
您可以使用PIL中的函数来比较两个图像。
import Image
import ImageChops
im1 = Image.open("splash.png")
im2 = Image.open("splash2.png")
diff = ImageChops.difference(im2, im1)
diff对象是一幅图像,其中每个像素都是第二幅图像中该像素的颜色值与第一张图像相减的结果。使用差异图像你可以做几件事。最简单的是diff.getbbox()函数。它会告诉你包含两幅图像之间所有变化的最小矩形。
您也可以使用来自PIL的函数实现这里提到的其他东西的近似。
推土机的距离可能正是你所需要的。 不过,要实时实现它可能有点重。
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,但我确信它也有相关性,不是吗?