1. 读取.off为Nx3 点云和Mx3网格面

def read_off(filename):
    points = []
    faces = []
    with open(filename, \'r\') as f:
        first = f.readline()
        if (len(first) > 4):  # Too handle error in .off like OFF492 312 0
            n, m, c = first[3:].split(\' \')[:]
        else:
            n, m, c = f.readline().rstrip().split(\' \')[:]
        n = int(n)
        m = int(m)
        for i in range(n):
            value = f.readline().rstrip().split(\' \')
            points.append([float(x) for x in value])
        for i in range(m):
            value = f.readline().rstrip().split(\' \')
            faces.append([int(x) for x in value])
    points = np.array(points)
    faces = np.array(faces)
    return points, faces

2. Nx3 点云写入ply文件

def export_ply(pc, filename):
    vertex = np.zeros(pc.shape[0], dtype=[(\'x\', \'f4\'), (\'y\', \'f4\'), (\'z\', \'f4\')])
    for i in range(pc.shape[0]):
        vertex[i] = (pc[i][0], pc[i][1], pc[i][2])
    ply_out = PlyData([PlyElement.describe(vertex, \'vertex\', comments=[\'vertices\'])])
    ply_filename = filename[:-4] + \'.ply\'
    ply_out.write(ply_filename)

#Convert ModelNet from .off to .ply
def write_ply_points_only_from_off():
    for i in range(len(cat)):
        if not os.path.exists(os.path.join(ply_dir,cat[i],\'train\')):
            os.makedirs(os.path.join(ply_dir,cat[i],\'train\'))
        if not os.path.exists(os.path.join(ply_dir,cat[i],\'test\')):
            os.makedirs(os.path.join(ply_dir,cat[i],\'test\'))
    for i in range(len(cat)):
        print(\'writing \', cat[i])
        filenames = os.listdir(os.path.join(root_dir, cat[i],\'train\'))
        for j,x in enumerate(filenames):
            filename = os.path.join(root_dir, cat[i], \'train\', x)
            out = os.path.join(ply_dir, cat[i], \'train\', x)
            points, faces = read_off(filename)
            export_ply(points,out)
        filenames = os.listdir(os.path.join(root_dir, cat[i],\'test\'))
        for j,x in enumerate(filenames):
            filename = os.path.join(root_dir, cat[i], \'test\', x)
            out = os.path.join(ply_dir, cat[i], \'test\', x)
            points, faces = read_off(filename)
            export_ply(points,out)
if __name__ == \'__main__\':
    write_ply_from_off()

3. 将Nx3点云和Mx3网格面写入ply

def export_ply_points_faces(pc,fc,filename):
    vertex = np.zeros(pc.shape[0], dtype=[(\'x\', \'f4\'), (\'y\', \'f4\'), (\'z\', \'f4\')])
    face = np.zeros(fc.shape[0],dtype=[(\'vertex_indices\', \'i4\', (3,))])
    for i in range(pc.shape[0]):
        vertex[i] = (pc[i][0], pc[i][1], pc[i][2])
    for i in range(face.shape[0]):
         face[i] = (fc[i,1],fc[i,2],fc[i,3])# face[i][0] = fc[i,1:]
    ply_out = PlyData([PlyElement.describe(vertex, \'vertex\',comments=[\'vertices\']),
                        PlyElement.describe(face, \'face\', comments=[\'faces\'])])

    ply_filename = filename[:-4] + \'.ply\'
    ply_out.write(ply_filename)

#Convert ModelNet from .off to .ply
def write_ply_points_faces_from_off():
    for i in range(len(cat)):
        if not os.path.exists(os.path.join(ply_dir,cat[i],\'train\')):
            os.makedirs(os.path.join(ply_dir,cat[i],\'train\'))
        if not os.path.exists(os.path.join(ply_dir,cat[i],\'test\')):
            os.makedirs(os.path.join(ply_dir,cat[i],\'test\'))
    for i in range(len(cat)):
        print(\'writing \', cat[i])
        filenames = os.listdir(os.path.join(root_dir, cat[i],\'train\'))
        for j,x in enumerate(filenames):
            filename = os.path.join(root_dir, cat[i], \'train\', x)
            out = os.path.join(ply_dir, cat[i], \'train\', x)
            points, faces = read_off(filename)
            export_ply_points_faces(points,faces,out)
        filenames = os.listdir(os.path.join(root_dir, cat[i],\'test\'))
        for j,x in enumerate(filenames):
            filename = os.path.join(root_dir, cat[i], \'test\', x)
            out = os.path.join(ply_dir, cat[i], \'test\', x)
            points, faces = read_off(filename)
            export_ply_points_faces(points,faces,out)
if __name__ == \'__main__\':
    write_ply_points_faces_from_off()

4. 效果





5.读取

#读取ply
def read_ply(filename):
    with open(os.path.join(self.root_dir, cat,self.split,filename), \'rb\') as f:
        plydata = PlyData.read(f)
    points = np.vstack([plydata[\'vertex\'][\'x\'], plydata[\'vertex\'][\'y\'], plydata[\'vertex\'][\'z\']]).T#(216,3)
    tri_data = plydata[\'face\'].data[\'vertex_indices\']
    faces = np.vstack(tri_data)#(172, 3)
#读取off
def read_off(self, filename):
    points = []
    faces = []
    with open(filename, \'r\') as f:
        first = f.readline()
        if(len(first)>4):#Too handle error like OFF492 312 0
            n,m,c = first[3:].split(\' \')[:]
        else:
            n, m, c = f.readline().rstrip().split(\' \')[:]
        n = int(n)
        m = int(m)
        for i in range(n):
            value = f.readline().rstrip().split(\' \')
            points.append([float(x) for x in value])
        for i in range(m):
            value = f.readline().rstrip().split(\' \')
            faces.append([int(x) for x in value])
    points = np.array(points)
    faces = np.array(faces)
    return points, faces

版权声明:本文为simingfan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/simingfan/p/11782677.html