Similarity between images

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Posts: 30
Joined: 2013-11-26T17:37:36-07:00
Authentication code: 6789

Similarity between images

Post by novito » 2014-01-23T15:42:57-07:00

I have an algorithm to find the main product image from an e-commerce product page. Next step is: Given a set of images found in the page, I would like to be able to find out the similarity between those images and the product image that I already have.

So basically, my question would be:

How can use ImageMagick in order to find how similar two images are? I am not really interested in if they look similar on the color, but on the shape and how similar the aspect ratio is between those:

So, for example, on this page: ... hite-black, the main product image would be:

And similar images would be: ... p90223.jpg ... p93568.jpg

Right now I am just focusing on finding a way to compare the aspect ratios of the images. Since I need to score those images in regards of how close their aspect ratio is to the main image, I would like that value to be between 0 and 1.

So, if I have a main image that is 100x50, and another one that is 200x100, the score should be high, because their aspect ratio is the same. However, if I have 100x50 and 50x100, the score should be low.

Anyway to find these kind of aspect ratio comparisons in ImageMagick?

User avatar
Posts: 24941
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Similarity between images

Post by fmw42 » 2014-01-23T17:31:19-07:00

Use -fx calculations.

ratio1=`convert image1 -format "%[fx:w/h]" info:`
ratio2=`convert image2 -format "%[fx:w/h]" info:`
ratiocompare=`convert xc: -format "%[fx:$ratio2/$ratio1]" info:`

the closer to 1 the closer the two ratios are the same.


With regard to the shape, you should convert to grayscale and compare the Image moments between two images. This is new to IM

identify -verbose -moments image1
identify -verbose -moments image1

There is new feature for the upcoming IM that will do that comparison for you. I am testing it right now mostly for color images. But you should be able to compare two grayscale images as well, though it may be less robust. I have not yet started to test on grayscale as the algorithm is still in a bit of flux.

compare -metric phash image1 image2 null:

This does a perceptual hash comparison between two image that can be different sizes. It uses the Image Moments described above.

This is available in IM, but I think the algorithm has some bugs. So I would wait for IM

I am in the process of making a document graphing my test results for eight different color images that should be available by early next week.

Post Reply