Optical Flow

Back to Tutorials

5. Unit Process 7. User Tracking 1 2  3  4  5  6  7  8  9  10  11  12  13  14  15  16



The ViiM SDK provides a class that computes the image optical flow applied to the sensor images. The optical flow technique computes the pixel's displacement between image frames, allowing motion analysis. In this tutorial you'll learn how use the ViiM's method that computes the optical flow, how to use and display the flow vectors and how to change the process properties during run time. Using motion analysis in your programs is very easy, just register an optical flow unit. Let's see how to do this.

If you read the first tutorial on using the ViiM engine and the Unit Process tutorial about registering a unit, you should now have no trouble understanding the first lines of the following .cpp code (setup method): First of all, notice that we set notifications for the _engine and for the _opticalFlow processing unit with the same function to be called, but you could easily set different functions with different names.

We first enable smooth (see the smooth tutorial) and register the optical flow unit, in line 37. It's the registerUnitProcess that starts that unit's processing. Lines 40 and 41 call methods that give us the width and height of the process and line 45 just declares an offset, for drawing purposes. Line 51 declares a far threshold in the depth image, this way the program just "sees" up to 3 meters. In line 54 we tell _opticalFlow to use calibrated depth and color images.

As usual, the update phase is only used to update the image we want to draw, the depth image.

In the draw phase we get the optical flow vectors and draw then as a line. The following code does exactly that, using the offset to compute a grid where we are going to draw the vectors.

In lines 96 and 98 we use two methods to get the number of rows and columns of the computed optical flow grid. The methods that get the actual flow points are used in lines 102 and 103:

Each of these methods return a pointer to the array of floats that contains the optical flow values, for the X value, and for the Y value respectively. Alternatively, you could use one single method to get both x and y coordinates with

using it like

This method returns a pointer to the array of floats that contains the optical flow values, each point retrieved by indexation. The (x,y) components of the flow vectors are set in pairs in the array, thus the even indexes (including the zero index) retrieve the x component and the odd indexes the y component. In order to see the optical flow computation in different conditions we use the open framework's keyPressed method, which retrieves key pressed events. Inside the function we use a switch to change some optical flow parameters according to the pressed key. The code used is the following:

We chose to use the keys 'v' and 'V' to change the process size. The use is plain simple, just call setProcessSize with the size you want and the ViiM will adapt it for you in real time.

To change between smoothing the depth image or not just call useSmoothDepth with the true or false parameter respectively.

To calibrate the depth and color images the process is exactly the same. In lines 98 and 99 the threshold is moved away from or toward to the sensor respectively, using the setFarThreshold method. All the above methods set the parameters in real time, i.e. during processing, allowing you to build versatile applications.

If you press the u key the optical flow unit is removed, to save resources for example. This should give you an idea about the type of management one can easily do with the ViiM SDK. You can set the unit again pressing the r key.

Finally you can stop, start and restart the sensor. These methods were already explained in the Device Handler sample.

Check out this example in the folder apps, ViiM_OF_Samples and choose your OS.

5. Unit Process 7. User Tracking 1 2  3  4  5  6  7  8  9  10  11  12  13  14  15  16

Back to Tutorials