API
def applyColorMap(src, colormap, dst=None): # real signature unknown; restored from __doc__
"""
applyColorMap(src, colormap[, dst]) -> dst
. @brief Applies a GNU Octave/MATLAB equivalent colormap on a given image.
在给定图像上应用GNU Octave / MATLAB等效色图。
.
. @param src The source image, grayscale or colored of type CV_8UC1 or CV_8UC3.
源图像,灰度或彩色的CV_8UC1或CV_8UC3类型。
(CV_8UC1指8位无符号单通道矩阵、CV_8UC3指8位无符号三通道矩阵)
. @param dst The result is the colormapped source image. Note: Mat::create is called on dst.
结果是颜色映射的源图像。 注意:Mat :: create在dst上调用。
. @param colormap The colormap to apply, see #ColormapTypes
要应用的颜色图,请参见#ColormapTypes
applyColorMap(src, userColor[, dst]) -> dst
. @brief Applies a user colormap on a given image. 将用户颜色图应用于给定图像。
.
. @param src The source image, grayscale or colored of type CV_8UC1 or CV_8UC3.
源图像,灰度或彩色的CV_8UC1或CV_8UC3类型。
. @param dst The result is the colormapped source image. Note: Mat::create is called on dst.
结果是颜色映射的源图像。 注意:Mat :: create在dst上调用。
. @param userColor The colormap to apply of type CV_8UC1 or CV_8UC3 and size 256
要应用的CV_8UC1或CV_8UC3类型的颜色图,大小为256
"""
pass
colormap:查看并设置当前颜色图。
配色:通过将一个颜色方案,分配给一张图,使得一张黑白图彩色化。例如,画油画,首先是素描出物体的轮廓,接着是给画出的物体涂上适宜的颜色,这个记录不同物体对应的颜色的映射,就是配色方案。
在以下代码中,cv.applyColorMap()函数将一层的深度图(黑白)映射到三层,打印出来便是彩色图:
depth_image = cv.applyColorMap(cv.convertScaleAbs(depth_image, alpha=0.03), cv.COLORMAP_JET)
ColormapTypes
完整应用代码【将深度图的黑白图映射为彩色图】
# -*- encoding: utf-8 -*-
"""
@File : test_191123_将深度图打印成黑白图.py
@Time : 2019/11/24 15:57
@Author : Dontla
@Email : sxana@qq.com
@Software: PyCharm
"""
import pyrealsense2 as rs
import cv2 as cv
import numpy as np
pipeline = rs.pipeline()
cfg = rs.config()
cfg.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
cfg.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
profile = pipeline.start(cfg)
try:
while True:
fs = pipeline.wait_for_frames()
color_frame = fs.get_color_frame()
depth_frame = fs.get_depth_frame()
if not depth_frame or not color_frame:
continue
color_image = np.asanyarray(color_frame.get_data())
depth_image = np.asanyarray(depth_frame.get_data())
# 打印成黑白
# depth_image = cv.convertScaleAbs(depth_image, alpha=0.03)
# 打印成彩色
depth_image = cv.applyColorMap(cv.convertScaleAbs(depth_image, alpha=0.03), cv.COLORMAP_JET)
window = cv.namedWindow('window', cv.WINDOW_AUTOSIZE)
cv.imshow('window', depth_image)
cv.waitKey(1)
finally:
pipeline.stop()
map原理
具体是怎么map的暂时不用管吧,毕竟还没有精力到去看源码的地步,但猜测,对应每一种ColormapType,都有公式使被map的矩阵数值与map后的颜色的数值一一对应,执行前先判断是CV_8UC1还是CV_8UC3,到时它直接计算就好了。
能否map CV_24UC3的?
貌似也能,代码:
# -*- encoding: utf-8 -*-
"""
@File : test_191123_将深度图打印成黑白图.py
@Time : 2019/11/24 15:57
@Author : Dontla
@Email : sxana@qq.com
@Software: PyCharm
"""
import pyrealsense2 as rs
import cv2 as cv
import numpy as np
pipeline = rs.pipeline()
cfg = rs.config()
cfg.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
cfg.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
profile = pipeline.start(cfg)
try:
while True:
fs = pipeline.wait_for_frames()
color_frame = fs.get_color_frame()
depth_frame = fs.get_depth_frame()
if not depth_frame or not color_frame:
continue
color_image = np.asanyarray(color_frame.get_data())
depth_image = np.asanyarray(depth_frame.get_data())
# 打印成黑白
# depth_image = cv.convertScaleAbs(depth_image, alpha=0.03)
# 打印成彩色
# depth_image = cv.applyColorMap(cv.convertScaleAbs(depth_image, alpha=0.03), cv.COLORMAP_JET)
# 测试是否能map CV_24UC3的
color_image = cv.applyColorMap(color_image, cv.COLORMAP_JET)
window = cv.namedWindow('window', cv.WINDOW_AUTOSIZE)
cv.imshow('window', color_image)
cv.waitKey(1)
finally:
pipeline.stop()
运行后正常的color图变成这样了。。。
只不过,不知道那是不是我所理解的CV_24UC3,因为网上查不到CV_24UC3,还是CV_24UC3根本就是CV_8UC3,不知道我的理解是否是错误的。