PCL OpenNI2Grabber получает Point Cloud без программы просмотра

У меня камера Asus XTION. На моем компьютере установлена ​​Windows 7 с Visual Studio 2013. У меня также есть PCL, openCV, скомпилированный для VS2013, и установлен драйвер openNi. Теперь я хочу получить облако точек с камеры. С помощью образца я могу просмотреть фактические кадры и сохранить один:

    // 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.
    #define _CRT_SERCURE_NO_WARNINGS
    #include <pcl/io/openni2_grabber.h>
    #include <pcl/io/pcd_io.h>
    #include <pcl/visualization/cloud_viewer.h>
    #include <pcl/console/parse.h>
    #include <pcl/filters/passthrough.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.
    PointCloud<PointXYZRGBA>::Ptr filteredCloud(new PointCloud<PointXYZRGBA>);
    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<PointXYZRGBA>::ConstPtr& cloud)
    {
        if (!viewer->wasStopped())
            viewer->showCloud(cloud);

        if (saveCloud)
        {
            stringstream stream;
            stream << "inputCloud" << filesSaved << ".pcd";
            string filename = stream.str();

            // Filter object.
            PassThrough<pcl::PointXYZRGBA> filter;
            filter.setInputCloud(cloud);
            // Filter out all points with Z values not in the [0-2] range.
            filter.setFilterFieldName("z");
            filter.setFilterLimits(0.0, 1.5);

            filter.filter(*filteredCloud);

            if (io::savePCDFile(filename, *filteredCloud, true) == 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<PointXYZRGBA>(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 pcl::io::OpenNI2Grabber();
            if (openniGrabber == 0)
                return -1;
            boost::function<void(const PointCloud<PointXYZRGBA>::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();
    }

Но с этим кодом мне нужен зритель. Как я могу получить облако точек без программы просмотра? Он должен работать без Kinect SDK, только с PCL, openni.


person DripleX    schedule 08.04.2015    source источник
comment
Привет, я не вижу проблемы. Удаление ссылок на программу просмотра в этом коде не должно вызывать никаких проблем. Возможно, вы сможете объяснить, с какими трудностями вы это делаете?   -  person D.J.Duff    schedule 09.04.2015
comment
Привет, у меня нет проблем с этим кодом. Этот код работает без проблем. Но я хочу, чтобы в фоновом режиме работала пауза без окна средства просмотра PCL.   -  person DripleX    schedule 09.04.2015
comment
Начните с приведенного выше кода и удалите ссылки на зрителя.   -  person D.J.Duff    schedule 09.04.2015


Ответы (1)


Решил проблему так:

    #include <pcl/io/openni2_grabber.h>
    #include <pcl/io/pcd_io.h>
    #include <pcl/console/parse.h>
    #include <iostream>

    using namespace std;
    using namespace pcl;

    int main(int argc, char** argv)
    {
        PointCloud<PointXYZRGBA>::Ptr sourceCloud(new PointCloud<PointXYZRGBA>);

        boost::function<void(const PointCloud<PointXYZRGBA>::ConstPtr&)> function = [&sourceCloud](const PointCloud<PointXYZRGBA>::ConstPtr &cloud)
        {
            copyPointCloud(*cloud, *sourceCloud);
        };

        // Create Kinect2Grabber
        Grabber* grabber = new io::OpenNI2Grabber();
        // Regist Callback Function
        grabber->registerCallback(function);
        // Start Retrieve Data
        grabber->start();
        boost::this_thread::sleep(boost::posix_time::seconds(1));
        // Stop Retrieve Data
        grabber->stop();
        cout << "The Cloud size: " << sourceCloud->size() << " points ..." << endl;
    }
person DripleX    schedule 16.04.2015