Writing 3D range grid data to PLY file

I have been working on fixing 3D scanner capable of scanning entire human body. I needed to export the scanner data to Scanalyze to figure out what was wrong with the scanner. I could not find something easy and quick which I could use to export the range grid data to the PLY file format that Scanalyze understands. Final result of few hours of web search, reverse engineering and trial and error follows:

function cloud2ply(X,Y,Z,outfile)
% Writes range grid data to a PLY file
% X,Y,Z: 2D matrix describing x,y and z depths
%outfile: path/name of the output ply flie

%Ignores all zeros
M=(X(:)==0)&(Y(:)==0)&(Z(:)==0);

fid=fopen(outfile,'w');
fprintf(fid,'plyn');
fprintf(fid,'format ascii 1.0n');
fprintf(fid,'obj_info num_cols %dn',size(X,1));
fprintf(fid,'obj_info num_rows %dn',size(X,2));
fprintf(fid,'element vertex %dn',sum(~M));
fprintf(fid,'property float xn');
fprintf(fid,'property float yn');
fprintf(fid,'property float zn');
fprintf(fid,'element range_grid %dn',numel(X));
fprintf(fid,'property list uchar int vertex_indicesn');
fprintf(fid,'end_headern');

fprintf(fid,'%f %f %fn',[X(~M) Y(~M) Z(~M)]');
fprintf(fid,'%d %dn',[~M (~M).*cumsum(~M)]');

fclose(fid);

One Comment:

  1. There was a bug. PLY rangegrid starts indexing at 0. Matlab starts at 1. However thanks for the sample code, much appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *