Negate (Invert) only specific channel?

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
imek
Posts: 7
Joined: 2013-12-05T06:28:44-07:00
Authentication code: 6789

Negate (Invert) only specific channel?

Post by imek »

Related to my question question about negating only non-saturated colors, I would like to know how to convert a specific [s]channel[/s] color (say blue) to grayscale.

I tried to do it with a mask, but the b I used instead of g in the -channel option doesn't seem to refer to the blue channel.

Code: Select all

convert img.png -colorspace HSB -channel b -separate +channel -threshold 0% mask
Last edited by imek on 2013-12-05T13:47:43-07:00, edited 3 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Negate (Invert) only specific channel?

Post by fmw42 »

I am not sure what you really want. If you want channel b to be gray, all that does is make that channel a constant value and I suspect is not what you want.

Nevertheless taking your question literally, here is how to make the blue channel gray and leave the other channels the same.

convert image -channel b -evaluate set 50% +channel result


If you really want all blue shades to be gray, then you need to find what hue is blue and create a mask for that and then do the same as before. So you would be processing the hue channel from HSB which is the red channel and thresholding to get only values near blue to white and all the rest to black. Blue is at hue=240 so that needs to be converted to range 0 to 100% so hue=100*240/360=66.7%

so the mask would be obtained by

convert image -colorspace HSB -channel r -fuzz XX% -fill black +opaque "gray(67%)" -fill white -opaque "gray(67%)" +channel mask
imek
Posts: 7
Joined: 2013-12-05T06:28:44-07:00
Authentication code: 6789

Re: Negate (Invert) only specific channel?

Post by imek »

You're right: what I really want is to replace shades of blue with shades of gray.
Can the mask be non-monotone? I'd like to preserve the shades brightness, so alpha is a must.

From this, to That:
Image Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Negate (Invert) only specific channel?

Post by fmw42 »

try this


convert neg-bw-b1.png \
\( -clone 0 -colorspace HSL -channel b -separate +channel \) \
\( -clone 0 -colorspace HSL -channel r -separate +channel -fuzz 10% -fill black +opaque "gray(67%)" -fill white -opaque "gray(67%)" \) \
-compose over -composite result1.png

or


convert neg-bw-b1.png \
\( -clone 0 -colorspace HSL -channel b -separate +channel -auto-level \) \
\( -clone 0 -colorspace HSL -channel r -separate +channel -fuzz 10% -fill black +opaque "gray(67%)" -fill white -opaque "gray(67%)" \) \
-compose over -composite result2.png
imek
Posts: 7
Joined: 2013-12-05T06:28:44-07:00
Authentication code: 6789

Re: Negate (Invert) only specific channel?

Post by imek »

Hmm.. Both fail to preserve the colors behind the blue section and turn them to complete grayscale. Pure blue (#0000FF) should be completely grayscale, but colors such as purple or cyan should lose their saturation only partially, proportionally to their "blue-ness". I hope this makes sense. :D
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Negate (Invert) only specific channel?

Post by fmw42 »

try this


convert neg-bw-b1.png \
\( -clone 0 -colorspace HSL -channel b -separate +channel -auto-level \) \
\( -clone 0 -colorspace HSL -channel g -separate +channel -auto-level \) \
\( -clone 0 -colorspace HSL -channel r -separate +channel -fuzz 10% -fill black +opaque "gray(67%)" -fill white -opaque "gray(67%)" \) \
\( -clone 2 -clone 3 -compose multiply -composite \) \
-delete 2,3 -compose over -composite result.png
imek
Posts: 7
Joined: 2013-12-05T06:28:44-07:00
Authentication code: 6789

Re: Negate (Invert) only specific channel?

Post by imek »

Great! Thank you.
Post Reply