views:

98

answers:

2

I have an array of pixels with an associated intensity (as a float between 0 and 1) that I would like to convert to a RGB image.

The easiest way would be to just multiply each value by 255 and assign it to R+G+B to obtain a greyscale image, but I think it would be better to use a whole range of colors to show finer differences.

As far as I know this is called a pseudo/false color image, but I am having a hard time finding algorithms for it. Right now I am using this code, but I was wondering if there was a smarter/faster way to do it:

 if(intensity <= 0.25) {
  p->r = 0;
  p->g = 0;
  p->b = (intensity) * 4 * 255;
  return;
 }

 if(intensity <= 0.5) {
  p->r = 0;
  p->g = (intensity - 0.25) * 4 * 255;
  p->b = 255;
  return;
 }

 if(intensity <= 0.75) {
  p->r = 0;
  p->g = 255;
  p->b = (0.75 - intensity) * 4 * 255;
  return;
 }

 if(intensity <= 1.0) {
  p->r = (intensity - 0.75) * 4 * 255;
  p->g = (1.0 - intensity) * 4 * 255;
  p->b = 0;
  return;
 }

Also, are there any standard color palettes I should use for this?

+3  A: 

You could use HSV or HSL instead.

Try using your 0 to 1 value to set the Hue and use constants for saturation and value. You'll find plenty of example code for converting your HSV value to an RGB value.

http://en.wikipedia.org/wiki/HSL_and_HSV

When you see an "artificial colours" image, this is often what is used. The benefit is that all areas of the image are bright, with "dark" parts of the image simply coming out a different colour.

If you use HSL you can also set the Lightness to 50% for most of your 0-1 range, but allowing it to go up right near the end (eg from 0.9 to 1), which will help bring the very intense parts of your image up to white.

Another option is to use a False Colour Palette. Here, you simply create an 8-bit image, and create a palette for it that suits your needs. There are some good examples of these here...

rikh
I guess this would work if the OP's "intensity" were used for any of the three values, as long as the other two are kept constant.
FrustratedWithFormsDesigner
Yes, but sticking it in the hue would create a colourful image with all levels of intensity being clear. You can also only map part of the hue range to the 0-1 range, so you can create an image that goes from a cold blue to a hot hot yellow, via red.
rikh
I tried out creating a HSL/HSV and that seemed to work out pretty well, thanks! A false color palette would be even better, but I had some trouble implementing that - is there actually some smart way to do that (as a function of my intensity) or would I actually have to hardcode all the colors?
Nils
I'd just choose a useful palette and hardcode it (or load it in and work from that) instead of trying to generate it algorithmically. The example I linked to seemed like a good starting point.
rikh
A: 

Define a bunch of RGB values and the intensity they will correspond to. Each of the intensities in your image will lie between two of these points - you can multiply by a constant and truncate, then look them up in an array. Once you have the two RGB values, interpolate between them based on the distance between the image intensity and the intensities of the two endpoints.

Mark Ransom