I am aware of Matlab, matplotlib style colormap in OpenCV . The documentation explains its usage for C++. I was wondering if such an option exists for python using cv2 as w
Sadly OpenCV doesn't have any colorMap but you can write one. Not that difficult.
class ColorMap:
startcolor = ()
endcolor = ()
startmap = 0
endmap = 0
colordistance = 0
valuerange = 0
ratios = []
def __init__(self, startcolor, endcolor, startmap, endmap):
self.startcolor = np.array(startcolor)
self.endcolor = np.array(endcolor)
self.startmap = float(startmap)
self.endmap = float(endmap)
self.valuerange = float(endmap - startmap)
self.ratios = (self.endcolor - self.startcolor) / self.valuerange
def __getitem__(self, value):
color = tuple(self.startcolor + (self.ratios * (value - self.startmap)))
return (int(color[0]), int(color[1]), int(color[2]))
For OpenCV 2.4.11, applyColorMap
works in Python (even though the 2.4.11 docs still list only C++):
import cv2
im = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE)
imC = cv2.applyColorMap(im, cv2.COLORMAP_JET)
See also this Stack Overflow answer.
shame, it looks like it did not make it into the python api yet. but you could have a look at the implementation in modules/contrib/src/colormap.cpp, e.g. the jetmap is only a lookup-table, you could just steal it