Phil B
Phil B

Reputation: 6057

Is there an algorithm to detect the differences between two images?

I'm looking for an algorithm or library that can spot the differences between two images (like in a "find the errors" game) and output the coordinated of the bounding box containing those changes. I'm open to the algorithm being in Python, C, or almost any other language.

Upvotes: 4

Views: 5164

Answers (4)

Phil B
Phil B

Reputation: 6057

Imagemagick's compare (command-line) function does basically this, as you can read about/see examples of here. One constraint though, is that both images must be of the same size and not have been translated/rotated/scaled. If they are not of the same size/orientation/scale, you'll need to take care of that first. OpenCV contains some algorithms for that. You can find a good tutorial on OpenCV functions you could use to rectify the image here.

Upvotes: 0

Diego Catalano
Diego Catalano

Reputation: 689

If you just want to show the differences, so you can use the code below.

FastBitmap original = new FastBitmap(bitmap);
FastBitmap overlay = new FastBitmap(processedBitmap);

//Subtract the original with overlay and just see the differences.
Subtract sub = new Subtract(overlay);
sub.applyInPlace(original);

// Show the results
JOptionPane.showMessageDialog(null, original.toIcon());

For compare two images, you can use ObjectiveFideliy class in Catalano Framework. Catalano Framework is in Java, so you can port this class in another LGPL project.

FastBitmap original = new FastBitmap(bitmap);
FastBitmap reconstructed = new FastBitmap(processedBitmap);

ObjectiveFidelity of = new ObjectiveFidelity(original, reconstructed);

int error = of.getTotalError();
double errorRMS = of.getErrorRMS();
double snr = of.getSignalToNoiseRatioRMS();

//Show the results

Disclaimer: I am the author of this framework, but I thought this would help.

Upvotes: 3

PJSimon
PJSimon

Reputation: 343

"Parse" the two images into multiple smaller images by cropping the original image. The size of each "sub-image" would be the "resolution" of your scanning operation. For example, if the original images are 100 pixels x 100 pixels, you could set the resolution to 10 x 10 and you'd have one hundred 10 x 10 sub-images for each original image. Save the sub-images to disk.

Next, compare each pair of sub-image files, one from each original image. If there is a file size or data difference, then you can mark that "coordinate" as having a difference on the original images.

This algorithm assumes you're not looking for the coordinates of the individual pixel differences.

Upvotes: 2

sinelaw
sinelaw

Reputation: 16563

There are many, suited for different purposes. You could get a start by looking at OpenCV, the free computer vision library with an API in C, C++, and also bindings to Python and many other languages. It can do subtraction easily and also has functions for bounding or grouping sets of points.

Aside from simple image subtraction, one of the specific uses addressed by OpenCV is motion detection or object tracking.

You can ask more specific image-related algorithmic related questions in the Signal Processing stackexchange site.

Upvotes: 2

Related Questions