» blog/colormaps_in_opencv

Colormaps in OpenCV

Introduction

Yesterday I needed to visualize some data in OpenCV. It's easy to apply a colormap in MATLAB, but OpenCV doesn't come with predefined colormaps. So I simply took the interpolation steps from the GNU Octave colormaps and turned them into C++ classes.

The source code is on github:

Using the code

You only need cv::applyColorMap to apply a colormap on a given image. The signature of cv::applyColorMap is:

void applyColorMap(InputArray src, OutputArray dst, int colormap)

The following code example reads an image (given as a command line argument), applies a Jet colormap on it and shows the result:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "colormap.hpp"

using namespace cv;

int main(int argc, const char *argv[]) {
    // Get the path to the image, if it was given
    // if no arguments were given.
    string filename;
    if (argc > 1) {
        filename = string(argv[1]);
    }
    // The following lines show how to apply a colormap on a given image
    // and show it with cv::imshow example with an image. An exception is
    // thrown if the path to the image is invalid.
    if(!filename.empty()) {
        Mat img0 = imread(filename);
        // Throw an exception, if the image can't be read:
        if(img0.empty()) {
            CV_Error(CV_StsBadArg, "Sample image is empty. Please adjust your path, so it points to a valid input image!");
        }
        // Holds the colormap version of the image:
        Mat cm_img0;
        // Apply the colormap:
        applyColorMap(img0, cm_img0, COLORMAP_JET);
        // Show the result:
        imshow("cm_img0", cm_img0);
        waitKey(0);
    }

    return 0;
}

The available colormaps are:

enum
{
    COLORMAP_AUTUMN = 0,
    COLORMAP_BONE = 1,
    COLORMAP_JET = 2,
    COLORMAP_WINTER = 3,
    COLORMAP_RAINBOW = 4,
    COLORMAP_OCEAN = 5,
    COLORMAP_SUMMER = 6,
    COLORMAP_SPRING = 7,
    COLORMAP_COOL = 8,
    COLORMAP_HSV = 9,
    COLORMAP_PINK = 10,
    COLORMAP_HOT = 11
}

And here are the scales corresponding to each of the maps, so you know what they look like:

Name Scale
Autumncolorscale_autumn
Bonecolorscale_bone
Coolcolorscale_cool
Hotcolorscale_hot
HSVcolorscale_hsv
Jetcolorscale_jet
Oceancolorscale_ocean
Pinkcolorscale_pink
Rainbowcolorscale_rainbow
Springcolorscale_spring
Summercolorscale_summer
Wintercolorscale_winter

Demo

The demo coming with the cv::applyColorMap shows how to use the colormaps and how I've created the scales you have seen:

If you want to apply a Jet colormap on a given image, then call the demo with:

./cm_demo /path/to/your/image.ext

Or if you just want to generate the color scales, then call the demo with:

./cm_demo

Note: Replace ./cm_demo with cm_demo.exe if you are running Windows!

Building the demo

The project comes as a CMake project, so building it is as simple as:

philipp@mango:~/some/dir/colormaps-opencv$ mkdir build
philipp@mango:~/some/dir/colormaps-opencv$ cd build
philipp@mango:~/some/dir/colormaps-opencv/build$ cmake ..
philipp@mango:~/some/dir/colormaps-opencv/build$ make
philipp@mango:~/some/dir/colormaps-opencv/build$ ./cm_demo path/to/your/image.ext

Or if you use MinGW on Windows:

C:\some\dir\colormaps-opencv> mkdir build
C:\some\dir\colormaps-opencv> cd build
C:\some\dir\colormaps-opencv\build> cmake -G "MinGW Makefiles" ..
C:\some\dir\colormaps-opencv\build> mingw32-make
C:\some\dir\colormaps-opencv\build> cm_demo.exe /path/to/your/image.ext

Or if you are using Visual Studio, you can generate yourself a solution like this (please adjust to your version):

C:\some\dir\colormaps-opencv> mkdir build
C:\some\dir\colormaps-opencv> cd build
C:\some\dir\colormaps-opencv\build> cmake -G "Visual Studio 9 2008" ..
comments powered by Disqus