Kinect开发教程八:OpenNI2显示深度、彩色及融合图像

来源:互联网 发布:林彪的军事才能 知乎 编辑:程序博客网 时间:2024/04/18 23:26

 在《Kinect开发教程二:OpenNI读取深度图像与彩色图像并显示》中,小斤介绍了OpenNI读取深度与彩色图像数据的方法,并且借助OpenCV进行显示。

      OpenNI2在接口上与OpenNI有了较大变化,具体更新可以查看《OpenNI Migration Guide》。从获取深度,彩色传感器的数据而言,小斤觉得调用更为直观,但对于Kinect,一大缺憾是不支持OpenNI2提供的深度与彩色图像配准的方法(体现在下文中的device.isImageRegistrationModeSupported()方法)。

      但使用Kinect的童鞋也不必沮丧,在OpenNI2.1 beta中,小斤看到了新增的convertDepthToColorCoordinates()方法可以做一些深度与彩色坐标数据的转化,它的效果应该是与device.setImageRegistrationMode( IMAGE_REGISTRATION_DEPTH_TO_COLOR )类似的,有兴趣的童鞋可以尝试一下。

      在显示方面,小斤还是使用OpenCV,这次是使用OpenCV的C++接口进行操作。

[cpp] view plaincopy
  1. /************************* 
  2. OpenNI2 Deep, Color and Fusion Image 
  3. Author: Xin Chen, 2013.2 
  4. Blog: http://blog.csdn.net/chenxin_130 
  5. *************************/  
  6.   
  7. #include <stdlib.h>  
  8. #include <iostream>  
  9. #include <string>  
  10. #include "OpenNI.h"  
  11. #include "opencv2/core/core.hpp"  
  12. #include "opencv2/highgui/highgui.hpp"  
  13. #include "opencv2/imgproc/imgproc.hpp"  
  14. using namespace std;  
  15. using namespace cv;  
  16. using namespace openni;  
  17.   
  18. void CheckOpenNIError( Status result, string status )  
  19. {   
  20.     if( result != STATUS_OK )   
  21.         cerr << status << " Error: " << OpenNI::getExtendedError() << endl;  
  22. }  
  23.   
  24. int main( int argc, char** argv )  
  25. {  
  26.     Status result = STATUS_OK;    
  27.       
  28.     //OpenNI2 image  
  29.     VideoFrameRef oniDepthImg;  
  30.     VideoFrameRef oniColorImg;  
  31.   
  32.     //OpenCV image  
  33.     cv::Mat cvDepthImg;  
  34.     cv::Mat cvBGRImg;  
  35.     cv::Mat cvFusionImg;  
  36.       
  37.     cv::namedWindow("depth");  
  38.     cv::namedWindow("image");  
  39.     cv::namedWindow("fusion");  
  40.     char key=0;  
  41.   
  42.     //【1】  
  43.     // initialize OpenNI2  
  44.     result = OpenNI::initialize();  
  45.     CheckOpenNIError( result, "initialize context" );    
  46.   
  47.     // open device    
  48.     Device device;  
  49.     result = device.open( openni::ANY_DEVICE );  
  50.   
  51.     //【2】  
  52.     // create depth stream   
  53.     VideoStream oniDepthStream;  
  54.     result = oniDepthStream.create( device, openni::SENSOR_DEPTH );  
  55.   
  56.     //【3】  
  57.     // set depth video mode  
  58.     VideoMode modeDepth;  
  59.     modeDepth.setResolution( 640, 480 );  
  60.     modeDepth.setFps( 30 );  
  61.     modeDepth.setPixelFormat( PIXEL_FORMAT_DEPTH_1_MM );  
  62.     oniDepthStream.setVideoMode(modeDepth);  
  63.     // start depth stream  
  64.     result = oniDepthStream.start();  
  65.    
  66.     // create color stream  
  67.     VideoStream oniColorStream;  
  68.     result = oniColorStream.create( device, openni::SENSOR_COLOR );  
  69.     // set color video mode  
  70.     VideoMode modeColor;  
  71.     modeColor.setResolution( 640, 480 );  
  72.     modeColor.setFps( 30 );  
  73.     modeColor.setPixelFormat( PIXEL_FORMAT_RGB888 );  
  74.     oniColorStream.setVideoMode( modeColor);  
  75.       
  76. //【4】  
  77.     // set depth and color imge registration mode  
  78.     if( device.isImageRegistrationModeSupported(IMAGE_REGISTRATION_DEPTH_TO_COLOR ) )  
  79.     {  
  80.         device.setImageRegistrationMode( IMAGE_REGISTRATION_DEPTH_TO_COLOR );  
  81.     }  
  82.     // start color stream  
  83.     result = oniColorStream.start();    
  84.   
  85.     while( key!=27 )   
  86.     {    
  87.         // read frame  
  88.         if( oniColorStream.readFrame( &oniColorImg ) == STATUS_OK )  
  89.         {  
  90.             // convert data into OpenCV type  
  91.             cv::Mat cvRGBImg( oniColorImg.getHeight(), oniColorImg.getWidth(), CV_8UC3, (void*)oniColorImg.getData() );  
  92.             cv::cvtColor( cvRGBImg, cvBGRImg, CV_RGB2BGR );  
  93.             cv::imshow( "image", cvBGRImg );  
  94.         }  
  95.     
  96.         if( oniDepthStream.readFrame( &oniDepthImg ) == STATUS_OK )  
  97.         {  
  98.             cv::Mat cvRawImg16U( oniDepthImg.getHeight(), oniDepthImg.getWidth(), CV_16UC1, (void*)oniDepthImg.getData() );  
  99.             cvRawImg16U.convertTo( cvDepthImg, CV_8U, 255.0/(oniDepthStream.getMaxPixelValue()));  
  100.             //【5】  
  101.             // convert depth image GRAY to BGR  
  102.             cv::cvtColor(cvDepthImg,cvFusionImg,CV_GRAY2BGR);  
  103.             cv::imshow( "depth", cvDepthImg );  
  104.         }  
  105.         //【6】  
  106.         cv::addWeighted(cvBGRImg,0.5,cvFusionImg,0.5,0,cvFusionImg);  
  107.         cv::imshow( "fusion", cvFusionImg );  
  108.         key = cv::waitKey(20);  
  109.     }  
  110.   
  111.     //cv destroy  
  112.     cv::destroyWindow("depth");  
  113.     cv::destroyWindow("image");  
  114.     cv::destroyWindow("fusion");  
  115.   
  116.     //OpenNI2 destroy  
  117.     oniDepthStream.destroy();  
  118.     oniColorStream.destroy();  
  119.     device.close();  
  120.     OpenNI::shutdown();  
  121.   
  122.     return 0;  
  123. }  

      小斤由上到下解释一把:

      【1】使用OpenNI::initialize()方法进行初始化,对于错误处理,可以使用OpenNI::getExtendedError()方法。在这里,Device对象打开任意一个可用设备。

      【2】在OpenNI2中,可以通过创建VideoStream视频流对象来读取设备的深度图像和色彩图像数据。

      【3】对于VideoStream视频流对象,我们可以设备它的Mode,包括分辨率,FPS,像素格式等等。对于像素格式的类型,可以使用VideoStream的getSensorInfo()方法获得,目前Kinect只有PIXEL_FORMAT_DEPTH_1_MM可供选择。

      【4】如果设备支持深度与彩色图像配准的话,小斤在这里使用OpenNI2自带的接口进行配准。在while循环中,各个VideoStream对象通过readFrame()来读取对应的图像数据。

      【5】将OpenNI的图像数据转换为OpenCV可显示的图像格式。对于彩色图像,可以先将数据塞入OpenCV三通道(8位)RGB对象,再转换到BGR来显示。对于深度图像,先放入单通道(16位)对象(这是因为深度数据的值域较大),最近将深度值等比例缩小到[0,255]的值域中,作为灰度图显示。

      【6】最后的图像融合,由于addWeighted()方法需要两个输入图像是同一类型,所以小斤首先将深度灰度图(单通道),转化为BGR图像,这样就与彩色图像一致了。再通过该方法进行融合,小斤使用的比例是0.5,0.5,也就是融合图像的每个像素点的值,都是(深度图像该点的像素值*0.5)+ (彩色图像该点的像素值*0.5)。

0 0