生成带有rgb值的pcd文件并显示

来源:互联网 发布:php 面相过程 编辑:程序博客网 时间:2024/06/05 04:32

pcd文件中的格式为x y z rgb而不是分开的r g b,所以在原数据的基础上要进行变形,

由r g b得到rgb(float):int rgb = ((int)r << 16 | (int)g << 8 | (int)b);  式中的“<<”为左移符号;

由rgb得到r g b(int):int r = (rgb >> 16) & 0x0000ff; 
                             int g = (rgb >> 8) & 0x0000ff; 
                                     int b = (rgb) & 0x0000ff; 

一个自己写的例子:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>  
#include <pcl/io/pcd_io.h>  
#include <pcl/point_types.h>  
 
  char *filepath="   ";  //自己添加
  double array[][]={NULL};  自己添加

   FILE *fp;
  
 int main (int argc, char** argv)  
{  
     if((fp=fopen(filepath,"r"))==NULL)
{ printf("can not open %s",filepath);
}


  pcl::PointCloud<pcl::PointXYZRGB> cloud;   
  cloud.width    =   ;  数组的行数
  cloud.height   = 1;  
  cloud.is_dense = false;  
  cloud.points.resize (cloud.width * cloud.height);
  
     while(!feof(fp))
{    
   for (int m=0;m<  ;++m)
{
 
int frgb=0;          
        fscanf(fp,"%lf %lf %lf %lf %lf %lf",&array[m][0],&array[m][1],&array[m][2],&array[m][3],&array[m][4],&array[m][5]);//后面三列的输入类型也要为lf。
cloud.points[m].x = array[m][0];  
            cloud.points[m].y = array[m][1];  
            cloud.points[m].z = array[m][2];
      
frgb=((int)array[m][3]<<16|(int)array[m][4]<<8|(int)array[m][5]);
cloud.points[m].rgb=*reinterpret_cast<float*>(&frgb);//frgb在这由int型转换成了float型。

                      //reinterpret_cast()为指针类型转换。
}
}
     fclose(fp);

  pcl::io::savePCDFileASCII ("  ", cloud);  
  std::cerr << "already save " << cloud.points.size () << " data points to myrgbpointS3.pcd" << std::endl;  
  
  return 0;  
}
显示点云就比较简单了,只要pcd没有问题,就可以正常显示,代码如下:

/*
*GreedyProjection是根据点云进行三角化,而possion则是对water-tight的模型进行重建
*形成了封闭的mesh和很多冗余信息,需要对possion的重建进行修剪才能得到相对正确的模型
*/
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/vtk_io.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/features/normal_3d.h>
#include <pcl/surface/gp3.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string.h>




int main (int argc, char** argv)
{
  // Load input file into a PointCloud<T> with an appropriate type
  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);

 if(pcl::io::loadPCDFile<pcl::PointXYZRGB>(" ",*cloud)==-1)//打开点云文件
 {
      PCL_ERROR("Couldn't read file\n");  
      return -1;
 }
  //显示结果图
  boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer("3D Viewer"));
  viewer->setBackgroundColor(1,1,1);  //设置背景
  pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
  viewer->addPointCloud<pcl::PointXYZRGB> (cloud, rgb, "my cloud"); //添加点云


  viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "my cloud"); //设置点的大小
  viewer->addCoordinateSystem(1000.0);  //设置坐标系的大小
  viewer->initCameraParameters();
  
  while(!viewer->wasStopped())
  {
      viewer->spinOnce(100);
 boost::this_thread::sleep (boost::posix_time::microseconds(100000));
  }
  // Finish
  return 0;
}

0 0
原创粉丝点击