我知道 Matlab, matplotlib style colormap in OpenCV 。该文档解释了它对C ++的用法。我想知道是否存在使用cv2的python这样的选项。我google了很多,什么都没发现。我知道matplotlib的colormap选项,我可以使用,但如果cv2提供这样的选项,我可以消除将matplotlib colormaps转换为opencv图像的开销。它笨拙。我需要它用于我的项目。
答案 0 :(得分:7)
对于OpenCV 2.4.11,applyColorMap
适用于Python(即使2.4.11 docs仍然只列出C ++):
import cv2
im = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE)
imC = cv2.applyColorMap(im, cv2.COLORMAP_JET)
答案 1 :(得分:2)
答案 2 :(得分:1)
可悲的是,OpenCV没有任何colorMap,但你可以写一个。没那么困难。
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]))
答案 3 :(得分:0)
无法在 Python 中使用之前的 applyColorMap 示例。 以为我分享。 我为“胖”道歉。 如果您的摄像机未被识别,或者有多个摄像机,请将“1”替换为“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)
while True:
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 here
if cv2.waitKey(1) & 0xFF == ord('q'):
break