PCL----------ubuntu从kinect v2 使用openni2读取并保存pcd文件

环境要求:

libfreenect2,openni2,pcl

结果:

在显示的界面键盘按s或者空格保存pcd,按q退出

main.cpp

// Original code by Geoffrey Biggs, taken from the PCL tutorial in
// http://pointclouds.org/documentation/tutorials/pcl_visualizer.php

// Simple OpenNI viewer that also allows to write the current scene to a .pcd
// when pressing SPACE.

#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<PointXYZ>::Ptr cloudptr(new PointCloud<PointXYZ>); // 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.

void
printUsage(const char* programName)
{
    cout << "Usage: " << programName << " [options]"
         << endl
         << endl
         << "Options:\n"
         << endl
         << "\t<none>     start capturing from an OpenNI device.\n"
         << "\t-v FILE    visualize the given .pcd file.\n"
         << "\t-h         shows this help.\n";
}

// This function is called every time the device has new data.
void
grabberCallback(const PointCloud<PointXYZ>::ConstPtr& cloud)
{
    if (! viewer->wasStopped())
        viewer->showCloud(cloud);

    if (saveCloud)
    {
        stringstream stream;
        stream << "inputCloud" << filesSaved << ".pcd";
        string filename = stream.str();
        if (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)
{
    
    if (console::find_argument(argc, argv, "-h") >= 0)
    {
        printUsage(argv[0]);
        return -1;
    }
    bool justVisualize(false);
    
    string filename;
    if (console::find_argument(argc, argv, "-v") >= 0)
    {
        if (argc != 3)
        {
            printUsage(argv[0]);
            return -1;
        }

        filename = argv[2];
        justVisualize = true;
    }
    else if (argc != 1)
    {
        printUsage(argv[0]);
        return -1;
    }

    // First mode, open and show a cloud from disk.
    if (justVisualize)
    {
        // Try with color information...
        try
        {
            io::loadPCDFile<PointXYZ>(filename.c_str(), *cloudptr);
        }
        catch (PCLException e1)
        {
            try
            {
                // ...and if it fails, fall back to just depth.
                io::loadPCDFile<PointXYZ>(filename.c_str(), *fallbackCloud);
            }
            catch (PCLException e2)
            {
                return -1;
            }

            noColor = true;
        }

        cout << "Loaded " << filename << "." << endl;
        if (noColor)
            cout << "This cloud has no RGBA color information present." << endl;
        else cout << "This cloud has RGBA color information present." << endl;
    }
    // Second mode, start fetching and displaying frames from the device.
    else
    {
        openniGrabber = new io::OpenNI2Grabber();
        if (openniGrabber == 0)
            return -1;
        boost::function<void (const PointCloud<PointXYZ>::ConstPtr&)> f =
            boost::bind(&grabberCallback, _1);
        openniGrabber->registerCallback(f);
    }
    viewer = createViewer();
    if (justVisualize)
    {
        if (noColor)
            viewer->showCloud(fallbackCloud);
        else viewer->showCloud(cloudptr);
    }
    else openniGrabber->start();

    // Main loop.
    while (! viewer->wasStopped())
        boost::this_thread::sleep(boost::posix_time::seconds(1));

    if (! justVisualize)
        openniGrabber->stop();
}
 

 

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})

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值