Compare images and output the level of difference

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
Elapido
Posts: 42
Joined: 2011-06-10T14:27:28-07:00
Authentication code: 8675308

Compare images and output the level of difference

Post by Elapido »

I use normalize to correct levels in images. Then, I'd like to compare the original image with the processed image and have a numerical string in a text file that shows how different both images are, so as to know if the correction was strong or subtle. I've been trying with the compare command and -dissimilarity-threshold but I can't get any results. Any help? Thanx.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compare images and output the level of difference

Post by snibgo »

Does the compare command work at the command line? Eg

Code: Select all

compare -metric RMSE w.png b.png NULL:
It should give two numbers: an integer and a floating-point. These go to stderr, not stdout, so to put them in a file:

Code: Select all

compare -metric RMSE w.png b.png NULL: 2>diff.txt
(This is Windows syntax, but I think Unix is the same.)

Or you can do work in a convert command, and compare two images:

Code: Select all

convert w.png ( +clone -normalize ) -metric RMSE -format "%%[distortion]" -compare info: >x.txt
(Windows BAT syntax.)
snibgo's IM pages: im.snibgo.com
myspacee
Posts: 153
Joined: 2007-06-14T02:09:35-07:00

Re: Compare images and output the level of difference

Post by myspacee »

Hello,
append a request. Is there any way to obtain differences expressed as a percentage ?
See that is asked before, but not find code to run.

If IM returns changed 'pixel', I can count image 'pixel' and use proportion to obtain %, eg:
changed = 13239
image resolution (800x600) = 480000

100:x=480000:13239

Is correct? can help with code?

thank you,
m.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compare images and output the level of difference

Post by snibgo »

"-metric AE" gives the number of pixels that have changed. To get this as a percentage, you need to calculate in a script. (Because %[distortion] can't be used in fx.) This depends on your language. For example, in Windows BAT:

Code: Select all

for /F "usebackq" %%L in (`%IM%convert ^
  rose: ^
  ^( +clone -normalize ^) ^
  -metric AE ^
  -format "AREA=%%[fx:w*h]\nCHANGED=%%[distortion]" ^
  -compare info:`) ^
do set %%L

for /F "usebackq" %%L in (`%IM%identify ^
  -format "CHANGED_PC=%%[fx:100*%CHANGED%/%AREA%]"
  xc:`) ^
do set %%L
CHANGED_PC is set to 99.3478
snibgo's IM pages: im.snibgo.com
myspacee
Posts: 153
Joined: 2007-06-14T02:09:35-07:00

Re: Compare images and output the level of difference

Post by myspacee »

Thank you for reply snibgo,
very elegant; for ghetto people (like me) :]

Code: Select all

@echo off
echo -------------------------------
echo find diff
echo -------------------------------
compare -metric AE -fuzz 5% 1.png 2.png compare_fuzz.gif 2>diff.txt
set /P diff=<diff.txt

echo -------------------------------
echo pixel count for first image
echo -------------------------------
cconvert 1.png -format "%%[fx:w*h]" info: >pixelcount.txt
set /P pixelcount=<pixelcount.txt


set /A percentage=100-((%diff%*100)/%pixelcount%)


echo resolution  : %pixelcount%
echo differences : %diff%
echo similar to  : %percentage% %%
to use in Windows BAT.

as usually learn a lot, thank you.

m.
Post Reply