open_kinect2_save_pcd

该博客介绍了如何使用PCL库和OpenNI2Grabber从Kinect2相机采集点云数据,并将其以PCD格式保存到磁盘。程序会监听键盘输入,当按下空格键时,会保存当前的点云数据,文件名自动编号以防止覆盖。CMakeLists.txt文件用于构建项目,确保正确链接PCL库。
摘要由CSDN通过智能技术生成

通过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})
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小码1号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值