ColorMaps in OpenCV

Overview

The human perception isn’t built for observing fine changes in grayscale images. More…

// enums

enum cv::ColormapTypes;

// global functions

void
cv::applyColorMap(
    InputArray src,
    OutputArray dst,
    int colormap
    );

void
cv::applyColorMap(
    InputArray src,
    OutputArray dst,
    InputArray userColor
    );

Detailed Documentation

The human perception isn’t built for observing fine changes in grayscale images. Human eyes are more sensitive to observing changes between colors, so you often need to recolor your grayscale images to get a clue about them. OpenCV now comes with various colormaps to enhance the visualization in your computer vision application.

In OpenCV you only need applyColorMap to apply a colormap on a given image. The following sample code reads the path to an image from command line, applies a Jet colormap on it and shows the result:

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;

#include <iostream>
using namespace std;

int main(int argc, const char *argv[])
{
    // We need an input image. (can be grayscale or color)
    if (argc < 2)
    {
        cerr << "We need an image to process here. Please run: colorMap [path_to_image]" << endl;
        return -1;
    }
    Mat img_in = imread(argv[1]);
    if(img_in.empty())
    {
        cerr << "Sample image (" << argv[1] << ") is empty. Please adjust your path, so it points to a valid input image!" << endl;
        return -1;
    }
    // Holds the colormap version of the image:
    Mat img_color;
    // Apply the colormap:
    applyColorMap(img_in, img_color, COLORMAP_JET);
    // Show the result:
    imshow("colorMap", img_color);
    waitKey(0);
    return 0;
}

See also:

cv::ColormapTypes

Global Functions

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

Applies a GNU Octave/MATLAB equivalent colormap on a given image.

Parameters:

src The source image, grayscale or colored of type CV_8UC1 or CV_8UC3.
dst The result is the colormapped source image. Note: Mat::create is called on dst.
colormap The colormap to apply, see cv::ColormapTypes
void
cv::applyColorMap(
    InputArray src,
    OutputArray dst,
    InputArray userColor
    )

Applies a user colormap on a given image.

Parameters:

src The source image, grayscale or colored of type CV_8UC1 or CV_8UC3.
dst The result is the colormapped source image. Note: Mat::create is called on dst.
userColor The colormap to apply of type CV_8UC1 or CV_8UC3 and size 256