-fx advice

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?".
Post Reply
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

-fx advice

Post by GreenKoopa »

-fx "(u.r < 0.00002 || u.r > 0.99999) && (u.g < 0.00002 || u.g > 0.99999) && (u.b < 0.00002 || u.b > 0.99999) ? #000 : u"

19 minutes for a 6 megapixel image. Is that normal or am I structuring my expression poorly? This expression simply filters some noise from my image. Each pixel is independent of the others. Otherwise ImageMagick is performing very quickly for me.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: -fx advice

Post by magick »

The -fx expression is interpreted and is expected to be significantly slower than native code (e.g. -evaluate). You will get some speed-up if your version of ImageMagick is OpenMP-enabled and you have more than one core:
  • convert -version
    Version: ImageMagick 6.6.5-8 2010-11-07 Q16 http://www.imagemagick.org
    Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
    Features: OpenMP
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -fx advice

Post by fmw42 »

Looks like you are making the very high and very low values combined from each channel black, not really removing them.

But if that is the case, you can do the same without -fx, which is very slow by using thresholding and creating masks, then combining the masks (multiply is like &&) and then multiplying the combined mask by your image to black out those pixels.


convert yourimage \
\( -clone 0 -separate -channel rgb -black-threshold 0.002% -white-threshold 99.999% -fill black -opaque white -fill white +opaque black \) \
\( -clone 1 -clone 2 -compose multiply -composite -clone 3 -compose multiply -composite \) \
-delete 1-3 -compose multiply -composite resultimage


-black-threshold makes all low values black and leaves the rest unchanged
-white-threshold makes all the high values white and leaves the rest unchanged (but the low values are already now black)
then I use -fill black -opaque white to make all white values black
then I use -fill white +opaque black to make all non-black values white
this then creates a binary mask where all your selected low and high values are black and the rest are white

then I multiply the mask by the original image to black out your selected pixels

It should do what you are doing by -fx, if I am not mistaken, and be a lot faster.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: -fx advice

Post by anthony »

fmw42 wrote:convert yourimage \
\( -clone 0 -separate -channel rgb -black-threshold 0.002% -white-threshold 99.999% -fill black -opaque white -fill white +opaque black \) \
\( -clone 1 -clone 2 -compose multiply -composite -clone 3 -compose multiply -composite \) \
-delete 1-3 -compose multiply -composite resultimage
That last parenthesis can be replaced with a -flatten, but as you are then multiplying the original image as well you may as well just multiply ALL 4 images.

Code: Select all

convert yourimage \
     \( +clone -channel rgb -separate \
         -black-threshold 0.002% -white-threshold 99.999% \
         -fill black -opaque white    -fill white +opaque black \) \
     -background white -compose multiply -flatten   resultimage
WARNING: it is unclear how this work work for images containing transparency (alpha channel), though it is doubtful you are typing to do this type of thing on an image with transparency.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: -fx advice

Post by GreenKoopa »

I'm using ImageMagick 6.6.1-1 2010-04-04 Q16 (w/ OpenMP) on an older computer. Most things are very fast, but png compression with quality=90 does take a minute.

My image is the result of a modular diff (-compose subtract -composite), so yes I'm rolling the max white value (65535) back to black. Photoshop somehow introduces some noise when in 16-bit mode, even when saving in a lossless format. Of course it isn't noticeable when viewing or printing images, but it sure makes a diff look busy. It works beautifully, just much slower than other experiences with ImageMagick.

Thank you for the alternatives. It was all much more help than I expected. Not knowing ImageMagick very well, using -fx seemed like an intuitive way (for me at least) to express what I wanted.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -fx advice

Post by fmw42 »

-fx is a very useful tool. It can be used when no other options do what you want. It can also be useful to prototype some result and then try to do it more efficiently with other IM options. Unfortunately, it is slow.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: -fx advice

Post by GreenKoopa »

I'm still unsure of how multiplying the masked channels equates to zeroing when ALL channels are near zero (AND vs OR, and maybe you just understand my intent better than me). Still I absorbed the general idea and created a similar solution. My script runs 100x faster, eliminating my visual noise and shrinking my output .png 90%. I'm finally having more success than failure understanding ImageMagick.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -fx advice

Post by fmw42 »

when you multiply white by white, you get white. when you multiply black by black you get black, when you multiply white by black you get black. That I believe is like &&.

Then you multiply white by your image and it will be image and when you multiply black by your image you get black.

In IM white is like 1 and black is like 0.


Masking is a very powerful tool once you understand it and its uses in mathematical operations.
Post Reply