Is there OpenCV colormap in python?

后端 未结 3 485
感动是毒
感动是毒 2020-12-20 22:23

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

相关标签:
3条回答
  • 2020-12-20 22:30

    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]))
    
    0 讨论(0)
  • 2020-12-20 22:32

    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.

    0 讨论(0)
  • 2020-12-20 22:35

    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

    0 讨论(0)
提交回复
热议问题