http://qiita.com/mountcedar/items/6d97efc0ea6c68c7674f
main.cpp
#include <OpenNI.h> #include <opencv2/opencv.hpp> #include <vector> int main() { try { openni::OpenNI::initialize(); openni::Device device; int ret = device.open( openni::ANY_DEVICE ); if ( ret != openni::STATUS_OK ) { return -1; } openni::VideoStream colorStream; colorStream.create(device, openni::SENSOR_COLOR); colorStream.start(); openni::Recorder recorder; recorder.create( "kinect.oni" ); recorder.attach( colorStream ); recorder.start(); std::vector<openni::VideoStream*> streams; streams.push_back( &colorStream ); cv::Mat colorImage; while ( 1 ) { int changedIndex; openni::OpenNI::waitForAnyStream( &streams[0], streams.size(), &changedIndex ); if ( changedIndex == 0 ) { openni::VideoFrameRef colorFrame; colorStream.readFrame( &colorFrame ); if ( colorFrame.isValid() ) { colorImage = cv::Mat( colorStream.getVideoMode().getResolutionY(), colorStream.getVideoMode().getResolutionX(), CV_8UC3, (char*)colorFrame.getData() ); cv::cvtColor( colorImage, colorImage, CV_BGR2RGB ); cv::imshow( "Color Camera", colorImage ); } } int key = cv::waitKey( 10 ); if ( key == 'q' ) { break; } } } catch ( std::exception& ) { std::cout << openni::OpenNI::getExtendedError() << std::endl; } }
$ g++-4.2 -g main.cpp -o main -L/usr/local/lib/ni2 -I/usr/local/include/ni2 -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lOpenNI2 -v
INCLUDE = -I/usr/local/include/ni2 -I/usr/local/include LIBRARY = -L/usr/local/lib/ni2 -L/usr/local/lib LIBFILE = -lopencv_core -lopencv_imgproc -lopencv_highgui -lOpenNI2 TARGET = main CC = g++-4.2 CFLAGS = -g all: $(TARGET).cpp $(CC) $(CFLAGS) $(INCLUDE) $(LIBRARY) $(LIBFILE) $(TARGET).cpp -o $(TARGET)