Niels Kristian
Niels Kristian

Reputation: 8835

Compare two images, where the one is slightly cropped (zoomed)

Im using the image library rmagick for ruby to compare two images, in order to detect, if they are actually the same image (avoid duplications).

They way I'm usually doing it, is by using the difference method:

require 'rmagick'

img1.difference(img2)
#=> [238.738932291668, 0.001389172567175018, 0.0184313725490196]

The above example is the output of two images which is most certainly the same. This used to work very well for me, until I stumbled into a new scenario, that this method does not handle very well - if the images are the same (even same size), but one of them has been cropped e.g. 10px of the border. Now suddenly, even though the two images look exactly the same to the human eye, the computer will think they are very different, because one of the images has been cropped just a small bit.

Is there anyway I can handle this situation, so I will still detect that it's the same image, even though one of them has been cropped a little?

Upvotes: 4

Views: 338

Answers (1)

Heinrich
Heinrich

Reputation: 820

While your example pictures might look very similar to a human, the raw image data looks very different.

You need some more sophisticated image processing algorithm which does not compare raw data, but which is invariant to operations like transformation, scaling or rotation.

Generally, I would advise you to use a computer vision library like OpenCV; it also has ruby bindings. You might look at so-called Image Moments in this part of the documentation. The normalized central moments nu_ij should be invariant to translation and scaling; if you calculate these for both images and compare them, you should get near-identical values for a slightly cropped image.

Upvotes: 1

Related Questions