编程语言
首页 > 编程语言> > .off转.ply(python)

.off转.ply(python)

作者:互联网

1. 读取.off为N3点云和Mc网格面

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. N*3 点云写入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. N3点云和Mc网格面(第1个参数是点的数量,后面c-1个参数是点的坐标)

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][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. 效果





标签:off,python,join,ply,cat,path,os,dir
来源: https://www.cnblogs.com/simingfan/p/11782677.html