python opencv cv.applyColorMap()函数(颜色映射)ColormapTypes【将Intel Realsense D435深度图的黑白图映射为彩色图】
生活随笔
收集整理的这篇文章主要介绍了
python opencv cv.applyColorMap()函数(颜色映射)ColormapTypes【将Intel Realsense D435深度图的黑白图映射为彩色图】
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
文章目录
- API
- ColormapTypes
- 完整应用代码【将深度图的黑白图映射为彩色图】
- map原理
- 能否map CV_24UC3的?
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要应用的颜色图,请参见#ColormapTypesapplyColorMap(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"""passcolormap:查看并设置当前颜色图。
配色:通过将一个颜色方案,分配给一张图,使得一张黑白图彩色化。例如,画油画,首先是素描出物体的轮廓,接着是给画出的物体涂上适宜的颜色,这个记录不同物体对应的颜色的映射,就是配色方案。
在以下代码中,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 nppipeline = 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:continuecolor_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 nppipeline = 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:continuecolor_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,不知道我的理解是否是错误的。
参考文章1:Matlab函数解释:colormap
参考文章2:cv::ColormapTypes
参考文章3:Intel Realsense D435 将深度图的灰度图映射为彩色图,打印输出灰度图或彩色图
总结
以上是生活随笔为你收集整理的python opencv cv.applyColorMap()函数(颜色映射)ColormapTypes【将Intel Realsense D435深度图的黑白图映射为彩色图】的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Intel Realsense D435
- 下一篇: python 并行、并发以及多线程的概念