点云技术相关产学研社区

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

扫一扫,访问微社区

查看: 9254|回复: 8

三维点云坐标无法写入pcd文件,求教

[复制链接]
发表于 2014-7-2 09:56:45 | 显示全部楼层 |阅读模式
本帖最后由 和你一样小馒头 于 2014-7-3 16:09 编辑

本人最近在做有关三维重建的项目,现在利用kinect生成了三维点云数据,然后想要把点云坐标写入一个pcd文件,但是在把生成点云的工程代码与pcd_write.cpp结合的时候,遇到了阻碍,编译倒是成功了,可pcd文件建立不了,数据更是无法写入。代码如下:
  1. #include <pcl/io/pcd_io.h>
  2. #include <pcl/point_types.h>
  3. #include <XnCppWrapper.h>
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <vector>

  7. using namespace xn;
  8. using namespace std;

  9. //point cloud data struct
  10. struct SColorPoint3D
  11. {
  12.     float  X;
  13.     float  Y;
  14.     float  Z;
  15.     float  R;
  16.     float  G;
  17.     float  B;

  18.     SColorPoint3D( XnPoint3D pos, XnRGB24Pixel color )
  19.     {
  20.       X = pos.X;
  21.       Y = pos.Y;
  22.       Z = pos.Z;
  23.       R = (float)color.nRed / 255;
  24.       G = (float)color.nGreen / 255;
  25.       B = (float)color.nBlue / 255;
  26.     }
  27. };

  28. void GeneratePointCloud( DepthGenerator& rDepthGen,
  29.                          const XnDepthPixel* pDepth,
  30.                          const XnRGB24Pixel* pImage,
  31.                          vector<SColorPoint3D>& vPointCloud)
  32. {
  33.     // number of point is the number of 2D image pixel
  34.     DepthMetaData mDepthMD;
  35.     rDepthGen.GetMetaData( mDepthMD );
  36.     unsigned int uPointNum = mDepthMD.FullXRes() * mDepthMD.FullYRes();

  37.     // build the data structure for convert
  38.     XnPoint3D* pDepthPointSet = new XnPoint3D[ uPointNum ];
  39.     unsigned int i, j, idxShift, idx;
  40.     for( j = 0; j < mDepthMD.FullYRes(); ++j )
  41.     {
  42.         idxShift = j * mDepthMD.FullXRes();
  43.         for( i = 0; i < mDepthMD.FullXRes(); ++i )
  44.         {
  45.             idx = idxShift + i;
  46.             pDepthPointSet[idx].X = i;
  47.             pDepthPointSet[idx].Y = j;
  48.             pDepthPointSet[idx].Z = pDepth[idx];
  49.         }
  50.     }

  51.     // un-project points to real world
  52.     XnPoint3D* p3DPointSet = new XnPoint3D[ uPointNum ];
  53.     rDepthGen.ConvertProjectiveToRealWorld( uPointNum, pDepthPointSet, p3DPointSet );
  54.     delete[] pDepthPointSet;

  55.     // build point cloud
  56.     for( i = 0; i < uPointNum; ++ i )
  57.     {
  58.         // skip the depth 0 points
  59.         //if( p3DPointSet[i].Z == 0 )
  60.             //continue;

  61.         vPointCloud.push_back( SColorPoint3D( p3DPointSet[i], pImage[i] ) );
  62.     }
  63.     delete[] p3DPointSet;

  64.         }

  65. int main(void)
  66. {
  67.     XnStatus eResult = XN_STATUS_OK;
  68.     int i = 0;
  69.   
  70.         
  71.     // init
  72.     Context mContext;
  73.     eResult = mContext.Init();  

  74.     DepthGenerator mDepthGenerator;
  75.     eResult = mDepthGenerator.Create(mContext);
  76.     ImageGenerator mImageGenerator;
  77.     eResult = mImageGenerator.Create(mContext);

  78.     // set output mode
  79.     XnMapOutputMode mapMode;
  80.     mapMode.nXRes = XN_VGA_X_RES;
  81.     mapMode.nYRes = XN_VGA_Y_RES;
  82.     mapMode.nFPS  = 30;
  83.     eResult = mDepthGenerator.SetMapOutputMode(mapMode);
  84.     eResult = mImageGenerator.SetMapOutputMode(mapMode);

  85.         //mDepthGenerator.GetAlternativeViewPointCap().SetViewPoint( mImageGenerator );

  86.     // start generating  
  87.     eResult = mContext.StartGeneratingAll();  

  88.     // read data
  89.     vector<SColorPoint3D> vPointCloud;
  90.     while ( !xnOSWasKeyboardHit() )
  91.     {
  92.         eResult = mContext.WaitNoneUpdateAll();
  93.         // get the depth map
  94.         const XnDepthPixel*  pDepthMap = mDepthGenerator.GetDepthMap();

  95.         // get the image map
  96.         const XnRGB24Pixel*  pImageMap = mImageGenerator.GetRGB24ImageMap();

  97.         // generate point cloud
  98.         vPointCloud.clear();
  99.         GeneratePointCloud(mDepthGenerator, pDepthMap, pImageMap, vPointCloud);

  100.         // print point cloud运行结果只有这部分输出
  101.         cout.flags(ios::left);    //Left-aligned
  102.         cout << "Point number: " << vPointCloud.size() << endl;
  103.         for(i=0;i<vPointCloud.size();i++)
  104.         {
  105.             cout << setw(10) << i;
  106.             cout << "X:" << setw(10) << vPointCloud[i].X;
  107.             cout << "Y:" << setw(10) << vPointCloud[i].Y;
  108.             cout << "Z:" << setw(10) << vPointCloud[i].Z;
  109.             cout << "R:" << setw(10) << vPointCloud[i].R;
  110.             cout << "G:" << setw(10) << vPointCloud[i].G;  
  111.             cout << "B:" << setw(10) << vPointCloud[i].B <<endl;
  112.         }
  113.     }
  114. // 将数据写入data.pcd并保存
  115. pcl::PointCloud<pcl::PointXYZRGB> cloud;
  116. cloud.width=640;
  117. cloud.height=480;
  118. cloud.is_dense=false;
  119. cloud.points.resize(cloud.width*cloud.height);
  120. for(size_t i=0;i<cloud.points.size();++i)
  121. {
  122. cloud.points[i].x=vPointCloud[i].X;
  123. cloud.points[i].y=vPointCloud[i].Y;
  124. cloud.points[i].z=vPointCloud[i].Z;
  125. cloud.points[i].r=vPointCloud[i].R;
  126. cloud.points[i].g=vPointCloud[i].G;
  127. cloud.points[i].b=vPointCloud[i].B;
  128. }

  129. pcl::io::savePCDFileASCII("data.pcd",cloud);
  130. std::cerr<<"Saved "<<cloud.points.size()<<" data points to data.pcd."<<std::endl;
  131. for(size_t i=0;i<cloud.points.size();++i)
  132. std::cerr<<"  "<<cloud.points[i].x<<" "<<cloud.points[i].y<<" "<<cloud.points[i].z<<std::endl;
  133. //stop
  134.     mContext.StopGeneratingAll();  
  135.     mContext.Shutdown();  
  136.         return(0);
  137. }

复制代码
代码运行结果只是输出XYZRGB的值,感觉代码并没有运行最后关于pcl的部分,没有生成data.pcd文件,更没有写入数据。我的目的就是利用openNI+PCL+Kinect得到一个记录三维点云坐标的pcd文件,我也试图调试过,但是因为水平太过菜鸟,始终找不到原因,恳请各位指点迷津,谢谢


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册加入PCL中国点云技术相关产学研社区

x
回复

使用道具 举报

发表于 2014-7-3 11:39:06 | 显示全部楼层
cloud.points.x=vPointCloud.X;
  cloud.points.y=vPointCloud.Y;
  cloud.points.z=vPointCloud.Z;
   cloud.points.r=vPointCloud.R;
  cloud.points.g=vPointCloud.G;
  cloud.points.b=vPointCloud.B;
改用cloud.points[i].x 其他类似,然后保存那条代码似乎要加<pcl::PointXYZRGB>记不太清  你自己试试看
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-7-3 15:56:03 | 显示全部楼层
本帖最后由 和你一样小馒头 于 2014-7-3 16:30 编辑
ruoyihen 发表于 2014-7-3 11:39
cloud.points.x=vPointCloud.X;
  cloud.points.y=vPointCloud.Y;
  cloud.points.z=vPointCloud.Z;

首先谢谢您的指导,是我在复制代码时粗心大意漏掉了,上文中已经改正过来。您提到的保存pcd文件,我也重新检查过,我是按照官网例程pcd_write.cpp写的,问题应该不是出在这里
回复 支持 反对

使用道具 举报

发表于 2014-7-3 21:34:57 | 显示全部楼层
你试试把全局变量i换一个字母试试

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册加入PCL中国点云技术相关产学研社区

x
回复 支持 反对

使用道具 举报

发表于 2014-12-19 11:36:09 | 显示全部楼层
:)楼主 你的代码后来怎么修改的呢,我试了下也是不能保存文件
回复 支持 反对

使用道具 举报

发表于 2014-12-19 13:59:33 | 显示全部楼层
代码的一些函数我没怎么弄懂呢,我安装的PCL1.6 ALL-in-one,在pcl1.6下利用cmake配置工程文件,在vs2010上调试通过, while ( !xnOSWasKeyboardHit() )中间有个循环需要按下键盘这样才能执行后面保存文件的操作。:loveliness:
回复 支持 反对

使用道具 举报

发表于 2014-12-23 17:12:41 | 显示全部楼层
楼主最后是怎么搞定的?
回复 支持 反对

使用道具 举报

发表于 2015-1-22 11:03:05 | 显示全部楼层
你的错误是出在,while循环,应该把写入PCD文件指令放在while循环里面
回复 支持 反对

使用道具 举报

发表于 2015-3-20 17:50:07 | 显示全部楼层
新手,学习中,感觉你写的代码对我还是有启发的
回复 支持 反对

使用道具 举报

本版积分规则

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

GMT+8, 2024-4-28 08:11 , Processed in 1.800631 second(s), 17 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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