-fx function use for neutral gray filter

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?".
Locked
CanadianHusky
Posts: 7
Joined: 2020-03-11T09:49:46-07:00
Authentication code: 1152

-fx function use for neutral gray filter

Post by CanadianHusky »

Hello,

I studied the -fx functions for ImageMagick 7 at https://imagemagick.org/script/fx.php but could not figure out the correct function syntax for this algorithm

1 input image and 1 input value (threshold)

loop over all pixels

Code: Select all

if (max(r,g,b) - min(r,g,b)) > threshold then output pixel = original(r,g,b) else output pixel = average(r,g,b)
example with numbers
threshold = 15
input RGB pixel value (x,y) = 210,200,199
max(210,200,199) = 210
min(210,200,199) = 199
max - min = 11 < 15 ...theredore output pixel = (210+200+199)/3 = 203 for r,g,b (all channels equal = pure gray)

input RGB pixel value (x,y) = 210,200,180
max(210,200,180) = 210
min(210,200,180) = 180
max - min = 30 > 15 ...theredore output pixel = original pixel, unchanged

This is a 'neutralize gray' algorithm that I want to implement with IM

thank you kindly

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

Re: -fx function use for neutral gray filter

Post by snibgo »

Windows syntax:

Code: Select all

magick ^
  in.png ^
  -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" ^
  out.png
Note that all values are (usually) in the range 0.0 to 1.0.

EDIT Corrected for possible bug https://www.imagemagick.org/discourse-s ... =3&t=37676
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: -fx function use for neutral gray filter

Post by fmw42 »

I believe there is a bug in the current IM 7.0.9.27 version in -fx min. See

https://imagemagick.org/discourse-serve ... =3&t=37676

The -fx max and mean, seem to be consistent with -evaluate-sequence.

CanadianHusky
Posts: 7
Joined: 2020-03-11T09:49:46-07:00
Authentication code: 1152

Re: -fx function use for neutral gray filter

Post by CanadianHusky »

Thank you. Indeed very powerful -fx language. I am trying to work around that bug for -fx min

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

Re: -fx function use for neutral gray filter

Post by fmw42 »

If this is still an issue, post a bug report at https://github.com/ImageMagick/ImageMagick/issues

Locked