Pseudocolor implementation with OpenCV.

Jan 28, 2010 by     8 Comments    Posted under: OpenCV, Tutorials

In Computer Vision works in a lot of cases with gray images because there are a lot of motives. But human vision don’t perceives the gray levels so well as color levels.

Then if we need show a image to a person, we can color it. But, how is the best way to coloring gray image?

There are 3 ways to do it: Manually, automatically and colored by ranges.

In this tutorial, i go to develop the way most common automatic for gray image coloring.

To do it we need know we go to receive a gray level and we need return 3 values, one for red, one for blue and other for green.

We go to use this function:

\displaystyle{ s(x,y)=\vert sin(r(x,y)*p*PI + \Theta*PI)\vert}

And r(x,y) is the gray level and p is the number of repetitions and \displaystyle{ \Theta } is the displacement.

Then we only need define the p and \displaystyle{ \Theta } for each channel.

If we create a plot with this function with this parameters for red, green and blue ((2,0),(2,-0.1),(2,-0.3)) we get:

Pseudocolor Graph

Pseudocolor Graph with red(p=2,theta=0) green(2,-0.1) and blue(2,-0.3)

Then we only need set the gray level in range 0 to 1 and the sine returns values from 0 to 1 we interpret as float image values or we set in range 0 to 255.

To finish this is the result:

Pseudocolor Result

Pseudocolor Result

Download the code.

8 Comments + Add Comment

  • Hi! First of all, nice blog :)

    I am facing the opposite problem. I’ve a pseudocolor image that represents reflectivity (or energy). From black / blue values (no reflectivity) to red / white values (maximum reflectivity). In fact, my images are exactly the inverse of what you are showing: blue regions come from dark values, red regions come from white values.

    I guess I could use something like your function to map that 3 channel image back to a gray level image (because I’m using OpenCV to analyze it, and thus color is not so important). Right now, I’m simply weighting each channel:

    graylevel = 0.6 * r + 0.4 * g + 0.2 * b

    That way, white/red regions become brighter than blue/black regions.

    But I don’t really like that solution. Then I thought I could create a LUT table… but it would be much better with a map function like yours. Any experience with something like that?

  • Hi Luis, you know the color space and algorithm was encoding this image?. If you know or can determine it, then you can create a reverse function. It’s only i can help. It’s good you create lut table for better performance.

    Regards David.

  • It’s plain RGB, but no idea about the algorithm. The scale is similar to this one: http://www.efg2.com/Lab/Library/Color/AccuweatherRadarReflectivity.jpg

    So yes, I think I’ll prepare a LUT table and that’s all :)

  • The image is plain rgb, but you can use other color space to work, for example Lab space or HSV or similar because have some properties than others.

    The scale is simiar to this? http://en.wikipedia.org/wiki/Color_theory#Warm_vs._cool_colors

  • Well, something similar. Indeed, the scale is just the same that Hue values take in HSV, more like this:
    http://en.wikipedia.org/wiki/File:HSV-RGB-comparison.svg

    In fact, I also wanted to try those fuzzy logic sets to go from color to HUE. That would work too, I guess. There’s no violet (no colors above 240º), so I would have no problem with circling colors…

  • Ok, just in case you need it, I think I have something useful here. I tried with LUTs, but finding the right bin is not trivial (color distances, you know). So I went back to HSV.

    The thing is, in HSV, that lower and higher V values produce undefined H values. Therefore, H is not enough to map from pseudocolor to gray.

    But you get a nice mapping if the gray value is given by the following expression:

    gray = (240.0 – HUE) * (240.0 – HUE) * V.

    Then you adjust the dynamic range back to 0-255.

    That way, low light values produce low gray values, high light values produce high gray values and middle tones are handled by HUE.

    That’s of course if the pseudocolor map is similar to what we talked before: white-red (0 HUE)-yellow-green-cyan-blue (240 HUE)-black :)

  • Hello,
    What is the of the mapping function?
    Do you have any references for it?

  • Hi Olu, the mapping function is the above funcion on tutorial. In code i generat under pseudocolor.c a table with this function for all value colors 0..255 and i map the image with pseudocolor function.

Got anything to say? Go ahead and leave a comment!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Category

Polls

How Is My Site?

View Results

Loading ... Loading ...

Twitter: damiles3D