Selectively manipulating HSB channels

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
jbednar

Selectively manipulating HSB channels

Post by jbednar »

Is there a simple way to manipulate only the Brightness channel? For what I'm doing, I need to first swap the Saturation and Brightness channels of an image that was generated

I'm working with images generated using a Hue, Saturation, and Value colormap (as specified in Numerical Recipes in C). I need to swap the Saturation and Value (apparently equivalent to Brightness in ImageMagick's HSB colormap) channels, and then scale the the Brightness/Value channel by a specified amount.

This almost works:

convert file.png -colorspace HSB -separate -set colorspace HSB -swap 1,2 -combine -colorspace RGB -level 0%,50% out.png

If I leave off "-level 0%,5%", it correctly swaps Hue and Brightness/Value. And -level also works fine for small changes in level. However, it fails for large values -- if I increase the level so much that some pixels reach maximum brightness, their hue changes, with everything going to one of the three RGB colors that have full intensity for two of the three channels (i.e. R=Max,G=Max,B=0, R=0,G=Max,B=Max, etc.), thus destroying the orginal hue information.

What I want is to be able to change the level only of the Brightness channel, affecting no other channel, but nothing I can find so far will do that. Any suggestions?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Selectively manipulating HSB channels

Post by snibgo »

If you post a URL to your image, we can try to come up with something.

When levels works in RGB space (which I think it always does), setting "white" must set the maximum value in the three channels. similarly black. So white or black will lose the hue information.

I suspect the way to do what you want is with "-channel". But if you then convert the output to RGB, white and black will both lose hue.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Selectively manipulating HSB channels

Post by fmw42 »

If your file is already in HSB, then all the color conversion seems meaningless. Please clarify. If you are starting with an RGB file and then converting to HSB, why do you need to swap the S and B channels, do you need to have a file in HBS order?

Any way, assuming you need to convert from RGB to HSB, swap the S and B, then are you changing the old B or the new B channel with -level?

Assuming the new B (which was the old S), then

convert file.png -colorspace HSB -separate -swap 1,2 \
\( -clone 2 -level 0%,50% \) -swap 2,3 -delete 3 \
-set colorspace HSB -colorspace RGB -combine out.png
jbednar

Re: Selectively manipulating HSB channels

Post by jbednar »

fmw42 wrote:If your file is already in HSB, then all the color conversion seems meaningless. Please clarify. If you are starting with an RGB file and then converting to HSB, why do you need to swap the S and B channels, do you need to have a file in HBS order?

Any way, assuming you need to convert from RGB to HSB, swap the S and B, then are you changing the old B or the new B channel with -level?

Assuming the new B (which was the old S), then

convert file.png -colorspace HSB -separate -swap 1,2 \
\( -clone 2 -level 0%,50% \) -swap 2,3 -delete 3 \
-set colorspace HSB -colorspace RGB -combine out.png
Thanks for the reply! It doesn't actually work :-/, but I was able to use it to make a command that seems to work. First, the file is RGB by the time ImageMagick sees it; the reason I mentioned HSV/HSB as the source is that it was generated by a program that stored completely independent information in the three H, S, and V, channels for visualization purposes, and thus I know exactly what is in each of those three channels and can easily detect when any manipulation starts mixing them up. The S and V swapping is for technical reasons having to do with matching the visualization format from data from different sources, and in any case is working fine. The level change is for the *new* B, i.e. the old S.

Anyway, here's a sample image (of orientation preferences in the primary visual cortex):

http://homepages.inf.ed.ac.uk/jbednar/images/orsw.png

Note that the Value/Brightness channel is uniformly 1.0. If I simply swap V and B, it works fine:

http://homepages.inf.ed.ac.uk/jbednar/images/orsb.png

Now the shading is black and dark colors instead of white and light colors, which is what I wanted, but the image is too dark to make anything out. If I apply the command suggested above:

convert orsw.png -colorspace HSB -separate -swap 1,2 \( -clone 2 -level 0%,5% \) -swap 2,3 -delete 3 -set colorspace HSB -colorspace RGB -combine orsb_bad.png

all the hues get messed up:

http://homepages.inf.ed.ac.uk/jbednar/i ... sb_bad.png

But if I blindly merge your command with my original and cross my fingers:

convert orsw.png -colorspace HSB -separate -set colorspace HSB -swap 1,2 \( -clone 2 -level 0%,5% \) -swap 2,3 -delete 3 -combine -colorspace RGB orsb_bright.png

I get what I wanted:

http://homepages.inf.ed.ac.uk/jbednar/i ... bright.png

So, it all works now! Thanks! I've no idea what the differences between your command and that one mean, though.

Jim
Post Reply