ply文件批量转为txt文件
%读取当前目录下的所有ply格式点云文件,显示图像,输出为同名称的txt格式文件
path=pwd; %当前所在目录
file = dir(fullfile(path,'*.ply')); %读取所有ply格式文件
filenames = {file.name}';
filelength = size(filenames,1); %ply格式文件数
for idx = 1 : filelength %批处理
filedir = strcat(path, filenames(idx));
ptcloud=pcread(filenames{idx}); %ply格式文件用pcread读取
figure;
pcshow(ptcloud);
xlabel('X');
ylabel('Y');
zlabel('Z');
title(filenames{idx});
Data(:,1)= double(ptcloud.Location(1:end,1)); %提取所有点的三维坐标
Data(:,2)= double(ptcloud.Location(1:end,2));
Data(:,3)= double(ptcloud.Location(1:end,3));
namesplit=strsplit(filenames{idx},'.'); %分割ply文件的名称,分成文件名与ply后缀名
frontname=namesplit{1}; %提取文件名,舍弃后缀名
% fid=fopen(strcat(frontname,'.txt'),'wt');
eval(['fid=fopen(''',frontname,'.txt'',''wt'');']);
[b1,b2]=size(Data);
for i=1:b1 %将二维数组Data写入txt格式文件中
for j=1:b2-1
fprintf(fid,'%.6f\t ',Data(i,j)); %所有坐标数据保留小数点后六位
end
fprintf(fid,'%.6f\n',Data(i,b2));
end
clear Data;
fclose(fid);
end
txt文件批量转为ply文件
%读取当前目录下的所有TXT格式点云文件,循环命名输出PLY格式文件
%ply_write.m
Path = pwd;% 设置数据存放的文件夹路径
files = dir(fullfile(Path,'*.txt')); % 显示文件夹下所有符合后缀名为.txt文件的完整信息
len = size(files,1);% 获取所提取数据文件的个数
for i=1:len
fileName = strcat(Path,files(i,1).name);
name=files(i).name;
aa=load(name);
cc.vertex.x=aa(:,1);
cc.vertex.y=aa(:,2);
cc.vertex.z=aa(:,3);
newname=strcat(num2str(i),'.ply');%循环命名
ply_write(cc,newname,'ascii')
end
function ply_write ( Elements, Path, Format, Str )
if ( nargin < 4 )
Str = '';
if ( nargin < 3 )
Format = 'binary_big_endian';
elseif strcmpi(Format,'double')
Str = 'double';
Format = 'binary_big_endian';
end
end
[ fid, Msg ] = fopen ( Path, 'wt' );
if ( fid == -1 )
error(Msg);
end
PlyTypeNames = {'char','uchar','short','ushort','int','uint','float','double', ...
'char8','uchar8','short16','ushort16','int32','uint32','float32','double64'};
FWriteTypeNames = {'schar','uchar','int16','uint16','int32','uint32','single','double'};
MatlabTypeNames = {'int8','uint8','int16','uint16','int32','uint32','single','double'};
PrintfTypeChar = {'%d','%u','%d','%u','%d','%u','%-.6f','%-.14e'};
IntegerDataMin = [-128,0,-2^15,-2^31,0];
IntegerDataMax = [127,255,2^16-1,2^31-1,2^32-1];
%
% write PLY header
%
fprintf(fid,'ply\nformat %s 1.0\ncomment created by MATLAB ply_write\n',Format);
ElementNames = fieldnames(Elements);
NumElements = length(ElementNames);
Data = cell(NumElements,1);
for i = 1 : NumElements
eval(['tmp=isa(Elements.',ElementNames{i},',''struct'');']);
if ( tmp )
eval(['PropertyNames{i}=fieldnames(Elements.',ElementNames{i},');']);
else
PropertyNames{i} = [];
end
if ( ~isempty(PropertyNames{i}) )
eval(['Data{i}{1}=Elements.',ElementNames{i},'.',PropertyNames{i}{1},';']);
ElementCount(i) = prod(size(Data{i}{1}));
Type{i} = zeros(length(PropertyNames{i}),1);
else
ElementCount(i) = 0;
end
fprintf(fid,'element %s %u\n',ElementNames{i},ElementCount(i));
for j = 1 : length(PropertyNames{i})
eval(['Data{i}{j}=Elements.',ElementNames{i},'.',PropertyNames{i}{j},';']);
if ( ElementCount(i) ~= prod(size(Data{i}{j})) )
fclose(fid);
error('All property data in an element must have the same length.');
end
if ( iscell(Data{i}{j}) )
Type{i}(j) = 9;
Data{i}{j} = Data{i}{j}{1};
end
for k = 1 : length(MatlabTypeNames)
if ( isa(Data{i}{j},MatlabTypeNames{k}) )
Type{i}(j) = Type{i}(j) + k;
break;
end
end
if ( ~rem(Type{i}(j),9) )
fclose(fid);
error('Unsupported data structure.');
end
%
% Try to convert float data to integer data
%
% Array data.
%
if ( Type{i}(j) <= 8 )
if any(strcmp({'single','double'},MatlabTypeNames{Type{i}(j)}))
if ~any(floor(Data{i}{j}) ~= Data{i}{j}) % data is integer
MinValue = min(min(Data{i}{j}));
MaxValue = max(max(Data{i}{j}));
% choose smallest possible integer data format
tmp = max(min(find(MinValue >= IntegerDataMin)),min(find(MaxValue <= IntegerDataMax)));
if ~isempty(tmp)
Type{i}(j) = tmp;
end
end
end
else % cell array data
eval(['Data{i}{j}=Elements.',ElementNames{i},'.',PropertyNames{i}{j},';']);
tmp = 1;
for k = 1:prod(size(Data{i}{j}))
tmp = tmp & all(floor(Data{i}{j}{k}) == Data{i}{j}{k});
end
if tmp % data is integer
MinValue = inf;
MaxValue = -inf;
for k = 1:prod(size(Data{i}{j}))
MinValue = min(MinValue,min(Data{i}{j}{k}));
MaxValue = max(MaxValue,max(Data{i}{j}{k}));
end
% choose smallest possible integer data format
tmp = max(min(find(MinValue >= IntegerDataMin)),min(find(MaxValue <= IntegerDataMax)));
if ~isempty(tmp)
Type{i}(j) = tmp + 9;
end
end
end
% convert double to single if specified
if rem(Type{i}(j),9) == 8 & ~strcmpi(Str,'double')
Type{i}(j) = Type{i}(j) - 1;
end
if Type{i}(j) <= 8
fprintf(fid,'property %s %s\n',PlyTypeNames{Type{i}(j)},PropertyNames{i}{j});
else
fprintf(fid,'property list uchar %s %s\n',PlyTypeNames{Type{i}(j)-9},PropertyNames{i}{j});
end
end
end
fprintf(fid,'end_header\n');
switch Format
case 'ascii'
Format = 0;
case 'binary_little_endian'
fclose(fid);
fid = fopen(Path,'a','ieee-le');
Format = 1;
case 'binary_big_endian'
fclose(fid);
fid = fopen(Path,'a','ieee-be');
Format = 2;
end
for i = 1 : NumElements
if ~isempty(PropertyNames{i})
if ~Format % write ASCII data
for k = 1:ElementCount(i)
for j = 1:length(PropertyNames{i})
if Type{i}(j) <= 8
fprintf(fid,[PrintfTypeChar{Type{i}(j)},' '],Data{i}{j}(k));
else
fprintf(fid,'%u%s ',length(Data{i}{j}{k}),sprintf([' ',PrintfTypeChar{Type{i}(j)-9}],Data{i}{j}{k}));
end
end
fprintf(fid,'\n');
end
else % write binary data
if all(Type{i} <= 8) & all(Type{i} == Type{i}(1))
% property data without list types (fast)
tmp = zeros(length(PropertyNames{i}),ElementCount(i));
for j = 1:length(PropertyNames{i})
tmp(j,:) = Data{i}{j}(:)';
end
fwrite(fid,tmp,FWriteTypeNames{Type{i}(j)});
elseif all(Type{i} > 8)
% only list types
Type{i} = Type{i} - 9;
if length(PropertyNames{i}) == 1
% only one list property
tmp = FWriteTypeNames{Type{i}(1)};
for k = 1:ElementCount(i)
fwrite(fid,length(Data{i}{1}{k}),'uchar');
fwrite(fid,Data{i}{1}{k},tmp);
end
else
% multiple list properties
for k = 1:ElementCount(i)
for j = 1:length(PropertyNames{i})
fwrite(fid,length(Data{i}{j}{k}),'uchar');
fwrite(fid,Data{i}{j}{k},FWriteTypeNames{Type{i}(j)});
end
end
end
else
% mixed type
for k = 1:ElementCount(i)
for j = 1:length(PropertyNames{i})
if Type{i}(j) <= 8
fwrite(fid,Data{i}{j}(k),FWriteTypeNames{Type{i}(j)});
else
fwrite(fid,length(Data{i}{j}{k}),'uchar');
fwrite(fid,Data{i}{j}{k},FWriteTypeNames{Type{i}(j)-9});
end
end
end
end
end
end
end
fclose(fid);
return
end