1.PCD文件读写
- #include
- #include
- #include
-
-
-
- void savePcdFun()//save pcdfile
- {
- pcl::PointCloud
cloud; - cloud.width = 5;
- cloud.height = 1;
- cloud.is_dense = false;
- cloud.points.resize(cloud.width * cloud.height);
-
- for (int i = 0; i < cloud.points.size(); ++i)
- {
- cloud.points[i].x = 1024 * rand() / (RAND_MAX + 0.1f);
- cloud.points[i].y = 1024 * rand() / (RAND_MAX + 0.1f);
- cloud.points[i].z = 1024 * rand() / (RAND_MAX + 0.1f);
- }
-
- pcl::io::savePCDFile("do1.pcd", cloud);
- return;
-
-
- }
-
-
- void loadPcdFun()//load pcdfile
- {
- pcl::PointCloud
cloud; - if (pcl::io::loadPCDFile("ajaccio_2 - Cloud.pcd", cloud) ==-1)
- {
- PCL_ERROR("load pcdfile failed!");
- }
-
- for (int i = 0; i < cloud.points.size(); ++i)
- {
- std::cout << cloud.points[i].x << " " << cloud.points[i].y
- << " " << cloud.points[i].z<< std::endl;
- }
- return;
-
- }
-
-
-
- }
2.点云字段或数据连接:
- void concateFun()//concatenate filed
- {
- pcl::PointCloud
cloud_a; - if (pcl::io::loadPCDFile("do1.pcd", cloud_a) == -1)
- {
- PCL_ERROR("load pcdfile failed !");
- }
- pcl::PointCloud
cloud_b_normal; - cloud_b_normal.width = 5;
- cloud_b_normal.height = 1;
- cloud_b_normal.is_dense = false;
- cloud_b_normal.points.resize(cloud_b_normal.width * cloud_b_normal.height);
-
- for (int i = 0; i < cloud_b_normal.points.size(); ++i)
- {
- cloud_b_normal.points[i].normal[0]=1024*rand()/(RAND_MAX+1.0f);
- cloud_b_normal.points[i].normal[1]= 1024 * rand() / (RAND_MAX + 1.0f);
- cloud_b_normal.points[i].normal[2]= 1024 * rand() / (RAND_MAX + 1.0f);
-
- }
- pcl::PointCloud
cloud_c; - pcl::concatenateFields(cloud_a, cloud_b_normal, cloud_c);
- for (int j = 0; j < cloud_c.points.size(); ++j)
- {
- std::cout << cloud_c.points[j].x << " " << cloud_c.points[j].y
- << " " << cloud_c.points[j].z << " " << cloud_c.points[j].normal[0]
- << " " << cloud_c.points[j].normal[1] << " " << cloud_c.points[j].normal[2] <<
- std::endl;
- }
3.基于OpenNI接口的点云数据获取
- #pragma
- #include
- #include
- #include
- #include
- #include
-
- //读取深度信息 实时显示3d点云
- class SimpleOpenNIProcessor
- {
- public:
- pcl::visualization::CloudViewer viewer;
-
- //construct a cloud viewer,with a window name
- SimpleOpenNIProcessor() :viewer("pcl openni viewer")
- {
-
- }
- //定义回调函数cloub_cb_,获取到数据时对数据进行处理
- void cloud_cb_(const pcl::PointCloud
::ConstPtr& cloud) - {
- if (!viewer.wasStopped())
- {
- viewer.showCloud(cloud);
- }
- }
-
- void run()
- {
- //create a new grabber for OpenNI Devices
- pcl::Grabber* m_interface = new pcl::io::OpenNI2Grabber();
-
- //定义回调函数
- boost::function<void(pcl::PointCloud
::ConstPtr&)> f = - boost::bind(&SimpleOpenNIProcessor::cloud_cb_, this, _1);
-
- //注册回调函数
- boost::signals2::connection c = m_interface->registerCallback(f);
-
- //start receive cloud
- m_interface->start();
-
- while (!viewer.wasStopped())
- {
- boost::this_thread::sleep(boost::posix_time::seconds(1));
- }
-
- //stop data aquisition
- m_interface->stop();
-
- }
- };
- int main()
- {
- SimpleOpenNIProcessor myViewer;
- myViewer.run();
- return 0;
- }