matlab ply格式点云读入、显示、三角化

%2019.10.21 suyunzzz

%将ply格式的点云数据,先转换为txt文件,
% 然后以mat格式读入并显示,然后将点云网格化。
%Q:为什么不直接从ply格式读取呢?
% A:因为matlab自带的点云工具箱貌似没有直接对ply或者pcd文件进行重构的。
% % % 所以得需要将ply文件转换为mat格式。


filename = './bunny/bunny/reconstruction/bun_zipper_res2.ply';
ptcloud = pcread(filename);
% pcshow(ptcloud);

    Data(:,1)= double(ptcloud.Location(1:5:end,1));   %提取所有点的三维坐标
    Data(:,2)= double(ptcloud.Location(1:5:end,2));
    Data(:,3)= double(ptcloud.Location(1:5:end,3)); 
       namesplit=strsplit(filename,'.ply');           %分割ply文件的名称,分成文件名与ply后缀名
        frontname=namesplit{1};                           %提取文件名,舍弃后缀名
%  eval('fid=fopen(''1_buny.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,'%.4f\t ',Data(i,j));           %所有坐标数据保留小数点后四位
        end
        fprintf(fid,'%.4f\n',Data(i,b2));
    end
    clear Data;                 
    fclose(fid);
    
    
    filename_read =[frontname,'.txt'];%将字符串拼接起来

    fprintf('\n');
    fprintf(filename_read);
    fprintf('\n');
    load(filename_read);    %读入txt文件为mat文件

     
    %将X1_buny赋值给一个p
    
    p = bun_zipper_res2;
    
    %显示点云
    figure(1);
hold on
axis equal
title('点云','fontsize',14)
plot3(p(:,1),p(:,2),p(:,3),'r.')
view(-37.5,30)


[t]=MyCrust(p);

%% plot of the oyput triangulation
figure(2)
        hold on
        title('三角化输出','fontsize',14)
        axis equal
%         trisurf(t,p(:,1),p(:,2),p(:,3),'facecolor','g','edgecolor','r')%plot della superficie trattata
         trimesh(t,p(:,1),p(:,2),p(:,3),'edgecolor','r')%plot della superficie trattata
        view(-37.5,30)

 MyCrust.m

%% MyCrust
%
%Simple surface recostruction program based on Crust algorithm
%Given a set of 3D points returns a triangulated tight surface.
%
%The more points there are the best the surface will be fitted,
%although you will have to wait more. For very large models an 
%help memory errors may occurs.
%It is important even the point distribution, generally uniformly 
% distributed points with denser zones in high curvature features
% give the best results.
% 
%

% ͨ�����ڸ����������о��н��ܼ�����ľ��ȷֲ�����ṩ��ѽ����


% Remember  crust algorithom needs a cloud representing a volume 
% so open surface may give inaccurate results. 
%  
%  
% If any problems occurs in execution, or if you found a bug,
% have a suggestion or question just contact me at:  
%   
% giaccariluigi@msn.com
% 
%
%
%
%Here is a simple example:
%
%load Dino.mat%load input points from mat file
%
%[t]=MyCrust(p);
%
% figure(1)
%         hold on
%         title('Output Triangulation','fontsize',14)
%  
显示未定义函数或变量 'bun_zipper_res2'。%2019.10.21 苏云征 %将ply格式点云数据,先转换为txt文件, % 然后以mat格式读入显示,然后将点云网格化。 %Q:为什么不直接从ply格式读取呢? % A:因为matlab自带的点云工具箱貌似没有直接对ply或者pcd文件进行重构的。 % % % 所以得需要将ply文件转换为mat格式。 filename = './bunny/bunny/reconstruction/bun_zipper_res2.ply'; ptcloud = pcread(filename); % pcshow(ptcloud); Data(:,1)= double(ptcloud.Location(1:5:end,1)); %提取所有点的三维坐标 Data(:,2)= double(ptcloud.Location(1:5:end,2)); Data(:,3)= double(ptcloud.Location(1:5:end,3)); namesplit=strsplit(filename,'.ply'); %分割ply文件的名称,分成文件名与ply后缀名 frontname=namesplit{1}; %提取文件名,舍弃后缀名 % eval('fid=fopen(''1_buny.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,'%.4f\t ',Data(i,j)); %所有坐标数据保留小数点后四位 end fprintf(fid,'%.4f\n',Data(i,b2)); end clear Data; fclose(fid); filename_read =[frontname,'.txt'];%将字符串拼接起来 fprintf('\n'); fprintf(filename_read); fprintf('\n'); load(filename_read); %读入txt文件为mat文件 %将X1_buny赋值给一个p p = bun_zipper_res2; %显示点云 figure(1); hold on axis equal title('点云','fontsize',14) plot3(p(:,1),p(:,2),p(:,3),'r.') view(-37.5,30) [t]=MyCrust(p); %% plot of the oyput triangulation figure(2) hold on title('三角化输出','fontsize',14) axis equal % trisurf(t,p(:,1),p(:,2),p(:,3),'facecolor','g','edgecolor','r')%plot della superficie trattata trimesh(t,p(:,1),p(:,2),p(:,3),'edgecolor','r')%plot della superficie trattata view(-37.5,30)
03-20
评论 14
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tech沉思录

点赞加投币,感谢您的资瓷~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值