Applying colormaps

We'll end this chapter by learning about applying colormaps to images. This is a fairly simple but powerful method that can be used to modify the colors of an image or its tone in general. This algorithm simply replaces the colors of an input image using a colormap and creates a result. A colormap is a 256-element array of color values, in which each element represents the color that must be used for the corresponding pixel values in the source image. We will break this down further with a couple of examples, but before that, let's see how colormaps are applied to images.

OpenCV contains a function named applyColorMap that can be used to either apply predefined colormaps or custom ones created by the user. In case predefined colormaps are used, applyColorMap must be provided with a colormap type, which must be an entry from the ColormapTypes enum. Here's an example:

ColormapTypes colormap = COLORMAP_JET; 
applyColorMap(image, 
              result, 
              colormap); 

The following image depicts the result of various predefined colormaps that can be applied using the applyColorMap function:

As mentioned earlier, you can also create your own custom colormaps. You just need to make sure you follow the instructions for creating a colormap. Your colormap must have a size of 256 elements (a Mat object with 256 rows and 1 column) and it must contain color or grayscale values, depending on the type of image you are planning to apply the colormap to. The following is an example that shows how to create a custom colormap by simply inverting the green channel color:

Mat userColor(256, 1, CV_8UC3); 
for(int i=0; i<=255; i++) 
    userColor.at<Vec3b>(i,0) = Vec3b(i, 255-i, i); 
applyColorMap(image, 
              result, 
              userColor); 

Here is the result of the preceding example code:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.138.104