前提
plyファイルをmatplotlibで表示させたときにmeshlbとは異なるように表示される.
meshlabで点群の表示(plyファイルの中に色データがあるので着色されています)
matplotlibでplyファイルの3次元座標をもとに出力させた時
実現したいこと
以下のようにmatplotlibでの表示とmeshlabとの表示が同じになるようにしたい
別のplyファイルをmeshlabで表示させたとき
別のplyファイルを3次元座標をもとに出力させた時
該当のソースコード
python
1import struct 2import numpy as np 3import matplotlib.pyplot as plt 4file_path = '**.ply' 5output_path = '**.txt' 6with ( 7 open(file_path, 'rb') as f_in, 8 open(output_path, 'w') as f_out 9 ): 10 11 while True: 12 line = f_in.readline() 13 if b'end_header' in line: 14 break 15 if b'vertex ' in line: 16 vnum = int(line.split(b' ')[-1]) # num of vertices 17 coordinate = np.empty((0,3)) 18 for i in range(vnum): 19 temp = [] 20 for j in range(3): 21 xs = struct.unpack('f', f_in.read(4))[0] 22 print (xs, end=' ', file=f_out) 23 # print('xs = {}\n'.format(xs)) 24 temp.append(xs) 25 print ("", file=f_out) 26 temp_array = np.array(temp,dtype="float").reshape((1,3)) 27 # print('temp = {}'.format(temp_array.shape)) 28 coordinate = np.concatenate([coordinate, temp_array]) 29 30 print('Done') 31#coordinateが(点の数, 3)となっていることを確認 32 print(coordinate.shape) 33 34#3次元座標の配列からx,y,zの座標のリストに切り分ける 35x = coordinate[...,0] 36y = coordinate[...,1] 37z = coordinate[...,2] 38 39#結果を点のみ出力 40fig = plt.figure(figsize = (16, 16)) 41 42# 3DAxesを追加 43ax = fig.add_subplot(111, projection='3d') 44 45# Axesのタイトルを設定 46ax.set_title("Helix", size = 20) 47 48# 軸ラベルを設定 49ax.set_xlabel("x", size = 14) 50ax.set_ylabel("y", size = 14) 51ax.set_zlabel("z", size = 14) 52ax.plot(x, y, z, linestyle='None', marker='o') 53 54plt.show()
plyファイルの中身
plyファイルの中身を出力するコード
python
1import struct 2file_path = 'input.ply' 3 4with open(file_path, 'rb') as f: 5 # read header 6 while True: 7 line = f.readline() 8 print (line) 9 if b'end_header' in line: 10 break 11 if b'vertex ' in line: 12 vnum = int(line.split(b' ')[-1]) # num of vertices 13 if b'face ' in line: 14 fnum = int(line.split(b' ')[-1]) # num of faces 15 16 # read vertices 17 for i in range(vnum): 18 for j in range(3): 19 print (struct.unpack('f', f.read(4))[0], end=' ') 20 print ("")
出力されたplyファイルの中身
output.txt
1b'ply\r\n' 2b'format binary_little_endian 1.0\r\n' 3b'comment pointcloud saved from Realsense Viewer\r\n' 4b'element vertex 173845\r\n' 5b'property float32 x\r\n' 6b'property float32 y\r\n' 7b'property float32 z\r\n' 8b'property uchar red\r\n' 9b'property uchar green\r\n' 10b'property uchar blue\r\n' 11b'element face 335218\r\n' 12b'property list uchar int vertex_indices\r\n' 13b'end_header\r\n' 14-0.999194860458374 0.7624208927154541 -1.4152500629425049 15-43201033404416.0 -8.505155401575953e-14 -2.472513521814055e-24 161.9825851062962584e-23 2.3415821739039566e+27 4.552246991806896e-06 17...
最後に
うまくいく時といかない時の違いがわかりません.
うまく行ったファイル: vertex(ファイル内の点の数)1494点
表示がおかしいファイル: vertex(ファイル内の点の数)173845点
何がお気づきのことがあれば教えていただけると幸いです.