引用文章:python 从深度相机realsense生成pcl点云

从深度相机realsense生成pcl点云

  • 一、通过realsense取得深度信息和彩色信息
  • 二、获取坐标和色彩信息
  • 三、通过pcl可视化点云

一、通过realsense取得深度信息和彩色信息

ubuntu下intel realsense的软件可以打开realsen的界面,里面可以得到彩色图像和深度图像,我们通过realsense的python接口获取彩色信息和深度信息。

  1. 基础的获取彩色和深度信息,realsense中的视频流转换为python的numpy格式,通过opencv输出
import pyrealsense2 as rs
import numpy as np
import cv2
import pclif __name__ == "__main__":# Configure depth and color streamspipeline = rs.pipeline()config = rs.config()config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)# Start streamingpipeline.start(config)try:while True:# Wait for a coherent pair of frames: depth and colorframes = pipeline.wait_for_frames()depth_frame = frames.get_depth_frame()color_frame = frames.get_color_frame()if not depth_frame or not color_frame:continue# Convert images to numpy arraysdepth_image = np.asanyarray(depth_frame.get_data())color_image = np.asanyarray(color_frame.get_data())# Apply colormap on depth image (image must be converted to 8-bit per pixel first)depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)# Stack both images horizontallyimages = np.hstack((color_image, depth_colormap))# Show imagescv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)cv2.imshow('RealSense', images)key = cv2.waitKey(1)# Press esc or 'q' to close the image windowif key & 0xFF == ord('q') or key == 27:cv2.destroyAllWindows()breakfinally:# Stop streamingpipeline.stop()
  1. 获取内参和保存图片
    分别用opencv和scipy.misc保存图片,略微会有一些差异,同时我们也获取了相机参数。
import pyrealsense2 as rs
import numpy as np
import cv2
import scipy.misc
import pcldef get_image():# Configure depth and color streamspipeline = rs.pipeline()config = rs.config()config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)# Start streamingpipeline.start(config)#获取图像,realsense刚启动的时候图像会有一些失真,我们保存第100帧图片。for i in range(100):data = pipeline.wait_for_frames()depth = data.get_depth_frame()color = data.get_color_frame()#获取内参dprofile = depth.get_profile()cprofile = color.get_profile()cvsprofile = rs.video_stream_profile(cprofile)dvsprofile = rs.video_stream_profile(dprofile)color_intrin=cvsprofile.get_intrinsics()print(color_intrin)depth_intrin=dvsprofile.get_intrinsics()print(color_intrin)extrin = dprofile.get_extrinsics_to(cprofile)print(extrin)depth_image = np.asanyarray(depth.get_data())color_image = np.asanyarray(color.get_data())# Apply colormap on depth image (image must be converted to 8-bit per pixel first)depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)cv2.imwrite('color.png', color_image)cv2.imwrite('depth.png', depth_image)cv2.imwrite('depth_colorMAP.png', depth_colormap)scipy.misc.imsave('outfile1.png', depth_image)scipy.misc.imsave('outfile2.png', color_image)

二、获取坐标和色彩信息

  1. 通过realsense摄像头,获取了顶点坐标和色彩信息。具体并不是很了解,pc.mac_to() 和 points=pc.calculate()是把色彩和深度结合了? 再通过points获取顶点坐标。我们将顶点坐标和彩色相机得到的像素存入txt文件。
def my_depth_to_cloud():pc = rs.pointcloud()points = rs.points()pipeline = rs.pipeline()config = rs.config()config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)pipe_profile = pipeline.start(config)for i in range(100):data = pipeline.wait_for_frames()depth = data.get_depth_frame()color = data.get_color_frame()frames = pipeline.wait_for_frames()depth = frames.get_depth_frame()color = frames.get_color_frame()colorful = np.asanyarray(color.get_data())colorful=colorful.reshape(-1,3)pc.map_to(color)points = pc.calculate(depth)#获取顶点坐标vtx = np.asanyarray(points.get_vertices())#获取纹理坐标#tex = np.asanyarray(points.get_texture_coordinates())with open('could.txt','w') as f:for i in range(len(vtx)):f.write(str(np.float(vtx[i][0])*1000)+' '+str(np.float(vtx[i][1])*1000)+' '+str(np.float(vtx[i][2])*1000)+' '+str(np.float(colorful[i][0]))+' '+str(np.float(colorful[i][1]))+' '+str(np.float(colorful[i][2]))+'\n')

三、通过pcl可视化点云

https://github.com/strawlab/python-pcl/blob/master/examples/example.py

  1. 在pcl中,要显示三维加色彩的点云坐标,每个点云包含了 x,y,z,rgb四个参数,特别的,rbg这个参数是由三维彩色坐标转换过来的。刚才得到的could.txt中,x,y,z,r,g,b 转换为x,y,z,rgb,只改颜色数据np.int(data[i][3])<<16|np.int(data[i][4])<<8|np.int(data[i][5])。保存为cloud_rbg.txt
    with open('could.txt','r') as f:lines = f.readlines()num=0for line in lines:l=line.strip().split()data.append([np.float(l[0]),np.float(l[1]),np.float(l[2]),np.float(l[3]),np.float(l[4]),np.float(l[5])])#data.append([np.float(l[0]), np.float(l[1]), np.float(l[2])])num = num+1with open('cloud_rgb.txt', 'w') as f:for i in range(len(data)):f.write(str(np.float(data[i][0])) + ' ' + str(np.float(data[i][1])) + ' ' + str(np.float(data[i][2]))+ ' '  + str(np.int(data[i][3])<<16|np.int(data[i][4])<<8|np.int(data[i][5]))+'\n')
  1. 显示
def visual():cloud = pcl.PointCloud_PointXYZRGB()points = np.zeros((307200,4),dtype=np.float32)n=0with open('cloud_rgb.txt','r') as f:lines = f.readlines()for line in lines:l=line.strip().split()#保存xyz时候扩大了1000倍,发现并没有用points[n][0] = np.float(l[0])/1000points[n][1] = np.float(l[1])/1000points[n][2] = np.float(l[2])/1000points[n][3] = np.int(l[3])n=n+1print(points[0:100]) #看看数据是怎么样的cloud.from_array(points) #从array构建点云的方式visual = pcl.pcl_visualization.CloudViewing()visual.ShowColorCloud(cloud)v = Truewhile v:v = not (visual.WasStopped())
  1. 想生成pcd,再读取pcd,但是下面的程序在可视化的时候报错
def txt2pcd():import timefilename='cloud_rgb.txt'print("the input file name is:%r." % filename)start = time.time()print("open the file...")file = open(filename, "r+")count = 0# 统计源文件的点数for line in file:count = count + 1print("size is %d" % count)file.close()# output = open("out.pcd","w+")f_prefix = filename.split('.')[0]output_filename = '{prefix}.pcd'.format(prefix=f_prefix)output = open(output_filename, "w+")list = ['# .PCD v0.7 - Point Cloud Data file format\n', 'VERSION 0.7\n', 'FIELDS x y z rgb\n', 'SIZE 4 4 4 4\n','TYPE F F F U\n', 'COUNT 1 1 1 1\n']output.writelines(list)output.write('WIDTH ')  # 注意后边有空格output.write(str(count))output.write('\nHEIGHT ')output.write(str(1))  # 强制类型转换,文件的输入只能是str格式output.write('\nPOINTS ')output.write(str(count))output.write('\nDATA ascii\n')file1 = open(filename, "r")all = file1.read()output.write(all)output.close()file1.close()end = time.time()print("run time is: ", end - start)import pcl.pcl_visualizationcloud = pcl.load_XYZRGB('cloud_rgb.pcd')visual = pcl.pcl_visualization.CloudViewing()visual.ShowColorCloud(cloud, 'cloud')flag = Truewhile flag:flag != visual.WasStopped()
# TypeError: expected bytes, str found

原图,深度图,云图(云图一片黑,鼠标滚轮翻一下) 如下:
opencv保存的颜色图:

scipy保存的颜色图:

深度图:

深度图可视化:

云图(似乎深度和色彩没有对齐):

Intel Realsense D435 python 从深度相机realsense生成pcl点云相关推荐

  1. python 从深度相机realsense生成pcl点云

    简单说下步骤: 一.通过realsense取得深度信息和彩色信息 二.获取坐标和色彩信息 三.通过pcl可视化点云 一.通过realsense取得深度信息和彩色信息 ubuntu下intel real ...

  2. realsense D435 D435i D415深度相机在ros下获得RGB图、左右红外摄像图、深度图、IMU数据

    首先你要你确保你的相机驱动已经安装好,环境配置可以看我的另一篇文章:https://blog.csdn.net/weixin_46195203/article/details/119205851 ** ...

  3. Intel Realsense D435 python (Python Wrapper)example00: NumPy Integration 将深度帧数据转换为 Numpy 数组进行处理

    NumPy Integration: Librealsense frames support the buffer protocol. A numpy array can be constructed ...

  4. Intel Realsense D435 python (Python Wrapper)examples 官方案例汇总

    From pypi/pyrealsense 1.Intel Realsense D435 python (Python Wrapper)example -1: quick start (快速开始) F ...

  5. ros订阅相机深度信息_基于深度相机 RealSense D435i 的 ORB SLAM 2

    相比于上一篇文章,这里我们将官方给的 rosbag 数据包替换为来自深度相机的实时数据.之所以选择 Intel RealSense 这款深度相机,仅仅是因为它是最容易买到的...在京东上搜" ...

  6. 深度相机Kinect2.0三维点云拼接实验(三)

    文章目录 概要 刚体空间旋转的描述 旋转矩阵 欧拉角 四元数 刚体空间平移的描述 总结 概要   Kinect2.0是微软推出的一款RGB-D相机,它即支持普通相机的拍摄,也支持脉冲测量深度信息.本系 ...

  7. 哪些深度相机有python接口_python 从深度相机realsense生成pcl点云

    简单说下步骤: 一.通过realsense取得深度信息和彩色信息 二.获取坐标和色彩信息 三.通过pcl可视化点云 一.通过realsense取得深度信息和彩色信息 ubuntu下intel real ...

  8. Intel Realsense D435 python 实战(一)

    20200519 D435包含一个有源红外摄像头,有源红外摄像头旁边的两个能够输出黑白图像,应属于双目测距, 1战 2019年8月22日 公司的Intel Realsense D435摄像头的识别系统 ...

  9. Intel Realsense D435 python multiprocessing 摄像头多进程流传输

    参考文章1:python 测试multiprocessing多进程 参考文章2:Intel Realsense D435 多摄像头目标识别架构

最新文章

  1. C语言数组学习 - 使用窗口版程序演示
  2. CentOS 7/8 安装 oniguruma 和 oniguruma-devel
  3. SAP WebIDE 里开发 SAP UI5 应用时,使用 Ctrl + Space 实现代码自动完成功能
  4. 善用工具_如何善用色彩心理学
  5. 牛客小白月赛12:月月给华华出题(欧拉函数)
  6. 阿里Java架构师精通资料:性能优化+亿级并发架构汇总+架构选型
  7. 院士建议:多关注千千万万没有任何“帽子”的青年科技工作者
  8. Python天天美味(4) - isinstance判断对象类型
  9. 火了!评分9.7,这本Python书终于玩大了!
  10. java斗地主怎么出牌_斗地主滑动选牌出牌(Cocos Creator)
  11. Unity3D基础21:UI简介
  12. 1972年发射失败的苏联金星探测器可能今年坠落地球
  13. zk-03-Zookeeper部署和运行
  14. html基础-style样式
  15. lsof的安装及使用
  16. 10.1 单片机数字秒表程序
  17. 雷锋网专访陌陌产品总监雷小亮
  18. OBS使用WebRTC进行腾讯云推流播流
  19. C++常用的延时方法
  20. mysql必知必会的数据_MySQL必知必会--汇 总 数 据

热门文章

  1. 频谱仪使用方法图解_钳形电流表使用方法图解
  2. Swift vs. Objective-C:未来看好Swift的十个理由
  3. 解除ABAP程序编辑锁的方法
  4. SAP的会计凭证类别
  5. cfa mock exam 2020下载_2019年6月CFA考试为什么一定要做MOCK?
  6. java中map可以为空吗_Java: Map里面的键和值可以为空吗?
  7. 河北省电子工程高级职称公示_2019年河北省电子工程职称评审,中级职称已经出结果了!...
  8. python读取表格数据_Python读取Excel数据并根据列名取值
  9. 中单引号和双引号的区别与联系_VB中Sub与function的联系与区别
  10. 计算机网络拓扑结构的选择规则,赖工为您讲解计算机网络的拓扑结构