通过kinect2相机采集点云数据(pcd格式),空格保存数据,代码如下:
#include <pcl/io/openni2_grabber.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/console/parse.h>
#include <iostream>
using namespace std;
using namespace pcl;
PointCloud<PointXYZRGBA>::Ptr cloudptr(new PointCloud<PointXYZRGBA>); // A cloud that will store color info.
PointCloud<PointXYZ>::Ptr fallbackCloud(new PointCloud<PointXYZ>); // A fallback cloud with just depth data.
boost::shared_ptr<visualization::CloudViewer> viewer; // Point cloud viewer object.
Grabber* openniGrabber; // OpenNI grabber that takes data from the device.
unsigned int filesSaved = 0; // For the numbering of the clouds saved to disk.
bool saveCloud(false), noColor(false); // Program control.
// This function is called every time the device has new data.
void
grabberCallback(const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr& cloud)
{
if (! viewer->wasStopped())
viewer->showCloud(cloud);
if (saveCloud)
{
stringstream stream;
stream << "inputCloud" << filesSaved << ".pcd";
string filename = stream.str();
// pcl::io::savePCDFileASCII(clusterName, s2.cloud);
if (pcl::io::savePCDFileASCII(filename, *cloud) == 0)
{
filesSaved++;
cout << "Saved " << filename << "." << endl;
}
else PCL_ERROR("Problem saving %s.\n", filename.c_str());
saveCloud = false;
}
}
// For detecting when SPACE is pressed.
void
keyboardEventOccurred(const visualization::KeyboardEvent& event, void* nothing)
{
if (event.getKeySym() == "space" && event.keyDown())
saveCloud = true;
}
// Creates, initializes and returns a new viewer.
boost::shared_ptr<visualization::CloudViewer>
createViewer()
{
boost::shared_ptr<visualization::CloudViewer> v
(new visualization::CloudViewer("OpenNI viewer"));
v->registerKeyboardCallback(keyboardEventOccurred);
return (v);
}
int
main(int argc, char** argv)
{
// Second mode, start fetching and displaying frames from the device.
openniGrabber = new io::OpenNI2Grabber();
if (openniGrabber == 0)
return -1;
boost::function<void (const PointCloud<PointXYZRGBA>::ConstPtr&)> f = boost::bind(&grabberCallback, _1);
openniGrabber->registerCallback(f);
viewer = createViewer();
openniGrabber->start();
// Main loop.
while (! viewer->wasStopped())
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
unsigned int filesSaved = 0;
这里可以调需要保存点云的后缀,在关闭kinect相机后重新打开,为避免数据覆盖就需要更改值。
以下是CMakeLists.txt:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
# rgb_depth_saver为工程的名字
project(main) # PCL
find_package(PCL 1.8 REQUIRED)
list(REMOVE_ITEM PCL_LIBRARIES "vtkproj4")
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS}) # 运行主程序
add_executable(main main.cpp) # 链接库
target_link_libraries(main ${PCL_LIBRARIES})