Comparing bitmap images for differences?

lanwrangler at gmail.com lanwrangler at gmail.com
Tue May 1 09:01:45 EDT 2007


I know it's a long shot but does anyone have any pointers to generic
algorithms - or, even better, Python code - for comparing images and
computing a value for the "difference" between them?

What I want to do is to compare two bitmap images (taken from a
webcam, so I'll likely be using PIL) and get some idea of the
"difference" between them so I can tell if something in the image has
changed, eg, a person has entered the field of view.  I've had a look
at the PIL documentation and all it really told me was how little I
knew about image processing :-) and I couldn't see any recipes in the
Python Cookbook that are aimed at this problem area.  In a perfect
world I'd love a method such as CompareImage(Img1, Img2) which would
give a result of 255 if they're identical and 0 if not one pixel
matches with a sliding scale inbetween but I know I'm probably asking
for a lot there...

Some ideas I've had is, maybe, increasing the contrast on both images
(to take out variation in lighting etc), then compressing the results
to get a hash value and comparing the hash, but that sounds likely to
produce a lot of false positives.  I note that PIL provides a
histogram function for counting pixel colour values which sounds
potentially useful and if no-one's got any better ideas I'll probably
start working in that direction.  Or, maybe, dump the bitmap data into
a numpy array and do some kind of FFT on that but that feels very CPU-
intensive.

Those are my ideas so far but I thought it would be worth asking here
first in case there are some known-good algorithms for doing this kind
of thing rather than me trying to re-invent a wheel that ends up
triangular...

Thanks!
  Matthew.




More information about the Python-list mailing list