Page 1 of 1

[CLI] evaluate-sequence + fx without clamping?

Posted: 2020-01-18T04:19:25-07:00
by Hodor
I have 2 images of greyscale (16bits), and I want to do this formula: (image1_value - image0_value) + 0.5

I tried to do it like that:

Code: Select all

magick image1.png image2.png -evaluate-sequence subtract -fx "p{i,j} + 0.5" image_result.png
but it seems the -evaluate-sequence subtract is clamping, so that when (image1_value - image0_value) is lower than 0, it is clamped to 0.

Is there a way to do it without clamping?
Or better yet, run -fx on multiple images?

If not, any other way to do that?

Re: [CLI] evaluate-sequence + fx without clamping?

Posted: 2020-01-18T04:51:59-07:00
by snibgo
"-fx" is very slow. I suggest you use "-compose Mathematics" instead. This can do the subtraction and addition in one operation:

Code: Select all

magick image1.png image2.png -define compose:clamp=off -compose mathematics -define compose:args=0,1,-1,0.5 -composite result.png

Re: [CLI] evaluate-sequence + fx without clamping?

Posted: 2020-01-18T06:03:34-07:00
by Hodor
snigbo - awesome, I wasn't aware of it! Cool option and works great :)

Re: [CLI] evaluate-sequence + fx without clamping?

Posted: 2020-01-18T20:21:01-07:00
by GeeMack
Hodor wrote:
2020-01-18T04:19:25-07:00
Is there a way to do it without clamping? ... If not, any other way to do that?
There are several ways to do this with IM. The "-compose mathematics" method snibgo described is probably the fastest. You can also get around that clipping issue by compressing the levels of one image to the range of 0 to 50%, and the other to a range of 50 to 100%, then do the subtraction like this...

Code: Select all

magick ( image1.png +level 0,50% ) ( image2.png +level 50,100% ) -evaluate-sequence subtract -level 25,75% diff.png
After the subtraction the levels are in the range of 25-75%. The "-level 25,75%" operation brings that back out to fill the 0-100% range, and gives the same result as the composite method.

A couple FX expressions could be used to level the input images together instead of working them inside parentheses. Probably makes no difference.

Code: Select all

magick image1.png image2.png +level %[fx:t*50],%[fx:t*50+50]% -evaluate-sequence subtract -level 25,75% diff.png

Re: [CLI] evaluate-sequence + fx without clamping?

Posted: 2020-01-18T20:33:21-07:00
by fmw42
You can also remove the clamping for -compose minus -composite

Code: Select all

magick image1.png image2.png +swap -define compose:clamp=off -compose minus -composite diff.png
This will do diff = image1 - image2

(the +swap is needed to get the input in the proper order)

see https://imagemagick.org/script/command- ... php#define

The define only works if you have ImageMagick compiled with HDRI (default for IM 7)