Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there OpenCV colormap in python?

Tags:

python

opencv

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 well. I googled a lot to find nothing. I am aware of matplotlib's colormap option that I can use but if cv2 provides such option, I can remove the overhead of converting the matplotlib colormaps to opencv images. Its clumsy. I require it for my project.

like image 485
Yash Avatar asked Feb 23 '13 17:02

Yash


People also ask

Does OpenCV-Python have cv2?

cv2 is the module import name for opencv-python, "Unofficial pre-built CPU-only OpenCV packages for Python".

What is CMAP in OpenCV?

Define a colormap : A colormap is a mapping from 0-255 values to 256 colors. In OpenCV, we need to create an 8-bit color image of size 256 x 1 to store the 256 color values. Map the colors using a lookup table : In OpenCV you can apply a colormap stored in a 256 x 1 color image to an image using a lookup table LUT.

Is cv2 a library in Python?

OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2. imread() method loads an image from the specified file.

Is OpenCV and cv2 same?

cv2 (old interface in old OpenCV versions was named as cv ) is the name that OpenCV developers chose when they created the binding generators. This is kept as the import name to be consistent with different kind of tutorials around the internet.


3 Answers

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.

like image 176
Ulrich Stern Avatar answered Oct 22 '22 17:10

Ulrich Stern


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

like image 25
berak Avatar answered Oct 22 '22 18:10

berak


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]))
like image 40
Froyo Avatar answered Oct 22 '22 19:10

Froyo