把 .xyz 檔案轉換成 .ply 檔案

一杯明月發表於2024-06-15

把 .xyz 檔案轉換成 .ply 檔案 (加標頭檔案)_python將.ply轉為.xyz-CSDN部落格

from plyfile import PlyData,PlyElement
import re
import numpy as np
from os.path import join

def write_ply(save_path,points,text=True):
    
    points = [(points[i,0], points[i,1], points[i,2]) for i in range(points.shape[0])]
    vertex = np.array(points, dtype=[('x', 'f4'), ('y', 'f4'),('z', 'f4')])
    el = PlyElement.describe(vertex, 'vertex', comments=['vertices'])
    PlyData([el], text=text).write(save_path)


A = []
timestamps = []

with open('/Users/manmi/Documents/data/square_data/mm_data/timestamp.txt') as f:
    content = f.readlines()
    for line in content:
        timestamp = int(line)
        timestamps.append(timestamp)
timestamps.sort()      


for k in range(0, len(timestamps)):
    src_tamestamp = timestamps[k]
    path = join('/Users/manmi/Documents/data/square_data/mm_data/mm_xyz', str(src_tamestamp)+'.xyz')              
    with open(path) as f:
        content = f.readlines()
        for line in content:
            x = float(re.split('\s+', line)[0])
            y = float(re.split('\s+', line)[1])
            z = float(re.split('\s+', line)[2])
            A.append((x,y,z))
    points = np.array(A)

    savepath = join('/Users/manmi/Documents/data/square_data/mm_data/mm_ply',str(src_tamestamp)+'.ply')
    write_ply(savepath, points, text=True)

相關文章