473,852 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing bitmap images for differences?

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(Im g1, 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.

May 1 '07 #1
6 5379
la*********@gma il.com schrieb:
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(Im g1, 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...
There is the excellent OpenCV-library from intel which is FOSS and has a
python-binding. Either using swig, or a ctypes-version:

http://wwwx.cs.unc.edu/~gb/wp/blog/2...-using-ctypes/

With that, you can approach your problem. What is usually done is that a
sequence of background images is sampled & an average is built. Then the
actual image is subtracted from that image, with an epsilon to account
for noise.

The result is then converted to a binary image for further processing.
There are some blob-detection-recipes out there.

Another approach is to use the motion-detection parts of the lib.

Diez
May 1 '07 #2
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.
This might get you started.
http://tinyurl.com/7qexl
Louis
May 1 '07 #3

<la*********@gm ail.comwrote in message
news:11******** **************@ h2g2000hsg.goog legroups.com...
|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?

If PIL and the other posted ideas are not enough, another approach is to
convert the image from PIL format to NumPy array format. (pygame has
functions to do this, I believe, since it uses NumPy (actually the older
Numerical Python at present) for its surface arrays.) You can then do most
any calculation you want.

tjr

May 1 '07 #4
On May 1, 3:42 pm, "Diez B. Roggisch" <d...@nospam.we b.dewrote:
With that, you can approach your problem. What is usually done is that a
sequence of background images is sampled & an average is built. Then the
actual image is subtracted from that image, with an epsilon to account
for noise.
Thanks for the tip for an algorithm - I can see how that could work
really nicely (and it gives me some ideas for other things, too!)
Thanks also for the link to the OpenCV bindings. I'll give 'em a try
and see what happens.

Regards,
Matthew.

May 2 '07 #5
On May 1, 7:15 pm, "3c273" <nos...@nospam. comwrote:
This might get you started.http://tinyurl.com/7qexl
Wow, I thought briefly along those lines but then thought "No, it
can't possibly be that easy" :-) It looks like the approach given in
that link could benefit hugely from using numpy to treat the bitmaps
as arrays to speed up processing but that should be pretty
straightforward . I'll give it a try as-is and then look at
optimisation if required. Saying that, and thinking about the itch
I'm trying to scratch here, I think I could also safely ignore the
borders of the image so that would reduce the processing requirements
anyway. Add in a FIFO file buffer so I can do pre/post-event image
capture plus a spot of SMTP mail alerting and the job's done!

Thanks again.
Matthew.

May 2 '07 #6
On May 2, 10:36 am, "lanwrang...@gm ail.com" <lanwrang...@gm ail.com>
wrote:
Thanks for the tip for an algorithm - I can see how that could work
really nicely (and it gives me some ideas for other things, too!)
Thanks also for the link to the OpenCV bindings. I'll give 'em a try
and see what happens.
I did similar stuff for a project at Uni, but for tracking
pedestrians... in (eurgh) Java.
Tad boring, but might help a little bit: http://uni.johnsto.co.uk/crowd/

Unfortunately it only got tested on pre-recorded feeds, not live ones.
The background removal was fun. I think I ended up getting the
background by converting every 5th frame to grayscale, and calculating
the median for each pixel position across the sample. A couple filters
to remove tiny 1 or 2 pixel specks of noise, and then went onto blob
detection for identifying people and tracking their movement across
the entire video.

The same filtering in numpy should be really quite fast indeed!

May 3 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6794
by: Mad Scientist Jr | last post by:
I have a bitmap (32 pixels high, 8192 pixels wide) that contains 255 images, each 32 pixels wide, that I would like to chop up into individual 32x32 bitmap files. Rather than spending hours in Paint or Photoshop I would like to do this programmatically. My code below attempts to load in the original bitmap, crop it at the desired location, and save it to the correct file. I don't think I'm doing this right, I tried messing with different...
8
3338
by: Nathan Sokalski | last post by:
I am trying to write code to rotate a graphic that I have. Here is the code I am currently using: Dim frogbitmap As New Bitmap(Drawing.Image.FromFile(Server.MapPath("images/frog.gif"))) Dim froggraphic As Graphics = Graphics.FromImage(frogbitmap) froggraphic.RotateTransform(90) frogbitmap.Save(Server.MapPath("images/frog2.gif"), Imaging.ImageFormat.Gif)
7
2326
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving, the other in which I close the FileStream afterwards, although both return the same error. Here are the two versions of the code and the errors they each return (NOTE: I rebooted immediately before running each of these versions so that I knew they...
4
13308
by: Andrew | last post by:
Hi, I'm trying to blit small bitmaps onto a larger bitmap, and I've got a few issues wrt positioning and output size. I think my problems are due to DPI differences... My small images are 72 DPI My Large image (which i need to create in code) needs to be 300 DPI.
0
953
by: anchal25 | last post by:
Hi, I am new to this forum and have found out some image comparison algorithms here. But my requirement is to compare two images which are similar but different in size. First I tried to resize the bigger image to the size of the smaller one and then set the resolution of the bigger image to that of the smaller one, but it doesn't work. I wonder if two similar images would point to the same handle or the data content of the two similar...
0
9898
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
10670
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5736
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5934
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4549
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4143
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.