cfischer
cfischer

Reputation: 24902

False color filter in OpenCV

I'm trying to find some false color filter on OpenCV, and scourged the docs to no avail. I wonder if it's called something else in OpenCV, as it seems common enough to be implemented in the framework.

How can apply this filter?

Upvotes: 1

Views: 3682

Answers (4)

user5747799
user5747799

Reputation:

Here is a handy function I created written in Python taken out of a project which applies a colormap for pseudocolor/False Color which I used to distinguish temperature changes nicely using input from a thermal imaging device. This function can be retrofit however you like.

Code provides a mechanism to toggle between all the different OpenCV filters as well as the option to use a custom colormap.

Set filter_mode to 0-11 to apply a OpenCV filter. Otherwise set filter_mode to 12, or 13 to use a custom 1x256 RGB image gradient as the filter to transform the greyscale image

See usage below...

import cv2 # import OpenCV
...
def apply_colormap_filter(cv2_image, filter_mode) :
        # A colormap for psudocolors/false color to distinguish hot spots in image
        # input: [cv2_image] the greyscale input, [filter_mode] filter selection (0-13)

        if filter_mode >= 12 : # lut image constraint: image has to be RGB of size 1x256
            ###### custom colormaps ######
            if filter_mode == 12 :
                lut = cv2.imread('thermal-colormap.png') # 1x256 RGB image
            elif filter_mode == 13 :
                lut = cv2.imread('lut_sky.png') # 1x256 RGB image
            else :
                lut = cv2.imread('lut_sky.png') # 1x256 RGB image

            # APPLY CUSTOM FILTER
            cv2_image = cv2.cvtColor(cv2_image, cv2.COLOR_GRAY2BGR);

            final = cv2.LUT(cv2_image, lut)

            ###### custom colormaps ######
        elif filter_mode < 12 :
            ###### Utilise OpenCL inbuilt filters ######

            final = cv2.applyColorMap(cv2_image, filter_mode)

            ###### Utilise OpenCL inbuilt filters ######
        return final # return the resultant False-colored image

gray = cv2.imread('grayscale-image.png')  # some grayscale image of variable dimensions
thermal = apply_colormap_filter(gray, 12) # Use custom filter
cv2.imshow('Thermal Image', thermal)

Upvotes: 0

ypnos
ypnos

Reputation: 52337

There are several methods of false coloring. Typically, the image is a composite of three intensity images.

  1. create three 1-channel matrices that contain the values you want to depict. To reuse channels from existing matrices, use cv::split().
  2. apply proper normalization on each matrix (also depends on step 4)
  3. create a 3-channel matrix with cv::merge()
  4. optional: use cv::cvtColor() to change the meaning of these channels. E.g.: convert from HSV to RGB such that the first input variable will be the hue, second the saturation, third the value, of the false-color image.

The other case, where a grayscale image is blown-up to three colors using a continuous palette, is called pseudo color. See the answer by @melnibon.

Upvotes: 0

melnibon
melnibon

Reputation: 36

You could use applyColorMap : http://docs.opencv.org/trunk/modules/contrib/doc/facerec/colormaps.html

#include <opencv2/contrib/contrib.hpp>
cv::Mat grayImage...
cv::falseColored;
cv::applyColorMap(grayImage, falseColored, cv::COLORMAP_JET);

They are many false-color-maps available.

Upvotes: 2

Boyko Perfanov
Boyko Perfanov

Reputation: 3047

Failing how to implement it, you can take a more general approach:

  • Convert the image to HSV
  • Set the Hue channel of every pixel to your input hue.

Upvotes: 1

Related Questions