In this tutorial we will first show a simple way to pseudocolor / false color a grayscale image using OpenCV’s predefined colormaps. We will also show a way to define a custom colormap if you would rather use your own. As always I am sharing C++ and Python code that you can download here.
This post is dedicated to NASA’s New Frontiers program that has helped explore Jupiter, Venus, and now Pluto!
Often grayscale images of planets and other objects in space are pseudo colored to show detail, and to mark regions corresponding to different materials with different colors. We will use one of the grayscale photos of Pluto taken by New Horizons as an example in this tutorial.
What is a colormap ?
Let’s say we want to show the temperature in different parts of the United States on a map. We could overlay temperature data on top of a US map as a grayscale image — darker regions representing cooler temperatures, and brighter regions representing hotter regions. Not only is such a representation incredibly boring, it is a bad representation for two big reasons. First, the human visual system is not optimized to measure small changes in grayscale intensities. We are way better at perceiving changes in color. Second, we associate different meanings with different colors. It is much more meaningful to represent cooler temperatures using blue and warmer temperatures using red.
Temperature data is just one example, but there are several other cases where the data is single valued (grayscale), but it makes sense to convert it to color data for visualization. Other examples of data that are better visualized by pseudo-coloring are height, pressure, density, humidity so on and so forth.
Using applyColorMap in OpenCV
OpenCV defines 12 colormaps that can be applied to a grayscale image using the function applyColorMap to produce a pseudocolored image. Let’s quickly see how to apply colormap COLORMAP_JET to an image.
C++
using namespace cv;
Mat im_gray = imread("pluto.jpg", IMREAD_GRAYSCALE);
Mat im_color;
applyColorMap(im_gray, im_color, COLORMAP_JET);
Python
import cv2
im_gray = cv2.imread("pluto.jpg", cv2.IMREAD_GRAYSCALE)
im_color = cv2.applyColorMap(im_gray, cv2.COLORMAP_JET)
The figure below shows a visual representation of colormaps and the numeric values of COLORMAP_*. Lower grayscale values are replaced by colors to the left of the scale while higher grayscale values are to the right of the scale.
Value | Name | Scale |
0 | COLORMAP_AUTUMN | ![]() |
1 | COLORMAP_BONE | ![]() |
2 | COLORMAP_JET | ![]() |
3 | COLORMAP_WINTER | ![]() |
4 | COLORMAP_RAINBOW | ![]() |
5 | COLORMAP_OCEAN | ![]() |
6 | COLORMAP_SUMMER | ![]() |
7 | COLORMAP_SPRING | ![]() |
8 | COLORMAP_COOL | ![]() |
9 | COLORMAP_HSV | ![]() |
10 | COLORMAP_PINK | ![]() |
11 | COLORMAP_HOT | ![]() |
Custom Colormaps

There are some people who are never satisfied with the menu at a restaurant. The chef’s expertise in selecting the ingredients notwithstanding, they are going to customize their dish by adding, replacing, and substituting ingredients. Oh yeah, those people.
If you happen to be one of those people, and want to create your own colormap (what’s wrong with the 12 colormap for God’s sake ? ), here are the steps.
- Define a colormap : A colormap is a mapping from 0-255 values to 256 colors. In OpenCV, we need to create an 8-bit color image of size 256 x 1 to store the 256 color values.
- Map the colors using a lookup table : In OpenCV you can apply a colormap stored in a 256 x 1 color image to an image using a lookup table LUT.
C++
Mat im_color;
// NOTE : im_gray is 3-channel image with identical red, green, blue channels.
LUT(im_gray, lut, im_color);
Python
# NOTE : im_gray is 3-channel image with identical red, green, blue channels.
im_color = cv2.LUT(im_gray, lut)
You can download the C++ and Python code for custom colormaps and other images used in this post in the section below. Additional colormaps can be found at colormap.org. The image shown above was transformed using a modified version of one of their colormaps. I call this colormap algae!
Im subscribe and confirm e-mail. Now how I can download code?
Hi Bleach, you must have received an email with instructions. If not, please send me an email at [email protected] and I will send you the material. Thanks.
How can i map RGB and Depth .png images to generate RGBD image. Thanks in advance
Hi Satya,
How can i design a new colour space which is specific for a particular Application.
Hi Ravi,
I am not sure I understand your question. If you want to design a custom colormap for your application, please see the subsection called “Custom Colormaps”
Ok Sir..Thank you
Hello Sir,
i have subscribed but i am not able to download the code and books.
Please help.
Hi Ravi,
Sorry for the inconvenience. I sent you the material.
Sir,
i am not able to get the code. please mail me the code on [email protected].
Thank you
Ravi Holkar
Hi Ravi,
Sorry. I sent you the material.
Hello Satya, I am not able to download the code.can you please help me in getting it ?
Did you receive the Welcome email ? It has a link a to all code in this blog.
Subscribed but can’t download code, any help?
Did you receive the Welcome email ? It has link a to all code in this blog.
I did just now, thanks Satya! -g
Hello Satya I have subscribed too, but I can’t get the code either.
Did you receive it ? If not, please send me an email at [email protected]
Sir,
How can we display the colormap in the output. Is there any separate code for that? Can you please help
Hi. If I may suggest I coded an “elegant” random mapping generator that
generates proper LUT. It’s just a scaled interpolation of a cumulative
normal variable/ I think it’s a good way to generate LUTs randomly and
it would make a fine addition to your work :
import cv2
import numpy as np
from scipy.interpolate import interp1d
def get_random_reas():
w=5
reas=interp1d(range(w+1),np.cumsum([np.random.normal() for x in range(w+1)]),’cubic’)(np.arange(0,w,w/256.))
reas-=np.min(reas)
reas/=np.max(reas)
reas*=255.
return list(map(int,reas))
def applyCustomColorMap(im_gray) :
lut = np.zeros((256, 1, 3), dtype=np.uint8)
lut[:, 0, 0] = get_random_reas()
lut[:, 0, 1] = get_random_reas()
lut[:, 0, 2] = get_random_reas()
im_color = cv2.LUT(im_gray, lut)
return im_color;
Hello Sir I have subscribed too, but I can’t get the code either. I think it doesn’t work, could you please offer a help?