added oni-files reading
authorMaria Dimashova <no@email>
Wed, 21 Mar 2012 14:31:40 +0000 (14:31 +0000)
committerMaria Dimashova <no@email>
Wed, 21 Mar 2012 14:31:40 +0000 (14:31 +0000)
modules/highgui/src/cap_openni.cpp
samples/cpp/openni_capture.cpp

index 168a148..f38b1e9 100644 (file)
@@ -124,6 +124,8 @@ protected:
     xn::Context context;
     bool isContextOpened;
     
+    xn::ProductionNode productionNode;
+
     // Data generators with its metadata
     xn::DepthGenerator depthGenerator;
     xn::DepthMetaData  depthMetaData;
@@ -171,118 +173,158 @@ XnMapOutputMode defaultMapOutputMode()
 
 CvCapture_OpenNI::CvCapture_OpenNI( int index )
 {
+    XnStatus status;
+
     isContextOpened = false;
 
     // Initialize and configure the context.
-    if( context.Init() == XN_STATUS_OK )
+    status = context.Init();
+    if( status != XN_STATUS_OK )
     {
-        // Find devices
-        xn::NodeInfoList devicesList;
-        XnStatus status = context.EnumerateProductionTrees( XN_NODE_TYPE_DEVICE, NULL, devicesList, 0 );
-        if( status != XN_STATUS_OK )
-        {
-            std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to enumerate production trees: "
-                      << std::string(xnGetStatusString(status)) << std::endl;
-            return;
-        }
-
-        // Chose device according to index
-        xn::NodeInfoList::Iterator it = devicesList.Begin();
-        for( int i = 0; i < index; ++i ) it++;
+        std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to initialize the context: "
+                  << std::string(xnGetStatusString(status)) << std::endl;
+        return;
+    }
 
-        xn::NodeInfo deviceNode = *it;
-        status = context.CreateProductionTree( deviceNode );
-        if( status != XN_STATUS_OK )
-        {
-            std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to create production tree: "
-                      << std::string(xnGetStatusString(status)) << std::endl;
-            return;
-        }
+    // Find devices
+    xn::NodeInfoList devicesList;
+    status = context.EnumerateProductionTrees( XN_NODE_TYPE_DEVICE, NULL, devicesList, 0 );
+    if( status != XN_STATUS_OK )
+    {
+        std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to enumerate production trees: "
+                  << std::string(xnGetStatusString(status)) << std::endl;
+        return;
+    }
 
-        status = context.RunXmlScript( XMLConfig.c_str() );
-        if( status != XN_STATUS_OK )
-        {
-            std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to run xml script: "
-                      << std::string(xnGetStatusString(status)) << std::endl;
-            return;
-        }
+    // Chose device according to index
+    xn::NodeInfoList::Iterator it = devicesList.Begin();
+    for( int i = 0; i < index; ++i ) it++;
 
-        // Associate generators with context.
-        // enumerate the nodes to find if depth generator is present
-        xn::NodeInfoList depthList;
-        status = context.EnumerateExistingNodes( depthList, XN_NODE_TYPE_DEPTH );
-        if( status != XN_STATUS_OK )
-        {
-            std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to enumerate depth generators: "
-                      << std::string(xnGetStatusString(status)) << std::endl;
-            return;
-        }
-        if( depthList.IsEmpty() )
-        {
-            std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : The device doesn't have depth generator. Such devices aren't supported now." << std::endl;
-            return;
-        }
-        status = depthGenerator.Create( context );
-        if( status != XN_STATUS_OK )
-        {
-            std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to create depth generator: "
-                      << std::string(xnGetStatusString(status)) << std::endl;
-            return;
-        }
+    xn::NodeInfo deviceNode = *it;
+    status = context.CreateProductionTree( deviceNode, productionNode );
+    if( status != XN_STATUS_OK )
+    {
+        std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to create production tree: "
+                  << std::string(xnGetStatusString(status)) << std::endl;
+        return;
+    }
 
-        // enumerate the nodes to find if image generator is present
-        xn::NodeInfoList imageList;
-        status = context.EnumerateExistingNodes( imageList, XN_NODE_TYPE_IMAGE );
-        if( status != XN_STATUS_OK )
-        {
-            std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to enumerate image generators: "
-                      << std::string(xnGetStatusString(status)) << std::endl;
-            return;
-        }
+    status = context.RunXmlScript( XMLConfig.c_str() );
+    if( status != XN_STATUS_OK )
+    {
+        std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to run xml script: "
+                  << std::string(xnGetStatusString(status)) << std::endl;
+        return;
+    }
 
-        if( !imageList.IsEmpty() )
-        {
-            status = imageGenerator.Create( context );
-            if( status != XN_STATUS_OK )
-            {
-                std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to create image generator: "
-                          <<  std::string(xnGetStatusString(status)) << std::endl;
-                return;
-            }
-        }
+    // Associate generators with context.
+    // enumerate the nodes to find if depth generator is present
+    xn::NodeInfoList depthList;
+    status = context.EnumerateExistingNodes( depthList, XN_NODE_TYPE_DEPTH );
+    if( status != XN_STATUS_OK )
+    {
+        std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to enumerate depth generators: "
+                  << std::string(xnGetStatusString(status)) << std::endl;
+        return;
+    }
+    if( depthList.IsEmpty() )
+    {
+        std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : The device doesn't have depth generator. Such devices aren't supported now." << std::endl;
+        return;
+    }
+    status = depthGenerator.Create( context );
+    if( status != XN_STATUS_OK )
+    {
+        std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to create depth generator: "
+                  << std::string(xnGetStatusString(status)) << std::endl;
+        return;
+    }
 
-        // Set map output mode.
-        if( depthGenerator.IsValid() )
-            CV_DbgAssert( depthGenerator.SetMapOutputMode(defaultMapOutputMode()) == XN_STATUS_OK ); // xn::DepthGenerator supports VGA only! (Jan 2011)
-        if( imageGenerator.IsValid() )
-            CV_DbgAssert( imageGenerator.SetMapOutputMode(defaultMapOutputMode()) == XN_STATUS_OK );
+    // enumerate the nodes to find if image generator is present
+    xn::NodeInfoList imageList;
+    status = context.EnumerateExistingNodes( imageList, XN_NODE_TYPE_IMAGE );
+    if( status != XN_STATUS_OK )
+    {
+        std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to enumerate image generators: "
+                  << std::string(xnGetStatusString(status)) << std::endl;
+        return;
+    }
 
-        //  Start generating data.
-        status = context.StartGeneratingAll();
+    if( !imageList.IsEmpty() )
+    {
+        status = imageGenerator.Create( context );
         if( status != XN_STATUS_OK )
         {
-            std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to start generating OpenNI data: "
-                      << std::string(xnGetStatusString(status)) << std::endl;
+            std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to create image generator: "
+                      <<  std::string(xnGetStatusString(status)) << std::endl;
             return;
         }
+    }
 
-        if( !readCamerasParams() )
-        {
-            std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Could not read cameras parameters" << std::endl;
-            return;
-        }
+    // Set map output mode.
+    if( depthGenerator.IsValid() )
+        CV_DbgAssert( depthGenerator.SetMapOutputMode(defaultMapOutputMode()) == XN_STATUS_OK ); // xn::DepthGenerator supports VGA only! (Jan 2011)
+    if( imageGenerator.IsValid() )
+        CV_DbgAssert( imageGenerator.SetMapOutputMode(defaultMapOutputMode()) == XN_STATUS_OK );
 
-        outputMaps.resize( outputMapsTypesCount );
+    //  Start generating data.
+    status = context.StartGeneratingAll();
+    if( status != XN_STATUS_OK )
+    {
+        std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to start generating OpenNI data: "
+                  << std::string(xnGetStatusString(status)) << std::endl;
+        return;
+    }
 
-        isContextOpened = true;
+    if( !readCamerasParams() )
+    {
+        std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Could not read cameras parameters" << std::endl;
+        return;
     }
 
+    outputMaps.resize( outputMapsTypesCount );
+
+    isContextOpened = true;
+
     setProperty(CV_CAP_PROP_OPENNI_REGISTRATION, 1.0);
 }
 
 CvCapture_OpenNI::CvCapture_OpenNI(const char * filename)
 {
-    CV_Assert(0);
+    XnStatus status;
+
+    isContextOpened = false;
+
+    // Initialize and configure the context.
+    status = context.Init();
+    if( status != XN_STATUS_OK )
+    {
+        std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to initialize the context: "
+                  << std::string(xnGetStatusString(status)) << std::endl;
+        return;
+    }
+
+    // Open file
+    status = context.OpenFileRecording( filename, productionNode );
+    if( status != XN_STATUS_OK )
+    {
+        std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Failed to open input file (" << filename << "): "
+                  << std::string(xnGetStatusString(status)) << std::endl;
+        return;
+    }
+
+    std::cout << context.FindExistingNode( XN_NODE_TYPE_DEPTH, depthGenerator ) << std::endl;
+    std::cout << context.FindExistingNode( XN_NODE_TYPE_IMAGE, imageGenerator ) << std::endl;
+
+    if( !readCamerasParams() )
+    {
+        std::cerr << "CvCapture_OpenNI::CvCapture_OpenNI : Could not read cameras parameters" << std::endl;
+        return;
+    }
+
+    outputMaps.resize( outputMapsTypesCount );
+
+    isContextOpened = true;
 }
 
 CvCapture_OpenNI::~CvCapture_OpenNI()
@@ -424,6 +466,7 @@ double CvCapture_OpenNI::getDepthGeneratorProperty( int propIdx )
         break;
     case CV_CAP_PROP_OPENNI_REGISTRATION :
         propValue = depthGenerator.GetAlternativeViewPointCap().IsViewPointAs(imageGenerator) ? 1.0 : 0.0;
+        break;
     default :
     {
         std::stringstream ss;
@@ -731,15 +774,14 @@ IplImage* CvCapture_OpenNI::retrieveValidDepthMask()
 
 inline void getBGRImageFromMetaData( const xn::ImageMetaData& imageMetaData, cv::Mat& bgrImage )
 {
-    int cols = imageMetaData.XRes();
-    int rows = imageMetaData.YRes();
-
-    cv::Mat rgbImage( rows, cols, CV_8UC3 );
+    if( imageMetaData.PixelFormat() != XN_PIXEL_FORMAT_RGB24 )
+        CV_Error( CV_StsUnsupportedFormat, "Unsupported format of grabbed image\n" );
 
+    cv::Mat rgbImage( imageMetaData.YRes(), imageMetaData.XRes(), CV_8UC3 );
     const XnRGB24Pixel* pRgbImage = imageMetaData.RGB24Data();
 
     // CV_Assert( 3*sizeof(uchar) == sizeof(XnRGB24Pixel) );
-    memcpy( rgbImage.data, pRgbImage, cols*rows*sizeof(XnRGB24Pixel) );
+    memcpy( rgbImage.data, pRgbImage, rgbImage.total()*sizeof(XnRGB24Pixel) );
     cv::cvtColor( rgbImage, bgrImage, CV_RGB2BGR );
 }
 
index 2d1aac1..0428b8c 100644 (file)
@@ -93,9 +93,11 @@ void printCommandLineParams()
     cout << "-m        Mask to set which output images are need. It is a string of size 5. Each element of this is '0' or '1' and" << endl;
     cout << "          determine: is depth map, disparity map, valid pixels mask, rgb image, gray image need or not (correspondently)?" << endl ;
     cout << "          By default -m 01010 i.e. disparity map and rgb image will be shown." << endl ;
+    cout << "-r        Filename of .oni video file. The data will grabbed from it." << endl ;
 }
 
-void parseCommandLine( int argc, char* argv[], bool& isColorizeDisp, bool& isFixedMaxDisp, int& imageMode, bool retrievedImageFlags[] )
+void parseCommandLine( int argc, char* argv[], bool& isColorizeDisp, bool& isFixedMaxDisp, int& imageMode, bool retrievedImageFlags[],
+                       string& filename, bool& isFileReading )
 {
     // set defaut values
     isColorizeDisp = true;
@@ -108,6 +110,9 @@ void parseCommandLine( int argc, char* argv[], bool& isColorizeDisp, bool& isFix
     retrievedImageFlags[3] = true;
     retrievedImageFlags[4] = false;
 
+    filename.clear();
+    isFileReading = false;
+
     if( argc == 1 )
     {
         help();
@@ -154,6 +159,11 @@ void parseCommandLine( int argc, char* argv[], bool& isColorizeDisp, bool& isFix
                     exit(0);
                 }
             }
+            else if( !strcmp( argv[i], "-r" ) )
+            {
+                filename = argv[++i];
+                isFileReading = true;
+            }
             else
             {
                 cout << "Unsupported command line argument: " << argv[i] << "." << endl;
@@ -172,10 +182,17 @@ int main( int argc, char* argv[] )
     bool isColorizeDisp, isFixedMaxDisp;
     int imageMode;
     bool retrievedImageFlags[5];
-    parseCommandLine( argc, argv, isColorizeDisp, isFixedMaxDisp, imageMode, retrievedImageFlags );
+    string filename;
+    bool isVideoReading;
+    parseCommandLine( argc, argv, isColorizeDisp, isFixedMaxDisp, imageMode, retrievedImageFlags, filename, isVideoReading );
 
     cout << "Device opening ..." << endl;
-    VideoCapture capture( CV_CAP_OPENNI );
+    VideoCapture capture;
+    if( isVideoReading )
+        capture.open( filename );
+    else
+        capture.open( CV_CAP_OPENNI );
+
     cout << "done." << endl;
 
     if( !capture.isOpened() )
@@ -184,37 +201,41 @@ int main( int argc, char* argv[] )
         return -1;
     }
 
-    bool modeRes=false;
-    switch ( imageMode )
+    if( !isVideoReading )
     {
-        case 0:
-            modeRes = capture.set( CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_VGA_30HZ );
-            break;
-        case 1:
-            modeRes = capture.set( CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_SXGA_15HZ );
-            break;
-        case 2:
-            modeRes = capture.set( CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_SXGA_30HZ );
-            break;
-        default:
-            CV_Error( CV_StsBadArg, "Unsupported image mode property.\n");
+        bool modeRes=false;
+        switch ( imageMode )
+        {
+            case 0:
+                modeRes = capture.set( CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_VGA_30HZ );
+                break;
+            case 1:
+                modeRes = capture.set( CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_SXGA_15HZ );
+                break;
+            case 2:
+                modeRes = capture.set( CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_SXGA_30HZ );
+                break;
+            default:
+                CV_Error( CV_StsBadArg, "Unsupported image mode property.\n");
+        }
+        if (!modeRes)
+            cout << "\nThis image mode is not supported by the device, the default value (CV_CAP_OPENNI_SXGA_15HZ) will be used.\n" << endl;
     }
-    if (!modeRes)
-        cout << "\nThis image mode is not supported by the device, the default value (CV_CAP_OPENNI_SXGA_15HZ) will be used.\n" << endl;
 
     // Print some avalible device settings.
     cout << "\nDepth generator output mode:" << endl <<
-            "FRAME_WIDTH    " << capture.get( CV_CAP_PROP_FRAME_WIDTH ) << endl <<
-            "FRAME_HEIGHT   " << capture.get( CV_CAP_PROP_FRAME_HEIGHT ) << endl <<
-            "FRAME_MAX_DEPTH    " << capture.get( CV_CAP_PROP_OPENNI_FRAME_MAX_DEPTH ) << " mm" << endl <<
-            "FPS    " << capture.get( CV_CAP_PROP_FPS ) << endl;
+            "FRAME_WIDTH      " << capture.get( CV_CAP_PROP_FRAME_WIDTH ) << endl <<
+            "FRAME_HEIGHT     " << capture.get( CV_CAP_PROP_FRAME_HEIGHT ) << endl <<
+            "FRAME_MAX_DEPTH  " << capture.get( CV_CAP_PROP_OPENNI_FRAME_MAX_DEPTH ) << " mm" << endl <<
+            "FPS              " << capture.get( CV_CAP_PROP_FPS ) << endl <<
+            "REGISTRATION     " << capture.get( CV_CAP_PROP_OPENNI_REGISTRATION ) << endl;
     if( capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR_PRESENT ) )
     {
         cout <<
             "\nImage generator output mode:" << endl <<
-            "FRAME_WIDTH    " << capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FRAME_WIDTH ) << endl <<
-            "FRAME_HEIGHT   " << capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FRAME_HEIGHT ) << endl <<
-            "FPS    " << capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FPS ) << endl;
+            "FRAME_WIDTH   " << capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FRAME_WIDTH ) << endl <<
+            "FRAME_HEIGHT  " << capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FRAME_HEIGHT ) << endl <<
+            "FPS           " << capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FPS ) << endl;
     }
     else
     {