简单说下步骤:

一、通过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)#深度图像向彩色对齐align_to_color=rs.align(rs.stream.color)try:while True:# Wait for a coherent pair of frames: depth and colorframes = pipeline.wait_for_frames()frames = align_to_color.process(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()

2.获取内参和保存图片

分别用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')

2. 显示

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())

3.想生成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保存的颜色图

深度图

深度图可视化(这个是每有对齐的深度图align):

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

原文链接:https://blog.csdn.net/moshuilangting/article/details/89383371

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

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

    引用文章:python 从深度相机realsense生成pcl点云 从深度相机realsense生成pcl点云 一.通过realsense取得深度信息和彩色信息 二.获取坐标和色彩信息 三.通过pcl ...

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

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

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

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

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

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

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

    文章目录 摘要 Linux系统下的环境搭建 Ubuntu系统安装 ROS-Melodic安装 Kinect2.0驱动安装 PCL库安装 Open-CV库安装 注意事项 Windows10系统下的环境搭 ...

  6. Airsim雷达相机融合生成彩色点云

    代码,cmake,launch等都在网盘链接中 链接: https://pan.baidu.com/s/14hWBDnRZC41xkqAk6FirtA 提取码: 1een --来自百度网盘超级会员v4 ...

  7. Python NLPIR2016 与 wordcloud 结合生成中文词云

    前叙 该博文继承之前的文章,进一步介绍NLPIR2016的使用,三个内容(利用NLPIR 的发现新词功能自动提取文本内的新词汇,解决worldcloud中英文混合只显示中文的问题,NLPIR与worl ...

  8. Intel RealSense D435i 深度相机介绍

    参考: https://www.sohu.com/a/340984033_715754 https://www.chiphell.com/thread-1945054-1-1.html https:/ ...

  9. 用python开启相机_如何用Python打开realsenseD435相机并获取相机参数

    如何用Python打开realsenseD435相机 import pyrealsense2 as rs import numpy as np import cv2 if __name__ == &q ...

最新文章

  1. 思考:行业客户项目中的代理商(国外厂商)
  2. Android桌面组件App Widget开发三步走
  3. 继人工智能攻陷围棋,德州扑克也沦陷了
  4. 查找DetailsView1数据控件中的数据
  5. python变量如何使用,python如何使用变量
  6. 【竞赛相关】Kaggle活跃竞赛(12月汇总)
  7. windows 2008创建群集“xxx”时出错。由于超时时间已过,该操作返回
  8. 办公技巧:Word批量小技巧,大大提高工作效率
  9. java中的方法求和_在Java中模拟求和类型的巧妙解决方法
  10. 【剑指Offer】20包含min函数的栈
  11. 嵌入式学习(一)嵌入式c语言
  12. 原理图学习(点读笔调试)
  13. WIN10教育版怎么可以变更为专业版
  14. 编写一程序,从键盘输入10个实数,计算并输出算术平均数
  15. [swift] UIImage NSImage PNG透明区域填充自定义颜色实现
  16. kill word out e ef en em
  17. 技术资源分享(更新中)
  18. Vue上传多张图片到服务器,数据库存储图片路径并将图片渲染到前端
  19. php sftp上传图片,SFTP远道文件上传
  20. 谷歌浏览器Console不显示error信息

热门文章

  1. Socket编程中的强制关闭与优雅关闭及相关socket选项
  2. boot.ini文件解密
  3. 代码注入之远程线程篇
  4. Shell脚本的调试技术
  5. Android中自定义控件
  6. TCP/IP详解--学习笔记(10)-TCP连接的建立与中止
  7. Controller节点无法启动neutron-server
  8. VFS文件系统结构分析 与socket
  9. 计算机组成原理考试知识点总结,最新2018计算机组成原理期末复习考试知识点复习考点归纳总结总结...
  10. mysql 20小时内_生产环境删除数据库,如何实现在1小时内快速恢复?