Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing a portion of 2 jpgs or bmps 2

Status
Not open for further replies.

TomMcL2

Programmer
Apr 4, 2000
41
0
0
US
Hi all,
I need a little help getting started. I want to compare a section of 2 jpegs (that will already be in memory) to find out if there is a substantial difference in the two.

These images will be taken from a camera. The first image will always be the reference image and stored in memory. As the remaining images are loaded into memory I need to compare them to the reference image before writing to disk.

So in essence, I will be using this to detect motion.

If anybody has any ideas on where i should start looking, I would appreciate your help.

Thanks,
Tom
 
Actually I dont need to search ;)
I am writing a program in C++ that records the images from the cameras to an AVI file, but to save disk space I dont want to record unless there is motion.

Thanks
Tom
 
I'm not sure there exist a standard method of comparing images. You should compare each pixel in the bitmap.

Ion Filipski
1c.bmp
 
Hi Tom,

I was just wondering if you found a solution to this problem. I'm doing a similar thing in that i'm trying to compare two images, or frames, by comparing pixel by pixel and where the pixels are the same output a black pixel and where they are different output a white pixel. To show the motion between frames.

Any help would be great, Thanks.

Jon
 
I suppose that when loading JPEGs into memory they are uncompressed to a DIB BITMAP or a device dependent BITMAP Anyway, the idea is that the image is represented in memory as an array of bytes.
What you can do now is to use an algorithm called "The Levenshtein Distance". Basically, the Levenshtein distance between two strings (let's say the sample and the reference) is the number of changes required in the sample string to make it look like the original (reference).

Example:
Sample string: "This is a sarwng"
Reference string: "This is a string"
Changes to make them identical: t i

So, the Levenshtein distance between the two strings above is 2, because you need to make 2 changes in the sample string to make it identical to the reference string.

One (of many) application is in voice recognition software to compare the input data with an internal sample. If the Levenshtein distance between the two datasets is smaller than a value (threshold), then the input data is accepted as being identical with the reference. The reason for accepting the input data even if some differences were found is that the input data can have noise.
You can apply this in your software: the reference data will be a frame you have captured when you are sure that there is no movement in the monitored area. The input data will be a frame captured within the time interval you run the application. So, if the Levenshtein distance between the input (captured) data and the reference data is below a value (which you will set according to your needs, environmental video noise, noise generated by electrical nearby equipments, poor (low quality) cables between the capturing device and the computer, etc..) then it means there was no movement. If the Levenshtein distance is larger than that value, then you have movement and should start recording.

P.S.: The algorithm does not impose that the sample and the reference have the same size (length).

Please consult for a reference on Levenshtein distance algorithm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top