OpenNI2显示深度、彩色及融合图像

来源:互联网 发布:淘宝五十字好评大全 编辑:程序博客网 时间:2024/04/27 00:13

环境:OpenNI2(http://structure.io/openni); OpenCV2.4.6; VS2010

win32工程。

一、在该工程下,openCV环境配置可参考:http://wiki.opencv.org.cn/index.php/VC_2010%E4%B8%8B%E5%AE%89%E8%A3%85OpenCV2.4.4

二、OpenNI(安装默认路径)配置:

1、首先拷贝\OpenNI2\Redist目录下的所有文件到VS的工作目录


2、包含 相应的头文件跟库文件





一个经验证可以用的基于OpenNI2程序(致以谢意)

摘自:http://blog.csdn.net/chenxin_130/article/details/8619909

/*************************OpenNI2 Deep, Color and Fusion ImageAuthor: Xin Chen, 2013.2Blog: http://blog.csdn.net/chenxin_130*************************/#include <stdlib.h>#include <iostream>#include <string>#include "OpenNI.h"#include "opencv2/core/core.hpp"#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"using namespace std;using namespace cv;using namespace openni;void CheckOpenNIError( Status result, string status ){ if( result != STATUS_OK ) cerr << status << " Error: " << OpenNI::getExtendedError() << endl;}int main( int argc, char** argv ){Status result = STATUS_OK;      //OpenNI2 imageVideoFrameRef oniDepthImg;    VideoFrameRef oniColorImg;//OpenCV imagecv::Mat cvDepthImg;cv::Mat cvBGRImg;cv::Mat cvFusionImg;cv::namedWindow("depth");cv::namedWindow("image");cv::namedWindow("fusion");char key=0;//【1】// initialize OpenNI2    result = OpenNI::initialize();CheckOpenNIError( result, "initialize context" );  // open device  Device device;    result = device.open( openni::ANY_DEVICE );//【2】// create depth stream     VideoStream oniDepthStream;    result = oniDepthStream.create( device, openni::SENSOR_DEPTH );//【3】// set depth video mode    VideoMode modeDepth;    modeDepth.setResolution( 640, 480 );    modeDepth.setFps( 30 );    modeDepth.setPixelFormat( PIXEL_FORMAT_DEPTH_1_MM );    oniDepthStream.setVideoMode(modeDepth);// start depth stream    result = oniDepthStream.start(); // create color stream    VideoStream oniColorStream;    result = oniColorStream.create( device, openni::SENSOR_COLOR );// set color video modeVideoMode modeColor;    modeColor.setResolution( 640, 480 );    modeColor.setFps( 30 );    modeColor.setPixelFormat( PIXEL_FORMAT_RGB888 );    oniColorStream.setVideoMode( modeColor);//【4】// set depth and color imge registration modeif( device.isImageRegistrationModeSupported(IMAGE_REGISTRATION_DEPTH_TO_COLOR ) ){device.setImageRegistrationMode( IMAGE_REGISTRATION_DEPTH_TO_COLOR );}// start color stream    result = oniColorStream.start();  while( key!=27 ) {  // read frameif( oniColorStream.readFrame( &oniColorImg ) == STATUS_OK ){// convert data into OpenCV typecv::Mat cvRGBImg( oniColorImg.getHeight(), oniColorImg.getWidth(), CV_8UC3, (void*)oniColorImg.getData() );cv::cvtColor( cvRGBImg, cvBGRImg, CV_RGB2BGR );cv::imshow( "image", cvBGRImg );}  if( oniDepthStream.readFrame( &oniDepthImg ) == STATUS_OK ){cv::Mat cvRawImg16U( oniDepthImg.getHeight(), oniDepthImg.getWidth(), CV_16UC1, (void*)oniDepthImg.getData() );cvRawImg16U.convertTo( cvDepthImg, CV_8U, 255.0/(oniDepthStream.getMaxPixelValue()));//【5】// convert depth image GRAY to BGRcv::cvtColor(cvDepthImg,cvFusionImg,CV_GRAY2BGR);cv::imshow( "depth", cvDepthImg );}//【6】cv::addWeighted(cvBGRImg,0.5,cvFusionImg,0.5,0,cvFusionImg);cv::imshow( "fusion", cvFusionImg );key = cv::waitKey(20);}//cv destroycv::destroyWindow("depth");cv::destroyWindow("image");cv::destroyWindow("fusion");    //OpenNI2 destroy    oniDepthStream.destroy();    oniColorStream.destroy();    device.close();    OpenNI::shutdown();return 0;}


0 0