OpenNI 1.5.4: NiSimpleCreate.cpp - sample program

OpenNI

NiSimpleCreate.cpp - sample program

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

  • NiSimpleCreate.cpp

This section describes the NiSimpleCreate sample program. This sample program is similar to NiSimpleRead, but sets up a production graph using function calls instead of an XML file.

This sample program is similar to NiSimpleRead, but sets up a production graph using function calls instead of an XML file.

Key Features of the Sample Program

This sample program demonstrates how to set up an OpenNI production graph These are the features and concepts demonstrated by the sample program:

  • The OpenNI production graph
  • Setting up a production graph by using function calls
  • Node types: DepthGenerator node
  • Metadata ("frame objects")
  • Context
  • Method status returns and errors - At every stage of the program, error checking is performed

Global Declaration Block

The context initialization block of code at the top of the program declares and initializes a Context object. A context is a work space where you build your OpenNI production graph

            XnStatus nRetVal = XN_STATUS_OK;
            Context context;

            nRetVal = context.Init();
            CHECK_RC(nRetVal, "Initialize context");

Initializing a Context

The context initialization block of code at the top of the program declares and initializes a Context object. A context is a work space where you build your OpenNI production graph

            XnStatus nRetVal = XN_STATUS_OK;
            Context context;

            nRetVal = context.Init();
            CHECK_RC(nRetVal, "Initialize context");

Creating a Production Graph

A production graph is created by creating at least one node. Ibn this acse it is a DepthGenerator node. A DepthGenerator node generates depth maps. Depth maps are data frames of pixels, where each pixel value is its distance from the sensor in millimeters.

        DepthGenerator depth;
        nRetVal = depth.Create(context);

The following code block is a call to a macro. It simply checks that the return code reports a successful execution, and if not prints an error message and stops the application execution, returning control to the operating system.

Entering the Node into 'Generating state'

The following function call enters the production graph into 'Generating state'. This causes the nodes in the production graph to start generating data (by default, when a generator node is created, it's not generating any new data. This allows an application to configure it before actually starting it.

        nRetVal = context.StartGeneratingAll();

Declaring the DepthGenerator Frame Object

The application accesses the DepthGenerator node's data and associated configuration through the node's frame object (provided by the metadata object). This frame object provides fast access to a stored frame object data and its associated configuration.

        DepthMetaData depthMD;

3.6 Main Program Loop

Main Program Loop

The main program loop keeps going forever, or until somebody presses a keyboard key. The main body of the program is all within this loop.

The program 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.

        while (!xnOSWasKeyboardHit())
        {
         < main program body > 
        }

Updating the Data Available for Output

The following call to a 'Wait X Update All' method updates the data available for output. The WaitOneUpdateAll() method 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 then reads this data through the frame object.

                nRetVal = context.WaitOneUpdateAll(depth);

Getting the DepthGenerator's Frame Object

The DepthGenerator node's frame object contains the depth map as an array of pixels. The data is accessed though the Data() method (see later). This method returns a pointer to the first depth pixel in the map.

The following call to the GetMetaData() method gets the node's latest frame object, saving it as a metadata object. The frame object contains the depth map, which is an array of pixels. This call is central in the use of the DepthGenerator node – and in all generator nodes that make use of a metadata object.

                depth.GetMetaData(depthMD);

Getting the Depth Data and Configuration

The following print statement accesses the frame data, its ID, and some attributes of the frame data's associated configuration. The frame ID is the ID of the frame object. XRes() and YRes() are from the frame configuration; they are the resolution 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.

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

Releasing the Objects

At the conclusion of the program, the following code block unreferences the context and the production node, decreasing its reference count by 1. If the reference count reaches zero, the node or context will be destroyed.

            depth.Release();
            context.Release();
Generated on Wed May 16 2012 10:16:06 for OpenNI 1.5.4 by   doxygen 1.7.5.1