一、問題描述
RealSense保存的ply文件,用PCL可視化時,會報錯,是因爲RealSense保存的ply文件格式有問題。
二、問題分析
RealSense保存的ply文件格式是這樣的:
ply
format ascii 1.0
comment pointcloud saved from Realsense Viewer
element vertex 87748
property float32 x
property float32 y
property float32 z
property float32 nx
property float32 ny
property float32 nz
property uchar red
property uchar green
property uchar blue
element face 173974
property list uchar int vertex_indices
end_header
-0.623535 0.499023 -0.870117
-0.177295 0.166866 0.969908
90 91 87
-0.618652 0.498535 -0.869141
-0.176508 0.172906 0.968994
90 91 82
-0.61377 0.497803 -0.868164
-0.20753 0.223017 0.952468
91 91 87
...
下面是斯坦福官方(ply格式是斯坦福編制的)的例子:
ply
format ascii 1.0
comment author: anonymous
comment object: another cube
element vertex 8
property float32 x
property float32 y
property float32 z
property red uint8 { start of vertex color }
property green uint8
property blue uint8
element face 7
property list uint8 int32 vertex_index { number of vertices for each face }
end_header
0 0 0 255 0 0 { start of vertex list }
0 0 1 255 0 0
0 1 1 255 0 0
0 1 0 255 0 0
1 0 0 0 0 255
1 0 1 0 0 255
1 1 1 0 0 255
1 1 0 0 0 255
...
對比一下,
先看header部分,Stanford的點雲每個點有6個屬性(x,y,z,red,green,blue),RealSense的點雲每個點有9個屬性(x,y,z,nx,ny,nz,red,green,blue);
然後看數據部分,Stanford的每行有6個數字,很明顯,分別代表 x,y,z,red,green,blue。RealSense的從數據格式可以看出,每三行爲一組,看出每三行一組然後就很明瞭了,三行一共9個數,分別代表x,y,z,nx,ny,nz,red,green,blue。
RealSense保存的ply文件問題就在這裏,每個點的數據應該在同一行(即是以換行符區分點的),所以將RealSense的ply文件每一組放到一行就可以了。
像這樣:
-0.623535 0.499023 -0.870117 -0.177295 0.166866 0.969908 90 91 87
-0.618652 0.498535 -0.869141 -0.176508 0.172906 0.968994 90 91 82
-0.61377 0.497803 -0.868164 -0.20753 0.223017 0.952468 91 91 87
...
從header中的 element vertex 87748
可以看到,一共有87748個點,手動改簡直就是災難,所以寫了腳本,大家可以參考。
腳本鏈接
三、使用方法
1.建立如下的文件結構:
.
├── fix_realsense_ply.py
├── fixed
└── src
2.將需要修改的RealSense文件放到 src 文件夾中,然後執行命令:
python fix_realsense_ply.py
3.最後修改好的文件在 fixed 文件夾中
四、注意
如果你的ply和上面例子的一樣(3行一組),則直接執行,如果是2行一組,需要將源碼中vertex_size = vertex_size*3
改爲vertex_size = vertex_size*2
,並且將fix_ply()函數裏的get_fixed_vertex3()函數改爲get_fixed_vertex2()。