ubuntu18.04+python3+pycharm+pcl1.8实现点云的保存和读取

ubuntu18.04+python3+pycharm+pcl1.8实现点云的保存和读取
从开始学习opencv时,就一直用python,现在在学习3D视觉,pointcloud是个难啃的骨头,pcl库的一些函数用法大部分都是C++,深度转点云。点云图的保存和读取,大部分教程都是C,python很少,这里我来分享python-pcl的一些函数的参数使用,后续会更新python版本的pcl库的一些使用,点云滤波器、过滤器、3D测量、3D的物体分割与识别等

1.点云图的获取
首先要使用realsense D415获取点云图,需要安装PCL,realsense等工具库,安装说明见我上一个博客,这里直接给代码了,大概流程是:

  1. 启动相机获取彩色图,深度图,着色深度图,
  2. 深度图转为点云图,这里把深度的一些信息写入到txt文件中
  3. 然后读取txt文件,使用pcl.save()保存为PCD点云文件
import pyrealsense2 as rs
import numpy as np
import cv2
import pcl
from pcl import pcl_visualization


def get_image():
    # Configure depth and color streams
    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)
    # Start streaming
    pipeline.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)


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')
    with open('could.txt','r') as f:
        lines = f.readlines()
        num = 0
        data = []
        for 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+1
    with 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')


def visual_demo():
    cloud = pcl.PointCloud_PointXYZRGB()
    points = np.zeros((307200, 4), dtype=np.float32)
    n = 0
    with 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])/1000
            points[n][1] = np.float(l[1])/1000
            points[n][2] = np.float(l[2])/1000
            points[n][3] = np.int(l[3])
            n=n+1
    # print(points[0:100]) #看看数据是怎么样的
    cloud.from_array(points)# 从array构建点云的方式
    #保存点云文件
    pcl.save(cloud, './1.pcd')
    visual = pcl.pcl_visualization.CloudViewing()
    visual.ShowColorCloud(cloud)
    v = True
    while v:
        v = not (visual.WasStopped())


if __name__ == "__main__":
    get_image()
    my_depth_to_cloud()
    visual_demo()

2.点云图的保存:
这里主要讲PLY和PCD两种点云的文件类型,具体就不再解释了,目前,点云文件保存为*.pcd是最为广泛使用的,可存储的类型很多。可以直接使用PCL库的save函数进行保存(之前看过一篇点云保存的文章,主要操作就是把深度图的参数写入color_rgb.txt文件,保存的时候先读入这个文件,然后转换类型、统计源文件的点数等一些操作,看起来比较麻烦),废话不多说,直接上代码:

def visual_demo():
    cloud = pcl.PointCloud_PointXYZRGB()#创建pointcloud的类型,着色点云
    points = np.zeros((307200, 4), dtype=np.float32)
    pcl.save(cloud, './1.pcd')
    #函数参数说明:save(cloud: Any,path: {endswith},format: Any = None,binary: bool = False) -> Any
    # cloud:创建的点云对象
    # path:点云文件的保存路径,其余参数不用管

ok,点云图的保存就一个save函数搞定,点云文件保存如下
在这里插入图是片描述
3.点云图的读取
直接上代码:

def readpcdfile_demo():
	# 加载刚刚保存的点云文件:1.pcd
    cloud = pcl.load_XYZRGB('./1.pcd')
    visual = pcl.pcl_visualization.CloudViewing()# 创建visual类,创建显示窗口,名字为:PCD viewer
    visual.ShowColorCloud(cloud, b'cloud')#cloud前面一定不要忘了'b',至于为啥这里也不是太懂
    flag = True
    while flag:
        flag != visual.WasStopped()

读取结果如下:
在这里插入图片描述
欧克,点云的保存和读取更新到这,后面会更新python版本的点云滤波器、过滤器的代码和解释,谢谢,有错误的地方欢迎评论!

  • 6
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值