TXT格式点云数据转换成pcd格式用pcl

  1. #include "stdafx.h"  
  2. #include <pcl/io/pcd_io.h>  
  3. #include<iostream>  
  4. using namespace std;  
  5. int numofPoints(char* fname){  
  6.     int n=0;  
  7.     int c=0;  
  8.     FILE *fp;  
  9.     fp = fopen(fname,"r");  
  10.     do{  
  11.         c = fgetc(fp);  
  12.         if(c == '\n'){  
  13.             ++n;  
  14.         }  
  15.     }  
  16.     while(c != EOF);  
  17.     fclose(fp);  
  18.     return n;  
  19. }  
  20. int main()  
  21. {  
  22.    int n = 0; //n用来计文件中点个数      
  23.     FILE *fp_1;  
  24.     fp_1 = fopen("cat.txt","r");  
  25.     n = numofPoints("cat.txt");//使用numofPoints函数计算文件中点个数  
  26.     std::cout << "there are "<<n<<" points in the file..." <<std::endl;  
  27.     //新建一个点云文件,然后将结构中获取的xyz值传递到点云指针cloud中。  
  28.     pcl::PointCloud<pcl::PointXYZ> cloud;  
  29.     cloud.width    = n;  
  30.     cloud.height   = 1;  
  31.     cloud.is_dense = false;  
  32.     cloud.points.resize (cloud.width * cloud.height);  
  33.     //将点云读入并赋给新建点云指针的xyz      
  34.     double x,y,z;  
  35.     int i = 0;  
  36.     while(3 ==fscanf(fp_1,"%lf,%lf,%lf\n",&x,&y,&z)){  
  37.         cout<<x<<" "<<y<<" "<<z<<endl;  
  38.         cloud.points[i].x = x;  
  39.         cloud.points[i].y = y;  
  40.         cloud.points[i].z = z;  
  41.         ++i;  
  42.     }  
  43.     fclose(fp_1);  
  44.     //将点云指针指向的内容传给pcd文件  
  45.   
  46.     pcl::io::savePCDFileASCII ("yulan_tree_01.pcd", cloud);  
  47.   
  48.     std::cerr <<"Saved " << cloud.points.size () <<" data points to test_pcd.pcd." << std::endl;  
  49.     system("pause");  
  50.   return 0;  

  1. }  

  • 3
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 21
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值