Convert a 16 bit grayscale image into an RGB colorspace

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
fommil

Convert a 16 bit grayscale image into an RGB colorspace

Post by fommil »

Dear all,

I would like to convert a series of 16 bit grayscale images into an RGB colorspace - something like a rainbow colorspace - so that I can visually see the 16 bit depth. Anyone got any advice on how to do this with ImageMagick? In theory, it should be a lot easier than the many downsampling techniques that people use! (assuming such a colorspace exists, of course)

I've seen some talk in other forums about doing something similar, e.g. considering the gray values in terms of travelling along the axes of the RGB cube http://forums.ni.com/ni/attachments/ni/ ... travel.PNG but, of course, that wouldn't track out 16 bits so the "rainbow" would need to cover much more of the colorspace.
Last edited by fommil on 2010-11-30T12:52:03-07:00, edited 2 times in total.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Convert a 16 bit grayscale image into an RGB colorspace

Post by GreenKoopa »

I'm not sure what you mean by "so that I can visually see the 16 bit depth". Maybe an example of something you have tried would help.

First check out -type (usage examples). Additionally Converting Color to Gray-Scale, -colorspace, and -profile may be informative.
fommil

Re: Convert a 16 bit grayscale image into an RGB colorspace

Post by fommil »

GreenKoopa wrote:I'm not sure what you mean by "so that I can visually see the 16 bit depth". Maybe an example of something you have tried would help.

First check out -type (usage examples). Additionally Converting Color to Gray-Scale, -colorspace, and -profile may be informative.
My (and probably your) computer screen can only show 8 bits of depth for any channel - but that means 24 bits of colour depth. I have some 16 bit grayscale images and the usual approach to visualising this type of image is to choose an appropriate "window" to quantize into and then chop them to 8 bit grayscale images for viewing. I'd rather not do that. Instead, I'd like to convert the images into a pre-defined 16 bit colorspace. Unfortunately, I do not have an appropriate 16 bit colorspace in mind - I'm hoping someone here has done something similar.

It might not be possible of course, because it might not be possible to define an appropriate colorspace that accurately portrays 16 bits of depth.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Convert a 16 bit grayscale image into an RGB colorspace

Post by GreenKoopa »

Either I missed that last line or it was an edit. Do you want alternative ideas or how to accomplish a particular idea?

First off, you can never really see 16-bit depth visually. Your monitor can't do it even if your eye could. Your idea will get you visualizing more than 8-bit for sure. Alternatively, you can 'zoom in' on a particular part of the scale, blowing out the shadows and highlights. This alternative is easier, but may not be helpful to whatever you are doing. A similar alternative is to adjust the gamma.

You could just use -colorspace to turn your grayscale into hue (HSB or HSL). You could get exactly what that cube demonstrates using -fx.

To give you more, I think I need some more clarity as to your need.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Convert a 16 bit grayscale image into an RGB colorspace

Post by GreenKoopa »

Sorry, I think we are talking past each other by posting too quickly.

Of course you can fit 16-bit grayscale (2^16 = 64 Ki levels) into 8-bit RGB (2^(8*3) = 16384 Ki levels), but not in a way that helps you visualize anything. That cube idea would give you 10-bit by tracing 4 sides of an 8-bit cube. Is my understanding and math correct?
fommil

Re: Convert a 16 bit grayscale image into an RGB colorspace

Post by fommil »

GreenKoopa wrote:Either I missed that last line or it was an edit. Do you want alternative ideas or how to accomplish a particular idea?

First off, you can never really see 16-bit depth visually. Your monitor can't do it even if your eye could. Your idea will get you visualizing more than 8-bit for sure. Alternatively, you can 'zoom in' on a particular part of the scale, blowing out the shadows and highlights. This alternative is easier, but may not be helpful to whatever you are doing. A similar alternative is to adjust the gamma.

You could just use -colorspace to turn your grayscale into hue (HSB or HSL). You could get exactly what that cube demonstrates using -fx.

To give you more, I think I need some more clarity as to your need.
I'm looking to accomplish the idea :-) (and sorry, I edited the post so you might have missed some clarifying points). Thanks, the "fx" switch looks like the best way to implement any idea.

I think you've got it - I want to transform my 16 bit grayscale images into a colorspace that exposes as much of those 16 bits as possible. And I want to have static, full size images, as the output.

You're quite right - the monitor (and especially our eyes!) are not technically capable of distinguishing 24 bits, but I want to squeeze as much out as possible!

The HSB/HSL approach is a good one, but I am not sure it covers the whole 16 bits. I am aware that the cube I linked would only expose 10 bits of depth (counting along 6 edges would only give a colorspace of 256*6 = 1536 which is slightly more than 10 bits). Tracing along the edges of the CMYK colorspace has the potential to provide even more than this. I think getting to 12 bits should be enough for my purposes.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Convert a 16 bit grayscale image into an RGB colorspace

Post by GreenKoopa »

Your ideas may best be tested on a simple gradient like

Code: Select all

convert -size 10x1024 gradient:black-white gradient.png
If your image is huge, -fx can be slow. Smaller test images can be quicker for that trial-and-error development process. Once you are generating what you want, someone can help you speed it up if necessary.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert a 16 bit grayscale image into an RGB colorspace

Post by fmw42 »

You can colorize your image with -clut and a color gradient image. See http://www.imagemagick.org/Usage/canvas ... colorspace The larger you make the length of the colorized gradient the more resolution you will have. So you could make your rainbow gradient 65535x1 pixel to get full colorization spanning 16K dynamic range.

also see my script below, pseudocolor
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Convert a 16 bit grayscale image into an RGB colorspace

Post by GreenKoopa »

As usual, fmw42 has the simplest implementation.

Using your cube mapping:

Code: Select all

convert -size 10x512 gradient:black-white -rotate -90 test_image.png
convert -size 1x64 gradient:#000-#f00 gradient:#f00-#ff0 gradient:#ff0-#fff gradient:#fff-#0ff gradient:#0ff-#00f gradient:#00f-#000 -append lut.png
convert -interpolate bilinear test_image.png lut.png -clut out.png
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert a 16 bit grayscale image into an RGB colorspace

Post by fmw42 »

the color gradient at http://www.imagemagick.org/Usage/canvas ... colorspace does not involve black or white. But see my page at http://www.fmwconcepts.com/imagemagick/ ... hp#rainbow for color gradients (rainbows) and step wedges for several techniques.
Post Reply