Skip to content

samplesCode

Florian Lance edited this page May 5, 2014 · 11 revisions

kinect

  • Simple kinect interface
#include <iostream>
#include "devices/rgbd/SWKinect.h"

int main()
{
    // create an opencv window
    cvNamedWindow("rgb_kinect", CV_WINDOW_AUTOSIZE | CV_GUI_NORMAL);
    cvNamedWindow("cloud_map_kinect", CV_WINDOW_AUTOSIZE | CV_GUI_NORMAL);

    cvMoveWindow("rgb_kinect",200,200);
    cvMoveWindow("cloud_map_kinect",200+640,200);

    swDevice::SWKinect kinectDevice;

    // init the kinect device
    if(kinectDevice.init() == -1)
    {
        std::cerr << "Error initializing kinect device. " << std::endl;
        return -1;
    }

    char key = ' ';

    // set the display loop
    while(key != 'q')
    {
        // grab new kinect frame
        kinectDevice.grab();

        // display the kinect rgb image in the opencv window
        cv::imshow("rgb_kinect",kinectDevice.bgrImage);

        // display the kinect cloud map in the opencv window
        cv::imshow("cloud_map_kinect",kinectDevice.cloudMap);

        // wait key event for escaping the loop
        key = cv::waitKey(5);
    }

    // destroy windows
    cvDestroyWindow("rgb_kinect");
    cvDestroyWindow("cloud_map_kinect");

    return 0;
}
  • Threaded kinect interface
#include <iostream>
#include "devices/rgbd/SWKinect_thread.h"

int main()
{
    // create an opencv window
    cvNamedWindow("rgb_kinect", CV_WINDOW_AUTOSIZE | CV_GUI_NORMAL);
    cvNamedWindow("cloud_map_kinect", CV_WINDOW_AUTOSIZE | CV_GUI_NORMAL);

    cvMoveWindow("rgb_kinect",200,200);
    cvMoveWindow("cloud_map_kinect",200+640,200);

    swDevice::SWKinect_thread kinectDeviceT;


    // init the kinect device
    if(kinectDeviceT.init(0) == -1)
    {
        std::cerr << "Error initializing kinect device. " << std::endl;
        return -1;
    }

    // start listening the kinect device
    kinectDeviceT.startListening();

    while(!kinectDeviceT.isDataAvailable())
    {
        cv::waitKey(5);
    }

    char key = ' ';

    // set the display loop
    while(key != 'q')
    {
        // display the kinect rgb image in the opencv window
        cv::imshow("rgb_kinect" ,kinectDeviceT.bgrImage());

        // display the kinect cloud map in the opencv window
        cv::imshow("cloud_map_kinect", kinectDeviceT.cloudMap());

        // wait key event for escaping the loop
        key = cv::waitKey(5);
    }

    // stop listening kinect device    
    kinectDeviceT.stopListening();

    // destroy windows
    cvDestroyWindow("rgb_kinect");
    cvDestroyWindow("cloud_map_kinect");

    return 0;
}

data recorder/loader

  • Kinect data recorder : this program will use boost mapped file for recording kinect/xtion data in real time.
#include <iostream>
#include <time.h>
#include "devices/rgbd/SWKinect_thread.h"
#include "devices/rgbd/SWSaveKinectData.h"

#include "boost/filesystem.hpp"

int main()
{

    // create an opencv window
    cvNamedWindow("rgb_kinect", CV_WINDOW_AUTOSIZE | CV_GUI_NORMAL);
    cvNamedWindow("cloud_map_kinect", CV_WINDOW_AUTOSIZE | CV_GUI_NORMAL);

    cvMoveWindow("rgb_kinect",200,200);
    cvMoveWindow("cloud_map_kinect",200+640,200);

    swDevice::SWKinect_thread kinectDeviceT;

    // init the kinect device
    if(kinectDeviceT.init(0) == -1)
    {
        std::cerr << "Error initializing kinect device. " << std::endl;
        return -1;
    }

    // start listening the kinect device
    kinectDeviceT.startListening();
    while(!kinectDeviceT.isDataAvailable())
    {
        cv::waitKey(5);
    }

    std::string path("./kinect_save/data_");
    boost::filesystem::path dir("./kinect_save");
    boost::filesystem::create_directory(dir);

    double maxLength = 60.0; // maximum length of the saving
    double maxSize   = 20.0; // maximum size in Go
    double fps       = 30.0; // fps

    swDevice::SWSaveKinectData dataSaver(path, maxLength, maxSize);

    bool saveVideoData = true;
    bool saveCloudData = true;
    dataSaver.start(saveVideoData, saveCloudData);

    char key = ' ';

    while(key != 'q')
    {
        clock_t time = clock();

        // display the kinect rgb image in the opencv window
        cv::imshow("rgb_kinect",kinectDeviceT.bgrImage());

        // display the kinect cloud map in the opencv window
        cv::imshow("cloud_map_kinect",kinectDeviceT.cloudMap());

        // save current kinect frame
        if(!dataSaver.save(kinectDeviceT.bgrImage(), kinectDeviceT.cloudMap()))
        {
            break;
        }

        // check time
        double elapsedTime = ((float)(clock() - time) / CLOCKS_PER_SEC);

        // wait for key events
        if(elapsedTime < 1000.0 / fps)
        {
            key = cv::waitKey(static_cast<int>(1000.0 / fps - elapsedTime));
        }
        else
        {
            key = cv::waitKey(5);
        }
    }

    // stop listening kinect data
    kinectDeviceT.stopListening();

    // stop the saving and create the data files
    dataSaver.stop();

    // destroy windows
    cvDestroyWindow("rgb_kinect");
    cvDestroyWindow("cloud_map_kinect");

    return 0;
}
  • Kinect data loader: this program will load previoulsy recorded kinect/xtion data.
#include <iostream>
#include <time.h>
#include "devices/rgbd/SWLoadKinectData.h"

int main()
{

    // create an opencv window
    cvNamedWindow("rgb_kinect", CV_WINDOW_AUTOSIZE | CV_GUI_NORMAL);
    cvNamedWindow("cloud_map_kinect", CV_WINDOW_AUTOSIZE | CV_GUI_NORMAL);
    // move opencv windows
    cvMoveWindow("rgb_kinect",200,200);
    cvMoveWindow("cloud_map_kinect",200+640,200);

    std::string path("./kinect_save/data_");

    swDevice::SWLoadKinectData dataLoader(path);

    bool saveVideoData = true;
    bool saveCloudData = true;
    double fps       = 30.0; // fps

    dataLoader.start();

    char key = ' ';
    while(key != 'q')
    {
        clock_t time = clock();

        cv::Mat rgb, cloudMap;

        // grab new kinect frame
        if(saveVideoData)
        {
            if(!dataLoader.grabVideo(rgb))
            {
                break;
            }
        }
        if(saveCloudData)
        {
            if(!dataLoader.grabCloud(cloudMap))
            {
                break;
            }
        }

        // display the kinect loaded rgb image in the opencv window
        cv::imshow("rgb_kinect",rgb);

        // display the kinect loaded cloud map in the opencv window
        cv::imshow("cloud_map_kinect",cloudMap);

        // check time
        double elapsedTime = ((float)(clock() - time) / CLOCKS_PER_SEC);

        // wait for key events
        if(elapsedTime < 1000.0 / fps)
        {
            key = cv::waitKey(static_cast<int>(1000.0 / fps - elapsedTime));
        }
        else
        {
            key = cv::waitKey(5);
        }
    }

    // stop the loading
    dataLoader.stop();

    // destroy windows
    cvDestroyWindow("rgb_kinect");
    cvDestroyWindow("cloud_map_kinect");

    return 0;
}

Clone this wiki locally