Image comparison algorithm Image comparison algorithm python python

Image comparison algorithm


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.


I have one done this with an image histogram comparison. My basic algorithm was this:

  1. Split image into red, green and blue
  2. Create normalized histograms for red, green and blue channel and concatenate them into a vector (r0...rn, g0...gn, b0...bn) where n is the number of "buckets", 256 should be enough
  3. subtract this histogram from the histogram of another image and calculate the distance

here is some code with numpy and pil

r = numpy.asarray(im.convert( "RGB", (1,0,0,0, 1,0,0,0, 1,0,0,0) ))g = numpy.asarray(im.convert( "RGB", (0,1,0,0, 0,1,0,0, 0,1,0,0) ))b = numpy.asarray(im.convert( "RGB", (0,0,1,0, 0,0,1,0, 0,0,1,0) ))hr, h_bins = numpy.histogram(r, bins=256, new=True, normed=True)hg, h_bins = numpy.histogram(g, bins=256, new=True, normed=True)hb, h_bins = numpy.histogram(b, bins=256, new=True, normed=True)hist = numpy.array([hr, hg, hb]).ravel()

if you have two histograms, you can get the distance like this:

diff = hist1 - hist2distance = numpy.sqrt(numpy.dot(diff, diff))

If the two images are identical, the distance is 0, the more they diverge, the greater the distance.

It worked quite well for photos for me but failed on graphics like texts and logos.


You really need to specify the question better, but, looking at those 5 images, the organisms all seem to be oriented the same way. If this is always the case, you can try doing a normalized cross-correlation between the two images and taking the peak value as your degree of similarity. I don't know of a normalized cross-correlation function in Python, but there is a similar fftconvolve() function and you can do the circular cross-correlation yourself:

a = asarray(Image.open('c603225337.jpg').convert('L'))b = asarray(Image.open('9b78f22f42.jpg').convert('L'))f1 = rfftn(a)f2 = rfftn(b)g =  f1 * f2c = irfftn(g)

This won't work as written since the images are different sizes, and the output isn't weighted or normalized at all.

The location of the peak value of the output indicates the offset between the two images, and the magnitude of the peak indicates the similarity. There should be a way to weight/normalize it so that you can tell the difference between a good match and a poor match.

This isn't as good of an answer as I want, since I haven't figured out how to normalize it yet, but I'll update it if I figure it out, and it will give you an idea to look into.