Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image comparison algorithm

I'm trying to compare images to each other to find out whether they are different. First I tried to make a Pearson correleation of the RGB values, which works also quite good unless the pictures are a litte bit shifted. So if a have a 100% identical images but one is a little bit moved, I get a bad correlation value.

Any suggestions for a better algorithm?

BTW, I'm talking about to compare thousand of imgages...

Edit: Here is an example of my pictures (microscopic):

im1:

enter image description here

im2:

enter image description here

im3:

enter image description here

im1 and im2 are the same but a little bit shifted/cutted, im3 should be recognized as completly different...

Edit: Problem is solved with the suggestions of Peter Hansen! Works very well! Thanks to all answers! Some results can be found here http://labtools.ipk-gatersleben.de/image%20comparison/image%20comparision.pdf

like image 552
honeymoon Avatar asked Nov 30 '09 10:11

honeymoon


People also ask

What is image matching algorithm?

Image matching techniques are the techniques used to find existence of a pattern within a source image. Matching methods can be classified in two categories i.e. Area based matching techniques and feature based matching techniques.

What is image comparison?

An image comparison system that identifies differences utilizing AI image recognition. This is a system that compares two different data sets such as documents, drawings and photos and extracts any image differences between them.

How do you compare two images in image processing?

Compare the pictures pixel for pixel, count the matches and the non matches. If they are within a certain threshold of error, you have a match. Otherwise, you could try reducing the resolution up to a certain point and see if the probability of a match improves.

How do you compare images?

Click on the File > New > Image option. Go to the File > Open option and select two or three images that you want to compare. Press the Compare button to view differences between images.


1 Answers

A similar question was asked a year ago and has numerous responses, including one regarding pixelizing the images, which I was going to suggest as at least a pre-qualification step (as it would exclude very non-similar images quite quickly).

There are also links there to still-earlier questions which have even more references and good answers.

Here's an implementation using some of the ideas with Scipy, using your above three images (saved as im1.jpg, im2.jpg, im3.jpg, respectively). The final output shows im1 compared with itself, as a baseline, and then each image compared with the others.

>>> import scipy as sp >>> from scipy.misc import imread >>> from scipy.signal.signaltools import correlate2d as c2d >>> >>> def get(i): ...     # get JPG image as Scipy array, RGB (3 layer) ...     data = imread('im%s.jpg' % i) ...     # convert to grey-scale using W3C luminance calc ...     data = sp.inner(data, [299, 587, 114]) / 1000.0 ...     # normalize per http://en.wikipedia.org/wiki/Cross-correlation ...     return (data - data.mean()) / data.std() ... >>> im1 = get(1) >>> im2 = get(2) >>> im3 = get(3) >>> im1.shape (105, 401) >>> im2.shape (109, 373) >>> im3.shape (121, 457) >>> c11 = c2d(im1, im1, mode='same')  # baseline >>> c12 = c2d(im1, im2, mode='same') >>> c13 = c2d(im1, im3, mode='same') >>> c23 = c2d(im2, im3, mode='same') >>> c11.max(), c12.max(), c13.max(), c23.max() (42105.00000000259, 39898.103896795357, 16482.883608327804, 15873.465425120798) 

So note that im1 compared with itself gives a score of 42105, im2 compared with im1 is not far off that, but im3 compared with either of the others gives well under half that value. You'd have to experiment with other images to see how well this might perform and how you might improve it.

Run time is long... several minutes on my machine. I would try some pre-filtering to avoid wasting time comparing very dissimilar images, maybe with the "compare jpg file size" trick mentioned in responses to the other question, or with pixelization. The fact that you have images of different sizes complicates things, but you didn't give enough information about the extent of butchering one might expect, so it's hard to give a specific answer that takes that into account.

like image 184
Peter Hansen Avatar answered Sep 22 '22 00:09

Peter Hansen