Page 1 of 1

V7 compare

Posted: 2013-08-19T10:27:06-07:00
by snibgo
If developers are thinking about about compare and v7, here's a suggestion.

A "-compare" operator could be added to convert and magick, working in a similar way to "-composite". It would do a comparison according to "-metric" etc, and make an output image just as the compare command currently does. However, it would also be influenced by a third image, if given, which acts as a mask. The text output would be available as an escape, eg "%[compare]".

The benefit is that, having created images in convert/magick, we wouldn't have to save results, exit convert, enter compare, and re-read the images.

It may be an easy addition to convert/magick. Or it might not, of course.

Re: V7 compare

Posted: 2013-08-19T10:39:25-07:00
by magick
It would be simple enough to support a -compare option for both IMv7 and 6. The returned value would likely be %[distortion]. We'll likely code up a solution in the next few weeks. Thanks.

Re: V7 compare

Posted: 2013-08-19T12:38:41-07:00
by fmw42
Correct me if I am wrong but this would be a read mask in IM 6, which has not been implemented.

Re: V7 compare

Posted: 2013-08-19T13:04:19-07:00
by snibgo
The prime functionality I'd like is a "-compare" operator in convert, that does much the same thing as the compare command.

Using a third image as a mask is a nice-to-have addition. I'm not exactly sure what I'd like it to do. Probably, using RMSE as an example: the pixel difference of the first two images is multiplied by the pixel intensity of the third, then the difference is squared, then all the differences are summed to find the mean, which is then square-rooted.

Is this the same as a read mask would do? I don't know.

Re: V7 compare

Posted: 2013-08-19T14:28:30-07:00
by fmw42
is this the same as a read mask would do? I don't know.
No, as you explain it, it would be more like a write mask. The read mask would only process those pixel defined by the mask. The write mask processes all pixels and then filters by the mask after processing all pixels.

Why can you not use -compose difference and then threshold on some appropriate value?

Sound like you want to compute the rmse values for every pixel in the two images with convert -compose ... -composite, (rather than compare, presumably to do other things in the convert in one command), then threshold the result.

You can compute the rmse difference images from scripting using compose difference on each channel, average the channels and use -evaluate pow 0.5 to compute the sqrt. Then get the average (mean) statistics from that image.

I do something similar only the metric is normalized cross correlation, in my script, simiar, at http://www.fmwconcepts.com/imagemagick/ ... /index.php

Try this to get the rmse error between the two same size images.

convert image1.png image2.png -compose difference -composite -separate +channel \
-poly "0.3333,2 0.3333,2 0.3333,2" -evaluate pow 0.5 -write rmse.png -format "%[mean] (%[fx:mean])" info:



rmse=sqrt( (r1-r2)^2 + (g1-g2)^2 +(b1-b2)^2) / 3 )

-poly does the squareing, adding and dividing by 3 (i.e multiply each square by 0.3333)


Of course my command line could be made into a new compose method, say -compose rmse and my script could be -compose ncc.


EDIT: Here are the command to do NCC, but it needs to be done in HDRI

ncc=(image1-mean1) * (image2-mean2) / (std1*std2)

infile1="cyclops.gif"
infile2="cyclops.gif"


convert $infile1 -colorspace gray 1tmp1.miff
convert $infile2 -colorspace gray 1tmp2.miff
qrange2=`convert xc: -format "%[fx:quantumrange*quantumrange]" info:`
m1=`convert 1tmp1.miff -format "%[mean]" info:`
m2=`convert 1tmp2.miff -format "%[mean]" info:`
m12=`convert xc: -format "%[fx:$m1*$m2]" info:`
s1=`convert 1tmp1.miff -format "%[standard-deviation]" info:`
s2=`convert 1tmp2.miff -format "%[standard-deviation]" info:`
s12=`convert xc: -format "%[fx:($qrange2)/($s1*$s2)]" info:`
imh convert \( 1tmp1.miff -evaluate subtract $m1 \) \( 1tmp2.miff -evaluate subtract $m2 \) \
-compose multiply -composite -evaluate multiply $s12 \
+write ncc.png -format "%[mean] (%[fx:mean])\n" info:
65535.6 (1.00001)

Note that we have to multiply the result by quantumrange^2. One quantumrange is need to compensate for IM normalizing to the range 0 to 1 before multiplying and unnormalizing the result by multply by quantumrange

(5/quantumrange)*(5/quantumrange)*quantumrange = 25/quantumrange and we need 25.

The other quantumrange in the square is needed because the ncc result in in the range -1 to 1. Thus to get the result of perfect match to be 65535 (1), we must multiply the 1 by quantumrange for the non-normalized result.

Re: V7 compare

Posted: 2013-08-19T14:38:41-07:00
by snibgo
I used RMSE as an example of how the mask might work.

Perhaps all the metrics can be achieved within a single convert, and the compare command is strictly redundant. Even if it is, incorporating it into the convert/magick command makes sense to me.

Re: V7 compare

Posted: 2013-08-19T18:02:21-07:00
by magick
Grab ImageMagick 6.8.6-9 beta tomorrow afternoon and try
  • convert image.png reference.png -metric RMSE -compare -format "%[distortion]" info:

Re: V7 compare

Posted: 2013-08-19T18:53:22-07:00
by fmw42
magick wrote:Grab ImageMagick 6.8.6-9 beta tomorrow afternoon and try
  • convert image.png reference.png -metric RMSE -compare -format "%[distortion]" info:

What about the output image? It would be nice to get the rmse resulting image as well as the statistics.


The above looks pretty much like

convert image image2 -compose rmse -composite rmseresult

with an output statistic, ie.

convert image image2 -compose rmse -composite -format %[mean] (%[fx:mean])" info:


I suppose you created a new function because you did not want to change -compose to add statistics, which makes sense. But seems one could get the same by just creating a new compose format, rmse

convert image image2 -compose rmse -composite -write resultimge -format "%[mean] (%[fx:mean])\n" info:
or
convert image image2 -compose rmse -composite -format "%[mean] (%[fx:mean])\n" -write info: resultimage


Either way, it would be nice at some later time to add more (compare) metrics to this as options (whichever ones have 0 to 1 limits), if you have not already done so. So -metric ncc might be a possible future metric, since it is also a metric of compare in range 0 to 1.

Re: V7 compare

Posted: 2013-08-20T03:23:36-07:00
by magick
The difference image is returned by -compare and -metric supports all the standard metrics including NCC.

Re: V7 compare

Posted: 2013-08-20T09:57:27-07:00
by fmw42
magick wrote:The difference image is returned by -compare and -metric supports all the standard metrics including NCC.

Great! Thanks.

Re: V7 compare

Posted: 2013-08-31T21:55:32-07:00
by fmw42
magick wrote:Grab ImageMagick 6.8.6-9 beta tomorrow afternoon and try
  • convert image.png reference.png -metric RMSE -compare -format "%[distortion]" info:

The above works fine. Also one can do the following to get the difference image.

convert image.png reference.png -metric RMSE -compare difference.png

But then it does not output the metric value.

So it seems to me that the -format "%[distortion]" info: might not really be necessary. If one made this work like compare and always output the metric value to the terminal. For example, if you only wanted the metric returned, one could then just do

convert image.png reference.png -metric RMSE -compare null:

in analogy to

compare -metric RMSE image.png reference.png null:


Am I missing something such that it needs to work with -format "%[distortion]" info:? Was there something that user snibgo requested that I am missing? Or is this just due to the difference between how compare is coded vs how convert needs to do the same thing?

Re: V7 compare

Posted: 2013-09-03T13:53:48-07:00
by fmw42
This seems to work fine in IM 6.8.6.9 Q16 Mac OSX, but it would be nice if it would respect the -precision argument

convert -precision 5 rose: \( rose: -blur 0x3 \) -metric RMSE -compare -format "%[distortion]\n" info:
0.10396026946004832681


convert rose: \( rose: -blur 0x3 \) -precision 5 -metric RMSE -compare -format "%[distortion]\n" info:
0.10396026946004832681


convert rose: \( rose: -blur 0x3 \) -metric RMSE -compare -format "%[distortion]\n" -precision 5 info:
0.10396026946004832681



Work Around:

printf "%1.5f\n" $(convert rose: \( rose: -blur 0x3 \) -metric RMSE -compare -format "%[distortion]" info:)
0.10396

Re: V7 compare

Posted: 2013-10-02T16:51:28-07:00
by snibgo
v6.8.7 under Windows 7: it does now respect "-precision", provided this is before "-format":

Code: Select all

D:\web\im>c:\im\ImageMagick-6.8.7-Q16\convert -precision 5 rose: ( rose: -blur 0x3 ) -metric RMSE -compare -format "%[distortion]\n" info:
0.10396

D:\web\im>c:\im\ImageMagick-6.8.7-Q16\convert rose: ( rose: -blur 0x3 ) -precision 5 -metric RMSE -compare -format "%[distortion]\n" info:
0.10396

D:\web\im>c:\im\ImageMagick-6.8.7-Q16\convert rose: ( rose: -blur 0x3 ) -metric RMSE -compare -format "%[distortion]\n" -precision 5 info:
0.10396

D:\web\im>c:\im\ImageMagick-6.8.7-Q16\convert -precision 10 rose: ( rose: -blur 0x3 ) -metric RMSE -compare -format "%[distortion]\n" info:
0.1039602695

D:\web\im>c:\im\ImageMagick-6.8.7-Q16\convert rose: ( rose: -blur 0x3 ) -precision 10 -metric RMSE -compare -format "%[distortion]\n" info:
0.1039602695

D:\web\im>c:\im\ImageMagick-6.8.7-Q16\convert rose: ( rose: -blur 0x3 ) -metric RMSE -compare -format "%[distortion]\n" -precision 10 info:
0.10396

Re: V7 compare

Posted: 2013-10-02T17:22:49-07:00
by fmw42
It has been working in IM since 6.8.6.10. Changes in general are folded into IM 7 as well.

And true as far as I understand that the -precision must be before -format.