Compare images and generate list of images which are not same

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
a1singh
Posts: 1
Joined: 2019-10-17T07:58:15-07:00
Authentication code: 1152

Compare images and generate list of images which are not same

Post by a1singh »

Hi Folks,
I have very recently discovered imagemagick and its amazing, and so is the community support.
I am looking for some help on comparing images.
Using windows 10 and the latest version of ImageMagick-7.0.8-68-Q16-x64

What I am trying to achieve:
I have approximately 40,000 images each in two folders, lets call them folder A and folder B, which I need to compare
-The file names in both folders is exactly the same. A -> 1, 2, 3, 4, 5..... B -> 1, 2, 3, 4, 5.....
-All images are in *.tiff format
-I need to compare image A->1 with B->

Data Prep:
I have currently taken a sample of 100 images from each folder to test the comparison, lets call them folder A# and folder B#.
The first step was to normalize the dimensions. Images in folder A# are 2400x2400, whereas the ones in folder B# are 2350x2350
I converted all images in folder A# to 2350x2350 using the following:

Code: Select all

mogrify -resize 2365x2365 *.tiff
then I removed the transparency from all images and converted it to white using:

Code: Select all

mogrify -background white -alpha remove -alpha off *.tiff
Approach #1:

Code: Select all

compare -metric ncc Filepath1.tiff Filepath2.tiff -compose src difference.tiff 2>Output.txt
This gave me 1 in output in some cases, which is a great.
But for most comparisons, it gave me 0.9xxx, even when the images were matching.

Approach #2:
I tried using auto-level in hopes to normalize differences arising from brightness, contrast, etc. using the following code and then ran the comparison again, with a similar result

Code: Select all

mogrify –auto-level *.tiff
Approach #3:
I then converted all images to grayscale, in order to see if the result improved using the following code, but with similar result

Code: Select all

mogrify -colorspace Gray  *.tiff
I have tried other metrics for comparison, such as AE, RMSE, but the result is similar. I have also, tried using FUZZ with AE, it only improves result marginally, not enough to make a clear distinction on the basis of values.

Code: Select all

compare -fuzz 25% -metric ncc Filepath1.tiff Filepath2.tiff -compose src difference.tiff 2>Output.txt
Open Question:
Is there a method to normalize images, so that their differences in composition no longer trigger a difference, but only if the images are acutally different if that makes sense?

Here are sample images:
Image 1A: https://drive.google.com/file/d/1ULv9Vk ... sp=sharing
Image 1B: https://drive.google.com/file/d/1xW4L3k ... sp=sharing
Compare Result: https://drive.google.com/file/d/1Aw_LRk ... sp=sharing
Comparison value: 0.849224

Some side questions:
1. How can I insert an mage inline in the thread here. when I tried to add the above images as [img], it just displayed a broken image icon
2. What is the difference if I write -fuzz before or after metric -AE
3. Is there a way to write 2>output.txt for all comparisons into 1 txt file with the format "filename - result". Currently, I am concatenating different text docs into 1 in order to parse it in excel. I am pretty sure there is a better way
4. How can I run the compare command for all images in two folders in using * operator. Currently, I am building the script in excel for inidvidual files, and then copying them in command line to execute.

Thanks in advance for your help.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare images and generate list of images which are not same

Post by fmw42 »

NCC does image normalization by subtracting the mean and dividing by the standard deviation

1) To insert an image inline in your compare command would need scripting and that is OS dependent. You would have to loop over each image and put the image into the command as a variable.

2) It would not matter so long as both are before the image files in the command line.

3) I think you have to write a script loop for each image. Then run compare and put the results into a variable and then write the filenames and the variables into your text file.

4) You cannot use wild cards in compare that way. I think you have to script loops over each folder.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compare images and generate list of images which are not same

Post by snibgo »

The inputs could be trimmed.

3. We can do things like this:

Code: Select all

f:\web\im>%IMG7%magick r1.png r2.png -metric NCC -format "%t - %[distortion]" -compare info:

r1 - 0.995078
If we ">>out.txt", the results will be appended to out.txt.
snibgo's IM pages: im.snibgo.com
Werty
Posts: 66
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: Compare images and generate list of images which are not same

Post by Werty »

Using small images is better for comparisons, resizing to like 10x10 pixels.

Code: Select all

Convert imageA.tiff -resize 10x10 imageAsmall.tiff
Convert imageB.tiff -resize 10x10 imageBsmall.tiff
then using your above compare command...

Code: Select all

compare -fuzz 25% -metric ncc imageAsmall.tiff imageBsmall.tiff -compose src difference.tiff 2>Output.txt
...gives a value of 1.00653

Using -scale instead of -resize gives 1.00333
Windows 7 user
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare images and generate list of images which are not same

Post by fmw42 »

Just precision issues.
Post Reply