Created
December 6, 2013 21:34
-
-
Save lxc-xx/7832487 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// KinectRecorder.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <iostream> | |
#include <fstream> | |
// OpenCV Header | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
#include <opencv2/imgproc/imgproc.hpp> | |
#include <Windows.h> | |
// OpenNI Header | |
#include <OpenNI.h> | |
#include <vector> | |
using namespace std; | |
using namespace cv; | |
using namespace openni; | |
string data_path = "D:\\xuanchong\\data\\"; | |
string case_name = "xuanchong_11_27"; | |
const int DEVNUM = 2; | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
SYSTEMTIME st; | |
//cout << "Please input the case name" << endl; | |
//cin >> case_name; | |
// 1. Initial OpenNI | |
if( OpenNI::initialize() != STATUS_OK ) | |
{ | |
cerr << "OpenNI Initial Error: " | |
<< OpenNI::getExtendedError() << endl; | |
return -1; | |
} | |
Array<DeviceInfo> devinfs; | |
OpenNI::enumerateDevices(&devinfs); | |
int devcnt = DEVNUM; | |
DeviceInfo df; | |
Device devlist[DEVNUM]; | |
VideoStream depths[DEVNUM]; | |
VideoStream colors[DEVNUM]; | |
Recorder recs[DEVNUM]; | |
ofstream log_file; | |
log_file.open (data_path + case_name + ".log"); | |
log_file << case_name << endl; | |
// 2. Open Devices | |
for (int i = 0; i < devcnt; i++) | |
{ | |
df = devinfs[i]; | |
Device &mDevice = devlist[i]; | |
if( mDevice.open( df.getUri() ) != STATUS_OK ) | |
{ | |
cerr << "Can't Open Device" << i << ": " | |
<< OpenNI::getExtendedError() << endl; | |
return -1; | |
} | |
// 3. Create depth stream | |
VideoStream & mDepthStream = depths[i]; | |
if( mDevice.hasSensor( SENSOR_DEPTH ) ) | |
{ | |
if( mDepthStream.create( mDevice, SENSOR_DEPTH ) == STATUS_OK ) | |
{ | |
// 3a. set video mode | |
VideoMode mMode; | |
mMode.setResolution( 640, 480 ); | |
mMode.setFps( 30 ); | |
mMode.setPixelFormat( PIXEL_FORMAT_DEPTH_1_MM ); | |
if( mDepthStream.setVideoMode( mMode) != STATUS_OK ) | |
{ | |
cout << "Can't apply VideoMode: " | |
<< OpenNI::getExtendedError() << endl; | |
} | |
} | |
else | |
{ | |
cerr << "Can't create depth stream on device: " | |
<< OpenNI::getExtendedError() << endl; | |
return -1; | |
} | |
} | |
else | |
{ | |
cerr << "ERROR: This device does not have depth sensor" << endl; | |
return -1; | |
} | |
// 4. Create color stream | |
VideoStream &mColorStream = colors[i]; | |
if( mDevice.hasSensor( SENSOR_COLOR ) ) | |
{ | |
if( mColorStream.create( mDevice, SENSOR_COLOR ) == STATUS_OK ) | |
{ | |
// 4a. set video mode | |
VideoMode mMode; | |
mMode.setResolution( 640, 480 ); | |
mMode.setFps( 30 ); | |
mMode.setPixelFormat( PIXEL_FORMAT_RGB888 ); | |
if( mColorStream.setVideoMode( mMode) != STATUS_OK ) | |
{ | |
cout << "Can't apply VideoMode: " | |
<< OpenNI::getExtendedError() << endl; | |
} | |
// 4b. image registration | |
//if( mDevice.isImageRegistrationModeSupported( | |
// IMAGE_REGISTRATION_DEPTH_TO_COLOR ) ) | |
//{ | |
// mDevice.setImageRegistrationMode( IMAGE_REGISTRATION_DEPTH_TO_COLOR ); | |
//} | |
} | |
else | |
{ | |
cerr << "Can't create color stream on device: " | |
<< OpenNI::getExtendedError() << endl; | |
return -1; | |
} | |
} | |
// 5. create OpenCV Window | |
char ddn[100],cdn[100],odn[100]; | |
sprintf_s(cdn, "color%d",i); | |
sprintf_s(ddn, "depth%d",i); | |
sprintf_s(odn, "kinect%d",i); | |
namedWindow( ddn, CV_WINDOW_AUTOSIZE ); | |
namedWindow( cdn, CV_WINDOW_AUTOSIZE ); | |
//VideoWriter colorv,depthv; | |
//depthv.open(data_path+string(ddn)+case_name+string(".avi"),CV_FOURCC('D','I','V','X'),30,Size(640, 480)); | |
//colorv.open(data_path+string(cdn)+case_name+string(".avi"),CV_FOURCC('D','I','V','X'),30,Size(640, 480)); | |
Recorder & rec = recs[i]; | |
string p_name = data_path+case_name+"_"+string(odn)+string(".oni"); | |
cout << p_name << endl; | |
//string name = "./data/foo.oni"; | |
rec.create(p_name.c_str()); | |
//rec.create(p_name.c_str()); | |
//rec.create((data_path+string(odn)+case_name+string(".oni")).c_str()); | |
rec.attach(mColorStream,true); | |
rec.attach(mDepthStream,true); | |
mDepthStream.start(); | |
mColorStream.start(); | |
} | |
// 6. start | |
VideoFrameRef mColorFrame; | |
VideoFrameRef mDepthFrame; | |
int iMaxDepth = depths[0].getMaxPixelValue(); | |
bool isrecording = false; | |
int frame= 0; | |
while( true ) | |
{ | |
/* | |
for (int i = 0; i < devcnt; i++) | |
{ | |
VideoStream & mColorStream = colors[i]; | |
VideoStream & mDepthStream = depths[i]; | |
char ddn[100],cdn[100],odn[100]; | |
sprintf_s(cdn, "color%d",i); | |
sprintf_s(ddn, "depth%d",i); | |
sprintf_s(odn, "kinect%d",i); | |
// 7. check is color stream is available | |
if( mColorStream.isValid() ) | |
{ | |
// 7a. get color frame | |
if( mColorStream.readFrame( &mColorFrame ) == STATUS_OK ) | |
{ | |
// 7b. convert data to OpenCV format | |
const Mat mImageRGB( | |
mColorFrame.getHeight(), mColorFrame.getWidth(), | |
CV_8UC3, (void*)mColorFrame.getData() ); | |
// 7c. convert form RGB to BGR | |
Mat cImageBGR; | |
//cvtColor( mImageRGB, cImageBGR, CV_RGB2BGR ); | |
// 7d. show image | |
//imshow( cdn, cImageBGR ); | |
//if (isrecording) colorv << cImageBGR; | |
} | |
} | |
// 8a. get depth frame | |
if( mDepthStream.readFrame( &mDepthFrame ) == STATUS_OK ) | |
{ | |
// 8b. convert data to OpenCV format | |
const Mat mImageDepth( | |
mDepthFrame.getHeight(), mDepthFrame.getWidth(), | |
CV_16UC1, (void*)mDepthFrame.getData() ); | |
// 8c. re-map depth data [0,Max] to [0,255] | |
Mat mScaledDepth; | |
//mImageDepth.convertTo( mScaledDepth, CV_8U, 255.0 / iMaxDepth ); | |
// 8d. show image | |
//imshow( ddn, mScaledDepth ); | |
//imshow( "DepthEdge", mde); | |
//if (isrecording) depthv << mScaledDepth; | |
} | |
} | |
*/ | |
if( isrecording ) | |
{ | |
GetLocalTime(&st); | |
log_file << frame << " " << st.wHour << ":" << st.wMinute << ":" << st.wSecond << "." << st.wMilliseconds << endl; | |
} | |
// 6a. check keyboard | |
int key = waitKey(1); | |
if( key == 'q' ) break; | |
else if (key == 's') | |
{ | |
if (isrecording) continue; | |
isrecording = true; | |
frame = 0; | |
cout << "Recording ... " << endl; | |
for (int i = 0; i < devcnt; i++) | |
{ | |
recs[i].start(); | |
} | |
GetLocalTime(&st); | |
printf("%s\n%u:%u:%u.%u\n",case_name.c_str(), st.wHour,st.wMinute,st.wSecond,st.wMilliseconds); | |
//log_file << st.wHour << ":" << st.wMinute << ":" << st.wSecond << "." << st.wMilliseconds << endl; | |
} | |
frame++; | |
} | |
// 9. stop | |
for (int i = 0; i < devcnt; i++) | |
{ | |
recs[i].stop(); | |
depths[i].destroy(); | |
colors[i].destroy(); | |
devlist[i].close(); | |
} | |
GetLocalTime(&st); | |
printf("%u:%u:%u.%u\n", st.wHour,st.wMinute,st.wSecond,st.wMilliseconds); | |
//log_file << st.wHour << ":" << st.wMinute << ":" << st.wSecond << "." << st.wMilliseconds << endl; | |
log_file.close(); | |
//fclose(tsf); | |
//rec.destroy(); | |
OpenNI::shutdown(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment