Skip to content

samplesCode

Florian Lance edited this page May 2, 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

...

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.h"
#include "devices/rgbd/SWSaveKinectData.h"

int main()
{
	SWKinect kinectDevice;
	
	// init the kinect device
	if(kinectDevice.init() == -1)
	{
		std::cerr << "Error initializing kinect device. " << std::endl;
		return -1;
	}
	
	std::string path("./kinect/data_");
	double maxLength = 60.0; // maximum length of the saving
	double maxSize   = 20.0; // maximum size in Go
	double fps		 = 30.0; // fps
	
	SWSaveKinectData dataSaver(path, maxLength, maxSize);
	
	bool saveVideoData = true;
	bool saveCloudData = true;	
	dataSaver.start(saveVideoData, saveCloudData);
	
	char key = ' ';	
	while(key != 'q')
	{
		clock_t time = clock();
	
		// grab new kinect frame
		kinectDevice.grab();

		// save current kinect frame
		dataSaver.save(kinectDevice.bgrImage, kinectDevice.cloudMap);		
		
		// check time
		double elapsedTime = static_cast<double>((clock() - time) / CLOCKS_PER_SEC); 
		
		// wait for key events
		if(elapsedTime < 1000.0 / fps)
		{
			key = waitKey(1000.0 / fps - elapsedTime);
		}
		else
		{
			key = waitKey(5);
		}				
	}

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

Clone this wiki locally