请选择 进入手机版 | 继续访问电脑版

点云技术相关产学研社区

 找回密码
 立即注册加入PCL中国点云技术相关产学研社区

扫一扫,访问微社区

查看: 3805|回复: 2

如何解析cvs的格式的点云到pcl

[复制链接]
发表于 2017-11-19 12:17:22 | 显示全部楼层 |阅读模式
  1. QString filename = QFileDialog::getOpenFileName (this, tr ("Open point cloud"), "/home/", tr ("Point cloud data (*.pcd *.ply *.csv)"));

  2.   PCL_INFO("File chosen: %s\n", filename.toStdString ().c_str ());
  3.   PointCloudT::Ptr cloud_tmp (new PointCloudT);

  4.   if (filename.isEmpty ())
  5.     return;

  6.   int return_status;
  7.   if (filename.endsWith (".pcd", Qt::CaseInsensitive))
  8.     return_status = pcl::io::loadPCDFile (filename.toStdString (), *cloud_tmp);
  9. // else
  10. //    return_status = pcl::io::loadPLYFile (filename.toStdString (), *cloud_tmp);
  11.   ifstream fin;
  12.   if (filename.endsWith (".csv", Qt::CaseInsensitive))
  13.   {
  14.           fin.open(filename.toStdString());
  15.           return_status=0;
  16.   }
  17.   string line;
  18.   vector<vector<float>> data;//存储
  19.   int num = 0;//行数

  20.   while (getline(fin, line))   //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取  
  21.   {  
  22.           num = num + 1;
  23.           istringstream sin(line); //将整行字符串line读入到字符串流istringstream中  
  24.           vector<float> fields; //存储一行的数据
  25.           string field;
  26.           while (getline(sin, field, ',')) //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符  
  27.           {
  28.                   if (isdigit(field[0]))//用第一个字符是否为数字来判断是数字或者nan
  29.                   {
  30.                           fields.push_back(atof(field.c_str()));
  31.                   }//将string转换为float类型添加到向量fields中
  32.                   else
  33.                   {
  34.                           fields.push_back(nan);
  35.                   }
  36.           }
  37.           data.push_back(fields);
  38.   }//
  39.   cout << "共有" << num << "行数据。" << endl;
  40.   cout << "end..." << endl;
  41.   if (return_status != 0)
  42.   {
  43.     PCL_ERROR("Error reading point cloud %s\n", filename.toStdString ().c_str ());
  44.     return;
  45.   }
  46.    PCL_WARN("file has loaded\n");

  47.    struct point
  48.    {
  49.            float x;
  50.            float y;
  51.            float z;
  52.    };

  53.    vector<point> pdata;
  54.    float xx = 0.0;
  55.    float yy = 0.0;
  56.    float step = 0.2;
  57.    for (int i = 0; i < data.size(); i++)
  58.    {
  59.            for (int j = 0; j < data[i].size(); j++)
  60.            {
  61.                    point p;
  62.                    p.x = xx;
  63.                    p.y = yy;
  64.                    p.z = data[i][j];
  65.                    pdata.push_back(p);

  66.                    xx = xx + step;
  67.            }
  68.            xx = 0.0;
  69.            yy = yy + step;
  70.    }
  71.    for(int i=0;i<pdata.size();i++)
  72.    {
  73.            pcl::PointXYZRGBA tmp;
  74.            tmp.x=pdata[i].x;
  75.            tmp.y=pdata[i].y;
  76.            tmp.z=pdata[i].z;
  77.            cloud_tmp->push_back(tmp);
  78.    }
  79.    cloud_tmp->is_dense=true;
  80.   // If point cloud contains NaN values, remove them before updating the visualizer point cloud
  81.   if (cloud_tmp->is_dense)
  82.     pcl::copyPointCloud (*cloud_tmp, *cloud_);
  83.   else
  84.   {
  85.     PCL_WARN("Cloud is not dense! Non finite points will be removed\n");
  86.     std::vector<int> vec;
  87.     pcl::removeNaNFromPointCloud (*cloud_tmp, *cloud_, vec);
  88.   }
复制代码
回复

使用道具 举报

发表于 2018-4-12 21:27:03 | 显示全部楼层
你好,我想问一下,你的.csv点云文件格式是什么样子的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-24 12:25:37 | 显示全部楼层
本帖最后由 mypcl 于 2018-4-24 16:33 编辑
lzy6041123 发表于 2018-4-12 21:27
你好,我想问一下,你的.csv点云文件格式是什么样子的

文本,每个数值之间用逗号隔开的,一行一组数据。
我的格式本来存的是深度图,比如600行600列,每个格子存的是深度数据。
回复 支持 反对

使用道具 举报

本版积分规则

QQ|小黑屋|点云技术相关产学研社区 ( 陕ICP备13001629号 )

GMT+8, 2024-4-16 19:04 , Processed in 2.055005 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表