Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ImageMagick compare with RMSE always return 1 regardless of dissimilarity-threshold?

I'm using ImageMagick to compare files and I want it to return exit code 0. if the images are within some threshold of similarity. However, using metric RMSE and setting dissimilarity-threshold to allow some range of variability, it still returns 1. It only seems to return 0 when I give it 2 identical images.

For example:

    > imageMagick compare -verbose -metric RMSE -dissimilarity-threshold 0.5 new_file.png old_file.png null
    > echo $?

    new_file.png PNG 1233x835 1233x835+0+0 8-bit sRGB 325677B 0.040u 0:00.040
    old_file.png PNG 1233x835 1233x835+0+0 8-bit sRGB 325712B 0.040u 0:00.039
    Image: new_file.png
      Channel distortion: RMSE
        red: 0 (0)
        green: 0.358198 (5.46575e-06)
        blue: 0.438701 (6.69415e-06)
        alpha: 0 (0)
        all: 0.283181 (4.32106e-06)
    new_file.png=>null PNG 1233x835 1233x835+0+0 8-bit sRGB 216246B 0.210u 0:00.220

    1



Since these two image files have such a small amount of difference and the total score calculated (0.283181) is less than my threshold of 0.5, I'd expect these two images to register as similar and return 0. (I've experimented with numerous dissimilarity-thresholds between 0.1 and up in the millions, but they also seem to have no effect.) Am I misunderstanding how to use this argument?

Edit: I know I can get the results I want using other combinations, like using -metric AE and -fuzz 0.5%, but I'm still curious, if I can use dissimilarity-threshold with RMSE.

like image 705
Zebbeni Avatar asked Sep 19 '25 00:09

Zebbeni


1 Answers

In Imagemagick, -metric rmse returns 0 (0) for perfectly matching images. The first value in in the quantum range of the ImageMagick compile. The second number in parenthesis is in the range 0 to 1. So, it will return values of quantum range and (1) for totally mismatched images. The dissimilarity-threshold ranges from 0 to 1. Use 1 if you want to test dissimilar images and do not want it to complain that the images are too dissimilar. It is likely you won't need -dissimilarity-metric if you are testing two same sized images, but will need it if using -subimage-search.

RMSE is a measure of difference. So if the images are the same then the difference will be 0.

For example:

convert -size 100x100 xc:white white.png
convert -size 100x100 xc:gray gray.png
convert -size 100x100 xc:black black.png

echo $?
1
compare -metric rmse white.png white.png -format "\n" null:
0 (0)

echo $?
0

compare -metric rmse white.png gray.png -format "\n" null:

compare -metric rmse white.png black.png -format "\n" null:
65535 (1)

compare -metric rmse -dissimilarity-threshold 1 white.png black.png -format "\n" null:
65535 (1)

echo $?
1

compare -metric rmse -dissimilarity-threshold 0 white.png black.png -format "\n" null:
65535 (1)

echo $?
1

So for two same sized images, -dissimilarity-threshold is irrelevant.

Your command

echo $?

is returning whether the command finished successfully or not. It is not the value of the rmse metric.

convert -size 200x200 xc:white white.png
convert -size 100x100 xc:black black.png

compare -metric rmse -subimage-search white.png black.png -format "\n" null:
compare: images too dissimilar `white.png' @ error/compare.c/CompareImageCommand/1148.
echo $?
2

compare -metric rmse -subimage-search -dissimilarity-threshold 1 white.png black.png -format "\n" null:
65535 (1) @ 0,0

echo $?
1

So the return code seems to be giving 0 for a perfect match, 1 for a non-perfect match and 2 for an error.

like image 176
fmw42 Avatar answered Sep 20 '25 14:09

fmw42