Touch Designer example: motion detection with webcams

By admin, 20 March, 2023

As often in coding there are several approaches to reach a similar goal, for instance by using different techniques and setting up components in a different way. This article shows two different approaches for detecting motion with a single webcam.

This article focusses on the caption of motion data with basic webcams. This article and example combines 2 basic tutorials on this subject, a first one based on the comparison of a delayed webcam regsitration and a second one that uses a component called Optical Flow component.

Both the examples start from data fed into the network with a webcam. We use a Videodevice in Top for that. The Video Device In TOP can be used to capture video from an external camera, capture card, capture dongle, IP camera, or video decoder connected to the system.

 

Example 1: Easy movement detection

The first example start with a Texture 3D TOP that will store an amount of frames in the work memory (cache) of the application to compare with to one another. Time Machine warps images only in time. The comparison of the resulting images is done by a composite that is set to "Add".  This way the overlapping pixels from the two images are showing color, the rest stays black. The rest of the network is comparable with the analyze section of the next example.

Easy movement detection is the link to the (extreem fast) youtube tutorial.

 

Example 2: Directional motion detection using a webcam

Using the Optical Flow and some CHOP processing we can get a relatively clean signal of the general direction of movement (Left/Right/Down/Up), for very cheap in all regards. We can then use these signals to trigger things based on the general direction of motion in the input, or control motion based on these directions.

Click here for more info

 

The optical flow component splits motion detection up in two color channels, the red one for the horizontal movement, the green channel for the vertical movement. To get horiwontal or vertical motion data the structure of the networks is nearly the same for both kinds of data. 

The horizontal movement (based on the red channel) is the part on the lower side of the screenshot above. With a treshold top we can split the data from the horizontal data from left to right and vice versa. This image data is analyzed and reduced to one pixel that changes in intensity. We used a Topto chop to transform the value of intensity into numbers (a number above zero, for the movement from left to right and vice versa). The rest of the tops are mainly to rename the values and filter the amount of data that is set at the beginning of the network with the video device in

We can repeate this principle for the vertical movement (based on the green channel). 

 

Download the example:

Click here to download the example.

A short description of the tutorial: this tutrial shows two basic techniques, based on the image of a single webcam to get motion data inside Touchdesigner. As an extent of this example, try to do the same with two  webcams - or use the movement data to generate and change parameters of a sound wavelength.