行走的蓑衣客

导航

 
#open3d安装:pip3 install open3d-python
import open3d as o3d
import numpy as np

def makePlyFile(xyzs, labels, fileName='makeply.ply'):
    '''Make a ply file for open3d.visualization.draw_geometries
    :param xyzs:    numpy array of point clouds 3D coordinate, shape (numpoints, 3).
    :param labels:  numpy array of point label, shape (numpoints, ).
    '''
    RGBS = [
        (0, 255, 255),
        (255, 0, 0),
        (0, 255, 0),
        (0, 0, 255),
        (0, 0, 0),
        (255, 0, 255)
    ]

    with open(fileName, 'w') as f:
        f.write('ply\n')
        f.write('format ascii 1.0\n')
        f.write('comment PCL generated\n')
        f.write('element vertex {}\n'.format(len(xyzs)))
        f.write('property float x\n')
        f.write('property float y\n')
        f.write('property float z\n')
        f.write('property uchar red\n')
        f.write('property uchar green\n')
        f.write('property uchar blue\n')
        f.write('end_header\n')
        for i in range(len(xyzs)):
            r, g, b = (255, 0, 0)

            x, y, z = xyzs[i]
            f.write('{} {} {} {} {} {}\n'.format(x, y, z, r, g, b))

if __name__ == "__main__":
    xyzs=[]
    with open("point.txt", "r") as f:
        for line in f.readlines():
            p=[]
            line = line.strip('\n')  # 去掉列表中每一个元素的换行符
            a=line.split(",")
            p.append(float(a[0]))
            p.append(float(a[1]))
            p.append(float(a[2]))
            xyzs.append(p)

    numpoints=len(xyzs)
    labels = np.random.randint(0, 4, numpoints)  # 4种label
    makePlyFile(xyzs, labels, 'demo.ply')
    pcd = o3d.io.read_point_cloud('demo.ply')
    o3d.visualization.draw_geometries([pcd])

 


 

posted on 2022-03-17 22:15  行走的蓑衣客  阅读(722)  评论(0)    收藏  举报