OpenNI 1.5.4: NiSimpleRead.cpp - sample program


NiSimpleRead.cpp - sample program

Source file: Click the following link to view the source code file:

  • NiSimpleRead.cpp

This section describes the SimpleRead sample program.

Global Declaration Block

The declaration block at the top of the program declares a Context object, a ScriptNode object, and an EnumerationErrors object. The context is workspace where the application builds an OpenNI production graph. The script node object loads and contains the OpenNI script and is the base node for the production graph. The production graph is a network of production nodes and is the principal OpenNI object model. The EnumerationErrors and XnStatus object types are for collecting errors from any of the OpenNI functions. Also declared is an OpenNI status flag for collecting return values from method calls.

            XnStatus nRetVal = XN_STATUS_OK;
            Context context;
            ScriptNode scriptNode;
            EnumerationErrors errors;

Main Program

Use Script to Set up a Context and Production Graph

The InitFromXmlFile() method is a shorthand combination of two other initialization methods — Init() and then RunXmlScriptFromFile() — which initializes the context object and then creates a production graph from an XML file. The XML script file describes all the nodes you want to create. For each node description in the XML file, this method creates a node in the production graph.

                nRetVal = context.InitFromXmlFile(fn, scriptNode, &errors);

Verify Existence of Nodes in the Sample Script File

Summary: This code is some more verification code. This time it checks that OpenNI found at least one node definition in the script file. The program continues execution only if at least one node definition if found.

            if (nRetVal == XN_STATUS_NO_NODE_PRESENT)
                    XnChar strError[1024];
                else if (nRetVal != XN_STATUS_OK)
                    printf("Open failed: %s\n", xnGetStatusString(nRetVal));
                    return (nRetVal);

Get a Node from the Production Graph

Assuming that the above call to InitFromXmlFile() succeeded, a production graph is then created.

The FindExistingNode() method in the following code block tries to get a reference to any one of the production nodes. This call specifies XN_NODE_TYPE_DEPTH to get a reference to a DepthGenerator node. A DepthGenerator node generates a depth map as an array of pixels, where each pixel is a depth value representing a distance from the sensor in millimeters. A reference to the node is returned in the depth parameter.

                DepthGenerator depth = (DepthGenerator)context.findExistingNode(NodeType.DEPTH);

Initialize the FPS Calculator

The following declaration block initializes the FPS Calculator.

                XnFPSData xnFPS;
                nRetVal = xnFPSInit(&xnFPS, 180);

Declare the DepthGenerator's Frame Object

The following statement declares a metadata object to provide a frame object for the xn::DepthGenerator node. A generator node's frame object contains a generated data frame and all its associated properties. This frame object, comprising the data frame and its properties, is accessible through the node's metadata object.

                DepthMetaData depthMD;

Main Program Loop

The main program loop repeatedly updates the data available in the node for output, and then gets the frame object (via the metadata object). The program then calculates the mid-point of the scene's 2D (two-dimensional) area.

Updating the Data Available for Output

the WaitOneUpdateAll() method in the following statement updates all generator nodes in the context that have new data available, first waiting for a specified node to have new data available. The application can then get the data (for example, using a metadata GetData() method). This method has a timeout.

                    nRetVal = context.WaitOneUpdateAll(depth);

Mark the Frame


Get the DepthGenerator's Data

The following statement gets the latest generated depth frame object, saving it as a metadata object.


The following print statement accesses the frame data, its ID, and the frame data's associated configuration, The frame ID is the ID of the frame object. Frame IDs are a sequential unique number with a wrap around. This method is inherited from the OutputMetaData class.

                    printf("Frame %d Middle point is: %u. FPS: %f\n", depthMD.FrameID(), depthMD(depthMD.XRes() / 2, depthMD.YRes() / 2), xnFPSCalc(&xnFPS));

In the above, XRes() and YRes() are the dimensions of the FOV in the scene. The call to depthMD() accesses a depth pixel through an X,Y coordinate. By calculating XRes()/2 and YRes()/2, this accesses the middle pixel.

Generated on Wed May 16 2012 10:16:06 for OpenNI 1.5.4 by   doxygen