Skip to content Skip to sidebar Skip to footer

Is There Opencv Colormap In Python?

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

Solution 1:

For OpenCV 2.4.11, applyColorMap works in Python (even though the 2.4.11 docs still list only C++):

importcv2im= cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE)
imC = cv2.applyColorMap(im, cv2.COLORMAP_JET)

See also this Stack Overflow answer.

Solution 2:

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

Solution 3:

Sadly OpenCV doesn't have any colorMap but you can write one. Not that difficult.

classColorMap:
    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]))

Solution 4:

Couldn't get the previous examples of applyColorMap in Python to work. Thought I share. I apologize for the 'fat'. If your video camera isn't being recognized, or have mult cameras, substitute '1' for '0'

import cv2
import numpy as np

frameWidth = 940
frameHeight = 680
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)  # 3=width, 4=height
cap.set(4, frameHeight)

whileTrue:
    success, imgColor, = cap.read()
    img = cv2.resize(imgColor, (frameWidth, frameHeight))  # if want to resize frame# ORIG IMG
    cv2.moveWindow("img", 0, 0)  # relocate shift reposition move so frame is at top left corner of monitor
    cv2.imshow("img", img)
    
    # COLOR ENHANCED
    cv2.moveWindow("imgColor", frameWidth, 0)  # relocate shift reposition move to side by side# COLORMAP_AUTUMN = 0# COLORMAP_BONE = 1# COLORMAP_COOL = 8# COLORMAP_HOT = 11# COLORMAP_HSV = 9# COLORMAP_JET = 2# COLORMAP_OCEAN = 5# COLORMAP_PINK = 10# COLORMAP_RAINBOW = 4# COLORMAP_SPRING = 7# COLORMAP_SUMMER = 6# COLORMAP_WINTER = 3
    cv2.imshow("imgColor", cv2.applyColorMap(imgColor, 3))  # change the last variable in hereif cv2.waitKey(1) & 0xFF == ord('q'):
        break

Post a Comment for "Is There Opencv Colormap In Python?"