Compare images script

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
s8utt
Posts: 4
Joined: 2018-06-19T06:37:22-07:00
Authentication code: 1152

Compare images script

Post by s8utt »

Hello.

I have a standard set of pictures ( say 50 )

I'd like to compare a certain portion of example picture against these standard photos to identify which one it is.

I was thinking of a simple script that could loop through the images until it found the matching image. ( portion of the image )

Suggestions ?

Is there a simple command line that can do the compare and return a 1 or 0 based on the result, I then write a vb script to perform the loop and act on the result.

Or any better alternative ?

Many Thanks
S8utt
s8utt
Posts: 4
Joined: 2018-06-19T06:37:22-07:00
Authentication code: 1152

Re: Compare images script

Post by s8utt »

Sorry please move this to the correct area ( users area ? )
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compare images script

Post by snibgo »

I've moved it to "Users".

What version of IM? On what platform (eg bash, Windows etc).

See the "magick compare" command, and the "-compare" operation of the "magick" command.

I'm unsure what you are comparing. If you have an example picture, and a set of photos, do you want to compare the entire example against each entire picture in the set? Or to a pre-specified crop of each photo in the set? Or do you want to search for the example within each picture in the set?
snibgo's IM pages: im.snibgo.com
s8utt
Posts: 4
Joined: 2018-06-19T06:37:22-07:00
Authentication code: 1152

Re: Compare images script

Post by s8utt »

Thanks for your reply, I'll try to provide more details.

IM version will be windows version.

I dont want to compare the entire picture against the entire pictures in the set.
Its a predefined crop area of the picture against the same crop area of the images in the set.

lets say for example the images are of a pack of playing cards, all 52 cards. The position / size of the card will be the same in the picture. So I can identify the area where the suit and number is found ( top left corner ) and compare this against the same area on the master set.
This would allow the identication of the card even if the middle design of the card changed.

Would you recommend just using IM as a command line and write a seperate script for the loop if..else statements ?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compare images script

Post by snibgo »

Okay, so you have one playing card. You want to crop a certain area from it, and compare to the same crop of each of the 52 cards in the pack. I suppose you want to find the closest. With luck, one will be an exact match.

I guess you don't want to do this job just once, because it would be easier to use human intelligence than a computer. So the strategy is probably to make all the crops first. Then re-use those crops for each individual playing card you want to identify.

The first step is to make the crops. If the 52 cards are in directory "pack\" , named *.png, and you want crops in "crops\" then:

Code: Select all

magick mogrify -path crops\ -crop 10x10+10+10 pack\*.png
Instead of "10x10+10+10", use the required width, height, left and top of the crop.

Then if your sample is sample.png:

Code: Select all

magick sample.png -crop 10x10+10+10 sampcrop.png

magick compare -metric RMSE sampcrop.png crops\abc.png NULL:
... where crops\abc.png is one of the crops. The number returned in parentheses is a score from 0.0 to 1.0. Repeat this (eg in "for" loop) for each card in the pack. The one with the lowest score is the best match.
snibgo's IM pages: im.snibgo.com
s8utt
Posts: 4
Joined: 2018-06-19T06:37:22-07:00
Authentication code: 1152

Re: Compare images script

Post by s8utt »

That is awesome, thanks for your time and I will definately give it a whirl.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare images script

Post by fmw42 »

You can crop in-line with compare:

Code: Select all

magick compare -metric rmse image1 image2 -crop 100x100+100+100 null:
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compare images script

Post by snibgo »

Can you crop just one of the images with "magick compare"? Depending on the workflow and number of comparisons, there may be a performance advantage in doing the crops just once, instead of repeatedly reading the same large card and cropping the same area from it.

Anyhow, there are always alternatives, such as "magick ... -crop ... -compare". Sometimes the trick is to get some code that does what you want, then worry about efficiency later.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare images script

Post by fmw42 »

You can use parenthesis processing to crop only one image or crop each differently within the compare command. But it may be more advantageous to crop all the images ahead of time, so that you do not need to crop them over and over.
Post Reply