matlab将txt数据转为PLY数据
matlab将txt数据转为PLY数据
- 自带函数存在问题
- PLY_Write函数说明
- ply_write函数代码
自带函数存在问题
博主使用matlab2019a,其自带点云处理函数并不好使,代码如下:
aa=pointcloud('a.txt')
pcwrite(aa,'aa.ply','ascii')
pcshow('aa.ply')
matlab可以正常显示,但是其他软件不接受,打开生成的ply文件与正常的ply文件对比,发现表头里数据类型有问题。
后来采用一位大佬写的函数,发现生成的ply文件可以被其他软件正常打开,调用如下:
aa=load('a.txt');
cc.vertex.x=aa(:,1);
cc.vertex.y=aa(:,2);
cc.vertex.z=aa(:,3);
ply_write(cc,'bb.ply','ascii')
PLY_Write函数说明
这是一个很老的matlab函数了,具体见文未。作者:Pascal Getreuer 时间:2004
%% PLY_WRITE writes 3D data as a PLY file.
%
% PLY_WRITE(DATA,FILENAME) writes the structure DATA as a binary
% PLY file. Every field of DATA is interpreted as an element
% and every subfield as an element property. Each subfield of
% property data must either be an array or a cell array of
% arrays. All property data in an element must have the same
% length.
%
% A common PLY data structure has the following fields:
% DATA.vertex.x = x coordinates, [Nx1] real array
% DATA.vertex.y = y coordinates, [Nx1] real array
% DATA.vertex.z = z coordinates, [Nx1] real array
%
% DATA.face.vertex_indices = vertex index lists,
% an {Mx1} cell array where each cell holds a one-
% dimesional array (of any length) of vertex indices.
% Some other common data fields:
% DATA.vertex.nx = x coordinate of normal, [Nx1] real array
% DATA.vertex.ny = y coordinate of normal, [Nx1] real array
% DATA.vertex.nz = z coordinate of normal, [Nx1] real array
%
% DATA.edge.vertex1 = index to a vertex, [Px1] integer array
% DATA.edge.vertex2 = second vertex index, [Px1] integer array
% Many other fields and properties can be added. The PLY format
% is not limited to the naming in the examples above -- they are
% only the conventional naming.
%
% PLY_WRITE(DATA,FILENAME,FORMAT) write the PLY with a specified
% data format, where FORMAT is
% 'ascii' ASCII text data
% 'binary_little_endian' binary data, little endian
% 'binary_big_endian' binary data, big endian (default)
%
% PLY_WRITE(DATA,FILENAME,FORMAT,'double') or
%
% PLY_WRITE(DATA,FILENAME,'double') write floating-point data as
% double precision rather than in the default single precision.
%
% Example:
% % make a cube
% clear Data;
% Data.vertex.x = [0;0;0;0;1;1;1;1];
% Data.vertex.y = [0;0;1;1;0;0;1;1];
% Data.vertex.z = [0;1;1;0;0;1;1;0];
% Data.face.vertex_indices = {[0,1,2,3],[7,6,5,4], ...
% [0,4,5,1],[1,5,6,2],[2,6,7,3],[3,7,4,0]};
% plywrite(Data,'cube.ply','ascii');
%
% Licensing:
% This code is distributed under the GNU LGPL license.
% Modified:
% 01 March 2007
ply_write函数代码
不多说了,下载文件是要积分,为了方便大家自取,直接粘贴这里。
好用了,不妨点个赞呀。
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';endend[ fid, Msg ] = fopen ( Path, 'wt' );if ( fid == -1 )error(Msg);endPlyTypeNames = {'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 : NumElementseval(['tmp=isa(Elements.',ElementNames{i},',''struct'');']);if ( tmp )eval(['PropertyNames{i}=fieldnames(Elements.',ElementNames{i},');']);elsePropertyNames{i} = [];endif ( ~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);elseElementCount(i) = 0;endfprintf(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.');endif ( iscell(Data{i}{j}) )Type{i}(j) = 9;Data{i}{j} = Data{i}{j}{1};endfor k = 1 : length(MatlabTypeNames)if ( isa(Data{i}{j},MatlabTypeNames{k}) )Type{i}(j) = Type{i}(j) + k;break;endendif ( ~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 integerMinValue = min(min(Data{i}{j}));MaxValue = max(max(Data{i}{j}));% choose smallest possible integer data formattmp = max(min(find(MinValue >= IntegerDataMin)),min(find(MaxValue <= IntegerDataMax)));if ~isempty(tmp)Type{i}(j) = tmp;endendendelse % cell array dataeval(['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});endif tmp % data is integerMinValue = 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 formattmp = max(min(find(MinValue >= IntegerDataMin)),min(find(MaxValue <= IntegerDataMax)));if ~isempty(tmp)Type{i}(j) = tmp + 9;endendend% convert double to single if specifiedif rem(Type{i}(j),9) == 8 & ~strcmpi(Str,'double')Type{i}(j) = Type{i}(j) - 1;endif Type{i}(j) <= 8fprintf(fid,'property %s %s\n',PlyTypeNames{Type{i}(j)},PropertyNames{i}{j});elsefprintf(fid,'property list uchar %s %s\n',PlyTypeNames{Type{i}(j)-9},PropertyNames{i}{j});endendendfprintf(fid,'end_header\n');switch Formatcase '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;endfor i = 1 : NumElementsif ~isempty(PropertyNames{i})if ~Format % write ASCII datafor k = 1:ElementCount(i)for j = 1:length(PropertyNames{i})if Type{i}(j) <= 8fprintf(fid,[PrintfTypeChar{Type{i}(j)},' '],Data{i}{j}(k));elsefprintf(fid,'%u%s ',length(Data{i}{j}{k}),sprintf([' ',PrintfTypeChar{Type{i}(j)-9}],Data{i}{j}{k}));endendfprintf(fid,'\n');endelse % write binary dataif 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}(:)';endfwrite(fid,tmp,FWriteTypeNames{Type{i}(j)});elseif all(Type{i} > 8)% only list typesType{i} = Type{i} - 9;if length(PropertyNames{i}) == 1% only one list propertytmp = FWriteTypeNames{Type{i}(1)};for k = 1:ElementCount(i)fwrite(fid,length(Data{i}{1}{k}),'uchar');fwrite(fid,Data{i}{1}{k},tmp);endelse% multiple list propertiesfor 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)});endendendelse% mixed typefor k = 1:ElementCount(i)for j = 1:length(PropertyNames{i})if Type{i}(j) <= 8fwrite(fid,Data{i}{j}(k),FWriteTypeNames{Type{i}(j)});elsefwrite(fid,length(Data{i}{j}{k}),'uchar');fwrite(fid,Data{i}{j}{k},FWriteTypeNames{Type{i}(j)-9});endendendendendendendfclose(fid);return
end
matlab将txt数据转为PLY数据相关推荐
- Eviews:季度数据转为月度数据(频率转换)
Eviews10 如何将季度数据转为月度数据(低频转高频) (40条消息) Eviews10 如何将季度数据转为月度数据(低频转高频)_什么都不懂的小白.的博客-CSDN博客_季度数据如何转为月度数据 ...
- VBA运行将多个excel的矩阵类型数据转为向量数据
因为从气象站获取的气象数据大都是矩阵形式的,列为站点,行为日期,但是在跑模型的时候需要把这种矩阵类型的数据转为一条一条的向量数据,可以在excel中利用一下VBA代码批量处理多个excel/csv文件 ...
- arcgis操作:dwg数据转为shp数据
基本知识: dwg数据是一种CAD类型的数据,shp数据是矢量数据 加载dwg数据 数据转换: dwg数据转成shp数据 转换方法: 直接导出 选择保存方式为shapefile 结果如下 如果是注记的 ...
- Matlab读取txt文件中的数据(使用textread函数)
在使用Matlab处理数据时,我们经常需要读取txt文档,可以使用Matlab中强大的textread函数. 它的基本语法是: [A,B,C,...] = textread(filename,form ...
- Matlab画图——txt文件数据处理(数据对比)
文件数据类型 clear all; clc; data1 = importdata('C:\Users\king\Desktop\hd.txt'); data2 = importdata('C:\Us ...
- 将xls表格数据转为点数据(1)
首先我们介绍第一种转换方式. 一.在[目录]窗口中定位到待转换的Excel表格ZKSJ.xsl,双击Excel表格,展开钻孔数据的工作表. 二.右键第一个工作表"ckb13$",单 ...
- 使用OGR2OGR将S57数据转为shp格式
S57海图数据中包含了大量的图层,在目前ArcMap软件作为必备的GIS软件之一,所以还是shp格式处理起来可能比较方便(这里的方便仅仅是对于某些人觉得shp最常见,用的最广泛而言,其实我本人觉得sh ...
- c#往结构体里面读数据_C# 结构体和ListT类型数据转Json数据保存和读取
1 一.结构体转Json2 3 public structFaceLibrary4 {5 public stringface_name;6 public byte[] face_Feature;7 } ...
- matlab读取txt数据绘图(python命令行传参)
(1)命令行实现高斯分布 一:综述 Python唯一支持的参数传递方式是『共享传参』(call by sharing)多数面向对象语言都采用这一模式,包括Ruby.Smalltalk和Java(Jav ...
最新文章
- 安装Python的wx库
- (DNS被劫持所导致的)QQ音乐与视频网页打开很慢的解决方法
- linux字符设备文件的打开操作,Linux字符设备驱动模型之字符设备初始化
- jQuery之换肤与cookie插件
- MySQL里面的in关键字
- .net core 正则表达式 获取 等号后面的值_Python3 正则表达式
- Python使用类来创建对象
- 解决批处理命令执行完毕后自动关闭cmd窗口
- 【最短路径问题笔记】SPFA算法及负环的判断
- linux编译命令io,Hadoop 用命令行编译URLCat
- Pytorch之模型加载/保存
- 【hdu1556】Color the ball——树状数组
- esp8266对接天猫精灵 微信控制
- 服务器与mysql数据库服务器_数据库与服务器的关系
- 五面拿下阿里飞猪offer,java基础知识梳理
- 【历史上的今天】5 月 10 日:淘宝网上线;机器感知之父出生;英国首批计算机投入运行
- 有赞搜索系统的架构演进
- 天刀计算机中丢失,天涯明月刀手游失踪白兔奇遇任务攻略
- 几何分布的概率和期望
- 一起聆听碳氮循环研究新动态,我们在苏州期待您的精彩分享和参与!
热门文章
- 朗强:HDMI自动切换器的功能与作用
- [19保研]中南财经政法大学文澜优秀大学生夏令营“工程PLUS”分营招生简章
- CTF | CTF-Web 捉迷藏
- rk3288做云服务器性能怎么样,十个问题秒懂RK3288处理器平板(上)
- pg_enterprise_views 偶然发现的神仙PG插件!!!
- fabric+cpabe加密的医疗数据共享代码(分级加密、多授权中心)
- c语言大作业班费收支管理系统,求C++编写的程序用静态成员的方法实现对班费的管理...
- Hook微信朋友圈图片下载(标题党)
- 《Single shot laser speckle based 3D acquisition system for medical applications》
- Java进阶1-JVM虚拟机