Feathered mask

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
mikmach
Posts: 42
Joined: 2015-02-06T07:50:43-07:00
Authentication code: 6789

Feathered mask

Post by mikmach »

I've refined command from my previous post to form:

Code: Select all

convert file.tif -channel H -fx "hue < 0.917 && hue > 0.083 && saturation > 0.10 ? r * 0.97 : r" +channel file-fx.tif
But I have two problems:

1. -fx takes terribly long time to work, on big files it really adds up
2. It creates sharp border. 0.97 translates to ca. 7-8 levels on 8 bit images (BTW - how IM rounds up levels in this case - any way to know it?)

Solution to second problem could be creation of mask, feather it a bit, lower reds in underlaying layer and at last apply mask. But: Is it possible to create mask without -fx in this case? If -fx is still necessary it will only increase length of processing.

My version of IM:

Code: Select all

Version: ImageMagick 6.9.3-5 Q8 x64 2016-02-20 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Feathered mask

Post by snibgo »

mikmach wrote:But: Is it possible to create mask without -fx in this case?
Yes. I don't use FX except on very small images. One trick is to create a mask, white where you want the full effect (100%), black where you don't want it at all (0%), or shades of gray. If you make a mask that is only black and white, you can blur it to transition the effect.

Then:

Code: Select all

convert ^
  input.png ^
  ( -clone 0 {the_effect} ) ^
  ( -clone 0 {a_mask} ) ^
  -compose Over -composite ^
  output.png
snibgo's IM pages: im.snibgo.com
mikmach
Posts: 42
Joined: 2015-02-06T07:50:43-07:00
Authentication code: 6789

Re: Feathered mask

Post by mikmach »

Well, it's kinda obvious (although thanks for syntax). The real question was: how to create mask for pixels fullfilling condition:

Code: Select all

hue > 0.917 && hue < 0.083 && saturation < 0.10
without FX?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Feathered mask

Post by snibgo »

You've reversed the hue range, so that condition will never be true. I'll use the condition you gave in the OP.

Create a mask that is white where 8.3% < hue < 91.7% AND saturation > 10%, and black otherwise.

We will make a mask from saturation, and another from hue, then multiply them together.

The saturation condition is simply a "-threshold". The hue condition can be implemented in a number of ways. I will use "-opaque". Note that:

(8.3+91.7)/2 = 50
91.7 - 50 = 41.7

Windows BAT script:

Code: Select all

%IM%convert ^
  rose: ^
  -colorspace HSL ^
  -separate ^
  -delete 2 ^
  ( -clone 0 -fuzz 41.7%% -opaque gray(50%%) ) ^
  ( -clone 1 -threshold 10%% ) ^
  -delete 0-1 ^
  -compose Multiply -composite ^
  mask.png
Putting it together in a single convert, with "-blur" for feathering:

Code: Select all

%IM%convert ^
  rose: ^
  -colorspace HSL ^
  ( -clone 0 ^
    -channel R -evaluate Multiply 0.97 +channel ^
  ) ^
  ( -clone 0 ^
    -separate ^
    -delete 2 ^
    ( -clone 0 -fuzz 41.7%% -opaque gray(50%%) ) ^
    ( -clone 1 -threshold 10%% ) ^
    -delete 0-1 ^
    -compose Multiply -composite ^
    -blur 0x2 ^
  ) ^
  -compose Over -composite ^
  r2.png
snibgo's IM pages: im.snibgo.com
mikmach
Posts: 42
Joined: 2015-02-06T07:50:43-07:00
Authentication code: 6789

Re: Feathered mask

Post by mikmach »

Thank you very much. But I have problem - mask is working great but red manipulation is broken. Links to 3 images:

Source image:
https://drive.google.com/open?id=0B6_Yh ... 3BwQlA4V1k

FX method (upped parameter to r * 0.5 to get easily visible results):
https://drive.google.com/open?id=0B6_Yh ... Hg4Z2hqbGc

Mask method (as above Multiply 0.5):
https://drive.google.com/open?id=0B6_Yh ... FZoUnNWbUU

If you open FX image and compare it with source image you will see that Green and Blue channels remain untouched, only Red is affected.

Mask part works great but reduction of Red not. In fact looks like it adds it!

Thanks for help.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Feathered mask

Post by snibgo »

Sorry, I misunderstood what you wanted. Try this:

Code: Select all

%IM%convert ^
  obecny.jpg ^
  ( -clone 0 ^
    -channel R -evaluate Multiply 0.5 +channel ^
  ) ^
  ( -clone 0 ^
    -colorspace HSL ^
    -separate ^
    -delete 2 ^
    ( -clone 0 -fuzz 41.7%% -opaque gray(50%%) ) ^
    ( -clone 1 -threshold 10%% ) ^
    -delete 0-1 ^
    -compose Multiply -composite ^
    -blur 0x2 ^
    -set colorspace sRGB ^
  ) ^
  -compose Over -composite ^
  r3.png
snibgo's IM pages: im.snibgo.com
mikmach
Posts: 42
Joined: 2015-02-06T07:50:43-07:00
Authentication code: 6789

Re: Feathered mask

Post by mikmach »

That's it! Thank you very much.

I thought this is something with -colorspace but put it in all the wrong places (outside of ()s ).
Post Reply