Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV: how to apply rainbow gradient map on an image?

Say we had an image we somehow modified via openCV:

enter image description here

And now we would love to apply to it Gradient Map (like one we can apply via photoshop):

enter image description here

So I wonder how to apply gradient map (rainbow colors) via openCV?

like image 809
myWallJSON Avatar asked Apr 06 '12 23:04

myWallJSON


People also ask

How do I apply a color map in OpenCV?

OpenCV - Color Maps. In OpenCV, you can apply different color maps to an image using the method applyColorMap() of the class Imgproc. src − An object of the class Mat representing the source (input) image.

What are the different types of gradient filters in OpenCV?

OpenCV provides three types of gradient filters or High-pass filters, Sobel, Scharr and Laplacian. We will see each one of them. 1. Sobel and Scharr Derivatives Sobel operators is a joint Gausssian smoothing plus differentiation operation, so it is more resistant to noise.

What is an image gradient?

Image gradients are one of the most important image processing and computer vision building blocks you’ll learn about. Behind the scenes, they are used for powerful image descriptor methods such as Histogram of Oriented Gradients and SIFT. They are used to construct saliency maps to reveal the most “interesting” regions of an image.

How do I choose the type of the colormap?

You can choose the type of the colormap you need, by passing its respective predefined value to the parameter named colormap of the applyColorMap () method. Following are the values representing various types of color maps and their respective outputs.


2 Answers

Here is a method to create false/pseudo-color images using Python, conversion to c++ should be very straightforward. Overview:

  1. Open your image as grayscale, and RGB
  2. Convert the RGB image to HSV (Hue, Saturation, Value/Brightness) color space. This is a cylindrical space, with hue represented by a single value on the polar axis.
  3. Set the hue channel to the grayscale image we already opened, this is the crucial step.
  4. Set value, and saturation channels both to maximal values.
  5. Convert back to RGB space (otherwise display will be incorrect).

There are a couple of catches though...

  1. As Hue is held in degrees and the color spectrum is represented from 0 to 180 (not 0-256 and not 0-360 (sometimes the case)), we need to rescale the grayscale image appropriately by multiplying by 180 / 256.0
  2. In the opencv case the hue colorscale starts at blue (not red, as in your image). ie. the mapping goes like this:

from: enter image description here to: enter image description here

If this is important to change we can do so by offsetting all the hue elements and wrapping them around 180 (otherwise it will saturate). The code does this by masking the image at this cut off point and then offsetting appropriately. Using an offset of 120, generates your colorscale:

from: enter image description here to: enter image description here

and the image processed this way seems to match yours very well (at end).

import cv

image_bw = cv.LoadImage("TfBmw.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE)
image_rgb = cv.LoadImage("TfBmw.jpg")

#create the image arrays we require for the processing
hue=cv.CreateImage((image_rgb.width,image_rgb.height), cv.IPL_DEPTH_8U, 1)
sat=cv.CreateImage((image_rgb.width,image_rgb.height), cv.IPL_DEPTH_8U, 1)
val=cv.CreateImage((image_rgb.width,image_rgb.height), cv.IPL_DEPTH_8U, 1)
mask_1=cv.CreateImage((image_rgb.width,image_rgb.height), cv.IPL_DEPTH_8U, 1)
mask_2=cv.CreateImage((image_rgb.width,image_rgb.height), cv.IPL_DEPTH_8U, 1)

#convert to cylindrical HSV color space
cv.CvtColor(image_rgb,image_rgb,cv.CV_RGB2HSV)
#split image into component channels
cv.Split(image_rgb,hue,sat,val,None)
#rescale image_bw to degrees
cv.ConvertScale(image_bw, image_bw, 180 / 256.0)
#set the hue channel to the greyscale image
cv.Copy(image_bw,hue)
#set sat and val to maximum
cv.Set(sat, 255)
cv.Set(val, 255)

#adjust the pseudo color scaling offset, 120 matches the image you displayed
offset=120
cv.CmpS(hue,180-offset, mask_1, cv.CV_CMP_GE)
cv.CmpS(hue,180-offset, mask_2, cv.CV_CMP_LT)
cv.AddS(hue,offset-180,hue,mask_1)
cv.AddS(hue,offset,hue,mask_2)

#merge the channels back
cv.Merge(hue,sat,val,None,image_rgb)
#convert back to RGB color space, for correct display
cv.CvtColor(image_rgb,image_rgb,cv.CV_HSV2RGB)

cv.ShowImage('image', image_rgb)
# cv.SaveImage('TfBmw_120.jpg',image_rgb)
cv.WaitKey(0)

Your image processed with offset = 120:

enter image description here

like image 189
fraxel Avatar answered Sep 21 '22 06:09

fraxel


Now exists the openCV function called applyColorMap which makes this process trivial. The following code will do the trick

image_cm = cv2.applyColorMap(image, cv2.COLORMAP_JET))

And this is the result:

Original plane Figure 1: Original plane Plane after applying colormap Figure2: Plane after applying colormap

like image 43
richar8086 Avatar answered Sep 18 '22 06:09

richar8086