Possible bug -fx min vs -evaluate-sequence min

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Locked
User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Possible bug -fx min vs -evaluate-sequence min

Post by fmw42 »

IM 7.0.9.27 Q16 Mac OSX and IM 6.9.10.97 Q16 Mac OSX

Different results from:

Input
Image

Code: Select all

magick \
lena.jpg \
-fx "min(u.r,u.g,u.b)" \
min1.png
Image

Code: Select all

magick lena.jpg -separate +channel -evaluate-sequence min min.png
Image


I think the -fx version is wrong, since I get the same result as the -evaluate-sequence process by using -compose darken as below:

Code: Select all

magick lena.jpg -separate +channel \
\( -clone 0,1 -compose darken -composite \) \
\( -clone 2,3 -compose darken -composite \) \
-delete 0-3 \
mindark.png
Image

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Possible bug -fx min vs -evaluate-sequence min

Post by snibgo »

I confirm the problem. It seems to occur only when we have three arguments to min(). So a workaround is:

min(u.r,min(u.g,u.b))

I've edited my recent reply https://www.imagemagick.org/discourse-s ... 32#p173232
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Possible bug -fx min vs -evaluate-sequence min

Post by fmw42 »

I will point out that -fx is generally slow, but simple and easy to understand. However, if you want faster processing, you often can write a longer command line that will avoid the need for -fx. For example for your -fx command, the following are equivalent.

Unix Syntax:

Code: Select all

magick \
lena.jpg \
-fx "max(u.r,max(u.g,u.b))-min(u.r,min(u.g,u.b))>0.1?u:(u.r+u.g+u.b)/3" \
out1.png

Code: Select all

magick \
lena.jpg \
\( -clone 0 -separate +channel \) \
\( -clone 1,2,3 -evaluate-sequence mean \) \
\( -clone 1,2,3 -evaluate-sequence max \) \
\( -clone 1,2,3 -evaluate-sequence min \) \
-delete 1,2,3 \
\( -clone 2 -clone 3 -compose difference -composite -threshold 10% -negate \) \
-delete 2,3 \
-define compose:clamp=off -compose over -composite \
out2.png

Code: Select all

magick compare -metric rmse out1.png out2.png null:
0 (0)

User avatar
magick
Site Admin
Posts: 11254
Joined: 2003-05-31T11:32:55-07:00

Re: Possible bug -fx min vs -evaluate-sequence min

Post by magick »

See https://imagemagick.org/script/fx.php. Notice the min() syntax as min(x, y), minimum of x and y. The Fx parser is fairly forgiving which explains why no exception is thrown for min(x,y,z).

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Possible bug -fx min vs -evaluate-sequence min

Post by fmw42 »

OK, thanks. I saw that and had thought that was the case. But then user snibgo used 3 arguments in an example. So I thought perhaps that was OK. I even tested a few simple fx calculations with %[fx:...] that seemed to work.

But if only two are allowed, then why does -fx max and mean work fine with 3 arguments and gives the same result as -evaluate-sequence? Was that just dumb luck?

User avatar
magick
Site Admin
Posts: 11254
Joined: 2003-05-31T11:32:55-07:00

Re: Possible bug -fx min vs -evaluate-sequence min

Post by magick »

Could be dumb luck. You would need to add a `debug()` to your `-fx` to trace the reason.

Locked