DriveWorks SDK Reference
5.8.83 Release
For Test and Development only

Single Camera Template Tracking

The tutorial shows the general structure of a program that uses the 2D template tracker to track templates in a single camera.

Initialize template array on both CPU and GPU

contextHandle is assumed to be previously initialized dwContextHandle_t.

dwTemplateArray_create(&templateCPU, maxFeatureCount,
DW_FEATURE2D_MEMORY_TYPE_CPU, contextHandle);
dwTemplateArray_create(&templateGPU, maxFeatureCount,
DW_FEATURE2D_MEMORY_TYPE_CUDA, contextHandle);
DW_API_PUBLIC dwStatus dwTemplateArray_create(dwTemplateArray *templateArray, const uint32_t maxTemplateCount, const dwMemoryType memoryType, dwContextHandle_t context)
Creates and initializes a template array.

Initialize template tracker parameters with default values

DW_API_PUBLIC dwStatus dwTemplateTracker_initDefaultParams(dwTemplateTrackerParameters *params)
Initializes TemplateTracker parameters with default values.
Configuration parameters for a dwTemplateTrackerIA.

Modify parameters according to tracking requirements

imageProps is the dwImageProperties structure of image to be tracked

dwImage_getPixelType(&params.pxlType, imageProps.format);
DW_API_PUBLIC dwStatus dwImage_getPixelType(dwTrivialDataType *const type, dwImageFormat const format)
Retrieves dwTrivialDataType associated with a specific format.
float32_t validWidth
Maximum valid template width, any templates with bbox.width > validWidth will be killed after trackin...
float32_t validHeight
Maximum valid template height, any templates with bbox.height > validHeight will be killed after trac...
#define DW_MAX_TEMPLATE2D_SIZE
defines the maximum template size

Select on which processor the tracker should be performed on. Allowed option is DW_PROCESSOR_TYPE_GPU.

params.imageWidth = 1280;
params.imageHeight = 800;
@ DW_PROCESSOR_TYPE_GPU
Definition: Types.h:170
uint32_t imageHeight
Height of the images that the tracker runs on.
uint32_t imageWidth
Width of the images that the tracker runs on.
dwProcessorType processorType
Processor type which determines on which processor the algorithm should be executed on.

Initialize template tracker with given parameters and bind output

dwTemplateTracker_initialize(&tracker, &params, 0, contextHandle);
DW_API_PUBLIC dwStatus dwTemplateTracker_initialize(dwTemplateTrackerHandle_t *obj, const dwTemplateTrackerParameters *params, cudaStream_t stream, dwContextHandle_t context)
Initialize the TemplateTracker module.

Create 2 image pyramids as detection/tracking input: one is current, one is previous, the 2 pyramids work as a double buffer (or ping-pong buffer). They swap each other after each detection/tracking cycle. So new frame will always be read into pyramidCurrent and the previous frame will be stored to pyramidPrevious.

dwPyramid_create only creates pyramid image and allocates memory, pyramid will be filled in dwImageFilter_computePyramid. Top level (level 0) in pyramid is always the same size as original input image.

inputImageProps.width, inputImageProps.height are the same in tracker/detector initialization

imagePxlType should be got from dwImage_getPixelType(inputImageProps.format).

contextHandle is assumed to be previously initialized dwContextHandle_t.

dwPyramid_create(&pyramidPrevious, levelCount,
inputImageProps.width, inputImageProps.height, pxlType, context);
dwPyramid_create(&pyramidCurrent, levelCount,
inputImageProps.width, inputImageProps.height, pxlType,context);
DW_API_PUBLIC dwStatus dwPyramid_create(dwPyramidImage *pyramid, uint32_t levelCount, uint32_t width, uint32_t height, dwTrivialDataType pxlType, dwContextHandle_t context)
Creates and initializes an image pyramid.

Add bounding boxes to be tracked to the tracker. BBox can be extracted from the DNN detector, or defined by the user.

// get bbox from DNN detector or user defining
uint32_t nNewBoxes = ...;
dwBox2Df* newBoxes = ...;
*featureData.featureCount = nNewBoxes;
for (uint32_t i = 0; i < nNewBoxes; i++) {
featureData.locations[i] = {newBoxes[i].x + newBoxes[i].width/2, newBoxes[i].y + newBoxes[i].height/2};
featureData.sizes[i] = {newBoxes[i].width, newBoxes[i].height}
featureData.statuses[i] = DW_FEATURE_STATUS_DETECTED;
}
// copy data to GPU
dwTemplateArray_copy(&templateGPU, &templateCPU, 0);
float32_t x
Specifies the x coordinate.
Definition: Types.h:226
float32_t height
Rectangle height.
Definition: Types.h:232
float32_t y
Specifies the y coordinate.
Definition: Types.h:228
float32_t width
Rectangle width.
Definition: Types.h:230
Defines a rectangle with floating point numbers.
Definition: Types.h:224

Start tracking the loop

while(true)
{
// swap current/previous pyramid
// so new frame will always be built in pyramidCurrent
// and old pyramid will be stored in pyramidPrevious
std::swap(pyramidCurrent, pyramidPrevious);
// CODE: Read image frame code
dwImageHandle_t image = ...;
// compute pyramid based on the new read frame image
dwImageFilter_computePyramid(&pyramidCurrent, image, 0, contextHandle);
dwTemplateTracker_trackPyramid(&templateGPU, &pyramidCurrent,
&pyramidPrevious, tracker);
// copy detected results to CPU
dwTemplateArray_copy(&templateCPU, &templateGPU, 0);
// break the loop when all features are killed
if (*featureData.featureCount == 0)
break;
// do some stuff in CPU
featureData.locations[...];
featureData.sizes[...];
}
struct dwImageObject * dwImageHandle_t
Definition: Image.h:110
DW_API_PUBLIC dwStatus dwImageFilter_computePyramid(dwPyramidImage *pyramid, const dwImageCUDA *image, cudaStream_t stream, dwContextHandle_t context)
Builds the pyramid from level 0 image.
DW_API_PUBLIC dwStatus dwTemplateTracker_trackPyramid(dwTemplateArray *templateArray, const dwPyramidImage *currentPyramid, const dwPyramidImage *previousPyramid, dwTemplateTrackerHandle_t obj)
Track the templates in currentPyramid .

Finally, free previously allocated memory.

dwPyramid_destroy(pyramidPrevious);
dwPyramid_destroy(pyramidCurrent);
DW_API_PUBLIC dwStatus dwPyramid_destroy(dwPyramidImage pyramid)
Destroy pyramid images.
DW_API_PUBLIC dwStatus dwTemplateArray_destroy(dwTemplateArray templateArray)
Destroys the template array and frees any memory created by dwTemplateArray_create().
DW_API_PUBLIC dwStatus dwTemplateTracker_release(dwTemplateTrackerHandle_t obj)
Releases the TemplateTracker module.

For the full implementation, refer to Template Tracker Sample.