Compare the two images where it is cropped slightly (enlarged)

Im using the rmagick image rmagick for ruby ​​to compare two images to detect if they are actually the same image (avoid duplication).

As usual, I do this 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 are certainly the same. This worked very well for me until I came across a new scenario that this method does not work very well - if the images are the same (even the same size), but one of them was cropped, for example. 10px border. Now all of a sudden, although both images look exactly like the human eye, the computer will think that they are very different, because one of the images was cropped just a little.

In any case, I can handle this situation, so I still find that this is the same image, although one of them is cropped a bit?

+4
source share
1 answer

While your sample images may look very human-like, the original image data looks very different.

You need an even more sophisticated image processing algorithm that does not compare the source data, but which is invariant for operations such as transform, scale, or rotation.

Typically, I would advise you to use a computer vision library, such as OpenCV ; it also has ruby bindings . You can see the 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 them for both images and compare them, you should get almost the same values ​​for the slightly cropped image.

+1
source

Source: https://habr.com/ru/post/1500923/


All Articles