Parallel Capturing and Processing

This topic explains how to run image acquisition and 3d data computation independently when using a single camera.

Please read the topics Opening a Camera and Grabbing 3D Data first.

Note

Rendering commands like RenderPointMap and RenderView use a single OpenGL context and therefore will be internally serialized and run sequentially, also when executed from separate Execute nodes. As these commands also have a single, global output node it is recommended to run at most one instance of each at a time.

Code Examples

Single Threaded

// Call the Open command to open the camera
NxLibCommand open(cmdOpen);
open.parameters()[itmCameras] = "1234";
open.execute();

// Create persistent command objects with two new, separate execution slots "capture" and "compute" under /Execute
// Create a new execution node "capture" under /Execute, to asynchronously run the cmdCapture command.
NxLibCommand capture(cmdCapture, "capture");

// Create a new execution node "compute" under /Execute, to asynchronously run the
// cmdComputeDisparityMap command.
NxLibCommand computeDisparityMap(cmdComputeDisparityMap, "compute");

// Note: the two command objects capture and computeDisparityMap now created two separate execution nodes
// /Execute/capture and /Execute/compute. Each of these nodes has its own Command, Parameters and Result subnodes
// and can be used to execute any command independently from commands any other execution slot.

// Note: when creating new execution slots their parameters will not be cleared after command execution and can be
// reused for the next execution in this slot
capture.parameters()[itmTimeout] = 200; // Set a capture timeout of 200ms

int numImagesCaptured = 0;
int numDisparityMapsComputed = 0;

// start the first capture command before entering the loop
capture.execute(false);
while (numImagesCaptured < 50) { // run loop until 50 images have been captured
	if (capture.finished()) {    // check if the capture operation is already done
		if (capture.successful()) numImagesCaptured++;
		capture.execute(false); // start the next image acquisition
	}
	if ((numImagesCaptured > 0) &&
	    computeDisparityMap
	        .finished()) { // check if we already have an image and the last disparity map computation is finished
		numDisparityMapsComputed++;
		computeDisparityMap.execute(false); // start the next disparity map computation
	}
}
printf("Captured %d images and computed %d disparity maps.\n", numImagesCaptured, numDisparityMapsComputed);

Multi Threaded

Note

The two command objects capture and computeDisparityMap in Thread 1 and Thread 2 create two separate execution nodes /Execute/capture and /Execute/compute. Each of these nodes has its own Command, Parameters and Result subnodes and can be used to execute any command independently from commands any other execution node.

Preparation (e.g. in main thread):

// Call the Open command to open the camera
NxLibCommand open(cmdOpen);
open.parameters()[itmCameras] = "1234";
open.execute();

// capture one image synchronously so that the computation thread has an initial image to process
NxLibCommand(cmdCapture).execute();

// ... start thread 1 and thread 2 now

Thread 1 (capturing):

// Create command object working in a separate execution slot "capture" under /Execute
NxLibCommand capture(cmdCapture, "capture");

// Note: when creating new execution slots their parameters will not be cleared after command execution and can
// be reused for the next execution in this slot
capture.parameters()[itmTimeout] = 200; // Set a capture timeout of 200ms
int numImagesCaptured = 0;

while (numImagesCaptured < 50) { // run loop until 50 images have been captured
	capture.execute();
	numImagesCaptured++;
}

Thread 2 (disparity map computation):

// Create command object working in a separate execution slot "compute" under /Execute
NxLibCommand compute(cmdComputeDisparityMap, "compute");

int numDisparityMapsComputed = 0;

while (numDisparityMapsComputed < 50) { // run loop until 50 images have been processed
	compute.execute();
	numDisparityMapsComputed++;
}