相关代码

# -*- coding: utf-8 -*-
"""
@File    : obstacle_detection.py
@Time    : 2019/12/11 10:24
@Author  : Dontla
@Email   : sxana@qq.com
@Software: PyCharm
"""
import timeimport numpy as np
import pyrealsense2 as rs
import cv2
import sysclass ObstacleDetection(object):def __init__(self):self.cam_serials = ['838212073161', '827312071726']def obstacle_detection(self):# 摄像头个数(在这里设置所需使用摄像头的总个数)cam_num = 6ctx = rs.context()'''连续验证机制'''# D·C 1911202:创建最大验证次数max_veri_times;创建连续稳定值continuous_stable_value,用于判断设备重置后是否处于稳定状态max_veri_times = 100continuous_stable_value = 10print('\n', end='')print('开始连续验证,连续验证稳定值:{},最大验证次数:{}:'.format(continuous_stable_value, max_veri_times))continuous_value = 0veri_times = 0while True:devices = ctx.query_devices()connected_cam_num = len(devices)if connected_cam_num == cam_num:continuous_value += 1if continuous_value == continuous_stable_value:breakelse:continuous_value = 0veri_times += 1if veri_times == max_veri_times:print("检测超时,请检查摄像头连接!")sys.exit()print('摄像头个数:{}'.format(connected_cam_num))'''循环reset摄像头'''# hardware_reset()后是不是应该延迟一段时间?不延迟就会报错print('\n', end='')print('开始初始化摄像头:')for dev in ctx.query_devices():# 先将设备的序列号放进一个变量里,免得在下面for循环里访问设备的信息过多(虽然不知道它会不会每次都重新访问)dev_serial = dev.get_info(rs.camera_info.serial_number)# 匹配序列号,重置我们需重置的特定摄像头(注意两个for循环顺序,哪个在外哪个在内很重要,不然会导致刚重置的摄像头又被访问导致报错)for serial in self.cam_serials:if serial == dev_serial:dev.hardware_reset()# 像下面这条语句居然不会报错,不是刚刚才重置了dev吗?莫非区别在于没有通过for循环ctx.query_devices()去访问?# 是不是刚重置后可以通过ctx.query_devices()去查看有这个设备,但是却没有存储设备地址?如果是这样,# 也就能够解释为啥能够通过len(ctx.query_devices())函数获取设备数量,但访问序列号等信息就会报错的原因了print('摄像头{}初始化成功'.format(dev.get_info(rs.camera_info.serial_number)))'''连续验证机制'''# D·C 1911202:创建最大验证次数max_veri_times;创建连续稳定值continuous_stable_value,用于判断设备重置后是否处于稳定状态print('\n', end='')print('开始连续验证,连续验证稳定值:{},最大验证次数:{}:'.format(continuous_stable_value, max_veri_times))continuous_value = 0veri_times = 0while True:devices = ctx.query_devices()connected_cam_num = len(devices)if connected_cam_num == cam_num:continuous_value += 1if continuous_value == continuous_stable_value:breakelse:continuous_value = 0veri_times += 1if veri_times == max_veri_times:print("检测超时,请检查摄像头连接!")sys.exit()print('摄像头个数:{}'.format(connected_cam_num))'''配置各个摄像头的基本对象'''pipeline1 = rs.pipeline(ctx)pipeline2 = rs.pipeline(ctx)config1 = rs.config()config2 = rs.config()config1.enable_device(self.cam_serials[0])config2.enable_device(self.cam_serials[1])config1.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)config2.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)config1.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)config2.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)pipeline1.start(config1)pipeline2.start(config2)align1 = rs.align(rs.stream.color)align2 = rs.align(rs.stream.color)'''运行摄像头'''try:while True:frames1 = pipeline1.wait_for_frames()# print(np.asanyarray(frames1.get_depth_frame().get_data()).shape)  # (480, 640)frames2 = pipeline2.wait_for_frames()aligned_frames1 = align1.process(frames1)aligned_frames2 = align2.process(frames2)aligned_depth_frame1 = aligned_frames1.get_depth_frame()# print(np.asanyarray(aligned_depth_frame1.get_data()).shape)  # (480, 640)aligned_depth_frame2 = aligned_frames2.get_depth_frame()color_frame1 = aligned_frames1.get_color_frame()color_frame2 = aligned_frames2.get_color_frame()if not aligned_depth_frame1 \or not color_frame1 \or not aligned_depth_frame2 \or not color_frame2:continuecolor_profile1 = color_frame1.get_profile()color_profile2 = color_frame2.get_profile()cvsprofile1 = rs.video_stream_profile(color_profile1)cvsprofile2 = rs.video_stream_profile(color_profile2)color_intrin1 = cvsprofile1.get_intrinsics()color_intrin2 = cvsprofile2.get_intrinsics()color_intrin_part1 = [color_intrin1.ppx, color_intrin1.ppy, color_intrin1.fx, color_intrin1.fy]color_intrin_part2 = [color_intrin2.ppx, color_intrin2.ppy, color_intrin2.fx, color_intrin2.fy]color_image1 = np.asanyarray(color_frame1.get_data())# 【191216 转换成灰度图并二值化】color_image1_gray = cv2.cvtColor(color_image1, cv2.COLOR_BGR2GRAY)# print(color_image1_gray.shape)  # (480, 640)print(color_image1_gray)color_image2 = np.asanyarray(color_frame2.get_data())depth_image1 = np.asanyarray(aligned_depth_frame1.get_data())# print(depth_image1.shape)  # (480, 640)print(depth_image1)depth_image2 = np.asanyarray(aligned_depth_frame2.get_data())# depth_colormap1 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image1, alpha=0.0425), cv2.COLORMAP_JET)depth_colormap2 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image2, alpha=0.03), cv2.COLORMAP_JET)# image1 = np.hstack((color_image1, depth_colormap1))# print(depth_image1.shape)  # (480, 640)image1 = np.hstack((color_image1_gray, depth_image1))# print(image1.shape)  # (480, 1280)image2 = np.hstack((color_image2, depth_colormap2))# cv2.imshow('win1', image1)cv2.imshow('win1', image1)cv2.imshow('win2', image2)cv2.waitKey(1)finally:pipeline1.stop()pipeline2.stop()if __name__ == '__main__':ObstacleDetection().obstacle_detection()

说明

color图转换成灰度图后其shape为(480, 640),depth图的shape也是(480, 640),按理说水平堆叠不会出问题。

打印数据后才发现,原来depth中的数据没有被map到[0, 255],导致cv2.imshow()函数自动map堆叠后的数据了(估计是将最大的值对应于255,然后得出比例系数,然后将所有数据乘以这个比例系数,所以这就是为啥灰度图没法正常显示了),所以打印成了下面这样:

解决办法

手动将depth数据map到[0,255]:

好了:

修改后代码

# -*- coding: utf-8 -*-
"""
@File    : obstacle_detection.py
@Time    : 2019/12/11 10:24
@Author  : Dontla
@Email   : sxana@qq.com
@Software: PyCharm
"""
import timeimport numpy as np
import pyrealsense2 as rs
import cv2
import sysclass ObstacleDetection(object):def __init__(self):self.cam_serials = ['838212073161', '827312071726']def obstacle_detection(self):# 摄像头个数(在这里设置所需使用摄像头的总个数)cam_num = 6ctx = rs.context()'''连续验证机制'''# D·C 1911202:创建最大验证次数max_veri_times;创建连续稳定值continuous_stable_value,用于判断设备重置后是否处于稳定状态max_veri_times = 100continuous_stable_value = 10print('\n', end='')print('开始连续验证,连续验证稳定值:{},最大验证次数:{}:'.format(continuous_stable_value, max_veri_times))continuous_value = 0veri_times = 0while True:devices = ctx.query_devices()connected_cam_num = len(devices)if connected_cam_num == cam_num:continuous_value += 1if continuous_value == continuous_stable_value:breakelse:continuous_value = 0veri_times += 1if veri_times == max_veri_times:print("检测超时,请检查摄像头连接!")sys.exit()print('摄像头个数:{}'.format(connected_cam_num))'''循环reset摄像头'''# hardware_reset()后是不是应该延迟一段时间?不延迟就会报错print('\n', end='')print('开始初始化摄像头:')for dev in ctx.query_devices():# 先将设备的序列号放进一个变量里,免得在下面for循环里访问设备的信息过多(虽然不知道它会不会每次都重新访问)dev_serial = dev.get_info(rs.camera_info.serial_number)# 匹配序列号,重置我们需重置的特定摄像头(注意两个for循环顺序,哪个在外哪个在内很重要,不然会导致刚重置的摄像头又被访问导致报错)for serial in self.cam_serials:if serial == dev_serial:dev.hardware_reset()# 像下面这条语句居然不会报错,不是刚刚才重置了dev吗?莫非区别在于没有通过for循环ctx.query_devices()去访问?# 是不是刚重置后可以通过ctx.query_devices()去查看有这个设备,但是却没有存储设备地址?如果是这样,# 也就能够解释为啥能够通过len(ctx.query_devices())函数获取设备数量,但访问序列号等信息就会报错的原因了print('摄像头{}初始化成功'.format(dev.get_info(rs.camera_info.serial_number)))'''连续验证机制'''# D·C 1911202:创建最大验证次数max_veri_times;创建连续稳定值continuous_stable_value,用于判断设备重置后是否处于稳定状态print('\n', end='')print('开始连续验证,连续验证稳定值:{},最大验证次数:{}:'.format(continuous_stable_value, max_veri_times))continuous_value = 0veri_times = 0while True:devices = ctx.query_devices()connected_cam_num = len(devices)if connected_cam_num == cam_num:continuous_value += 1if continuous_value == continuous_stable_value:breakelse:continuous_value = 0veri_times += 1if veri_times == max_veri_times:print("检测超时,请检查摄像头连接!")sys.exit()print('摄像头个数:{}'.format(connected_cam_num))'''配置各个摄像头的基本对象'''pipeline1 = rs.pipeline(ctx)pipeline2 = rs.pipeline(ctx)config1 = rs.config()config2 = rs.config()config1.enable_device(self.cam_serials[0])config2.enable_device(self.cam_serials[1])config1.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)config2.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)config1.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)config2.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)pipeline1.start(config1)pipeline2.start(config2)align1 = rs.align(rs.stream.color)align2 = rs.align(rs.stream.color)'''运行摄像头'''try:while True:frames1 = pipeline1.wait_for_frames()# print(np.asanyarray(frames1.get_depth_frame().get_data()).shape)  # (480, 640)frames2 = pipeline2.wait_for_frames()aligned_frames1 = align1.process(frames1)aligned_frames2 = align2.process(frames2)aligned_depth_frame1 = aligned_frames1.get_depth_frame()# print(np.asanyarray(aligned_depth_frame1.get_data()).shape)  # (480, 640)aligned_depth_frame2 = aligned_frames2.get_depth_frame()color_frame1 = aligned_frames1.get_color_frame()color_frame2 = aligned_frames2.get_color_frame()if not aligned_depth_frame1 \or not color_frame1 \or not aligned_depth_frame2 \or not color_frame2:continuecolor_profile1 = color_frame1.get_profile()color_profile2 = color_frame2.get_profile()cvsprofile1 = rs.video_stream_profile(color_profile1)cvsprofile2 = rs.video_stream_profile(color_profile2)color_intrin1 = cvsprofile1.get_intrinsics()color_intrin2 = cvsprofile2.get_intrinsics()color_intrin_part1 = [color_intrin1.ppx, color_intrin1.ppy, color_intrin1.fx, color_intrin1.fy]color_intrin_part2 = [color_intrin2.ppx, color_intrin2.ppy, color_intrin2.fx, color_intrin2.fy]color_image1 = np.asanyarray(color_frame1.get_data())# 【191216 转换成灰度图并二值化】color_image1_gray = cv2.cvtColor(color_image1, cv2.COLOR_BGR2GRAY)color_image2 = np.asanyarray(color_frame2.get_data())depth_image1 = np.asanyarray(aligned_depth_frame1.get_data())depth_image1_convertScaleAbs = cv2.convertScaleAbs(depth_image1, alpha=0.03)depth_image2 = np.asanyarray(aligned_depth_frame2.get_data())depth_colormap1 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image1, alpha=0.0425), cv2.COLORMAP_JET)depth_colormap2 = cv2.applyColorMap(cv2.convertScaleAbs(depth_image2, alpha=0.03), cv2.COLORMAP_JET)# image1 = np.hstack((color_image1, depth_colormap1))image1 = np.hstack((color_image1_gray, depth_image1_convertScaleAbs))image2 = np.hstack((color_image2, depth_colormap2))cv2.imshow('win1', image1)cv2.imshow('win2', image2)cv2.waitKey(1)finally:pipeline1.stop()pipeline2.stop()if __name__ == '__main__':ObstacleDetection().obstacle_detection()

Intel Realsense D435 opencv 为什么将color图转换成灰度图后,再与depth图水平堆叠,其结果一片黑色?(数据未map到0-255)相关推荐

  1. 利用OpenCV把一幅彩色图像转换成灰度图

    图像灰度化的目的是为了简化矩阵,提高运算速度. 彩色图像中的每个像素颜色由R.G.B三个分量来决定,而每个分量的取值范围都在0-255之间,这样对计算机来说,彩色图像的一个像素点就会有256*256* ...

  2. opencv:把三通道图转换成灰度图、二值图

    #include <opencv2/core/core.hpp> #include<opencv2/highgui/highgui.hpp> #include<openc ...

  3. Javascript图像处理之将彩色图转换成灰度图

    最近看了Justany_WhiteSnow的Javascript图像处理一文,写的非常好,于是就练练手,略做了一些封装: (function () {function imageToGray(iCan ...

  4. er图转换成关系模型的例题,将ER图转换为关系模型

    I know how to convert an entity set, relationship, etc. into the relational model but what i wonder ...

  5. 彩色BMP转换成灰度图的原理

    图像处理中,大部分的处理方法都需要事先把彩色图转换成灰度图才能进行相关的计算.识别. 彩色图转换灰度图的原理如下: 我们知道彩色位图是由R/G/B三个分量组成,其文件存储格式为 BITMAPFILEH ...

  6. Windows Forms:在C#中将图像转换成灰度图

    Windows Forms:在C#中将图像转换成灰度图 本文翻译自Windows Forms: Convert an image into grayscale in C# 这篇文章向你展示在C# Wi ...

  7. python opencv cv.applyColorMap()函数(颜色映射)ColormapTypes【将Intel Realsense D435深度图的黑白图映射为彩色图】

    文章目录 API ColormapTypes 完整应用代码[将深度图的黑白图映射为彩色图] map原理 能否map CV_24UC3的? API def applyColorMap(src, colo ...

  8. Tensorflow_yolov3 Intel Realsense D435奇怪的现象,多摄像头连接时一旦能检测到深度马上就会卡(卡住)

    两个摄像头连接时一旦能检测到深度马上就会卡(小于30公分),,单个摄像头没事,这是使用了多线程传输后的现象,不知咋回事... 后来加了这句验证全局变量是否存在,好像好点了,有待验证 20200401 ...

  9. Intel Realsense D435运行报错 RuntimeError: Camera not connected! dev.hardware_reset()函数需加睡眠sleep()

    解决方案: 参考:Intel Realsense D435报错 RuntimeError: MFCreateDeviceSource(_device_attrs, &_source) retu ...

最新文章

  1. 【程序员趣味】用Python制作带字小人举牌
  2. pip如何安装whl
  3. 大型企业网络运维,ACL,VTP,NAT,vlan.总合。
  4. WinAPI: waveOutGetPlaybackRate - 获取输出设备当前的播放速度设置(默认速度值的倍数)...
  5. 二叉树的层序遍历 使用队列和不使用队列
  6. 从tcp到netty(一)
  7. java面试题2014_Java生态系统– 2014年我的5大亮点
  8. 【定时同步系列3】8PSK调制+OM定时+信号分段处理+误码率曲线之MATLAB仿真(实信号模型)
  9. python----流程控制
  10. c++数据结构队列栈尸体_数据结构-栈与队列(二)
  11. [递归][重心] Luogu P4886 快递员
  12. workman 心跳
  13. 华为电脑管家PcManager多屏协同功能破解
  14. 【掩码机制】解决LSTM中特征长度不一致问题
  15. android 开机自动运行程序
  16. 编曲宿主DAW是什么 2023年编曲宿主软件哪个好用
  17. 企业信息化战略与实施
  18. 上传图片到php服务器
  19. 运用hadoop计算TF-IDF
  20. 低温环境对电池的影响

热门文章

  1. 注意我写的OTA短信同步设置的代码有很多问题
  2. LSMW批处理使用方法(12)_步骤16、17
  3. 4、Power Query-智能汇总工作簿下的指定或所有工作表数据
  4. ABAP中将字符格式的金额转换为数值的函数
  5. ABAP OO小例子
  6. 一招教你解决大数据量下的各种报表使用问题
  7. 将矩阵转为一行_LeetCode1253:矩阵重构
  8. mysql交互式创建表_用mysql语句创建数据表详细教程
  9. python消费datahub_DataHub使用指南-阿里云开发者社区
  10. bminfowindow是什么_三步实现地图自定义InfoWindow