Interactive Projections
Tracking the position of people or objects can be useful when creating interactive projection mapping projects. In TouchDesigner there are various methods to achieve this; This tutorial will introduce you to some common techniques to determine the position of an object. Let's dive in!
To begin, open the VideoDeviceIn TOP and select your DEVICE in the parameters window - you can read more about operators and the parameters window here. Connect this operator to a Null TOP and activate the Display option by clicking on the blue dot in your node, this will allow you to easily monitor any alterations to your original source.
Object Isolation
The first method focuses on isolating your object by combining various TOPs to eliminate its background. It's important to note that this process begins outside of TouchDesigner, during scene setup in real life; Ensure your background has a solid, contrasting colour to your object.
In Touchdesigner, right-click on the connection line to INSERT a node to your network, here select the Level TOP. This operator is useful to adjust the brightness and contrast of your image. Use the sliders available to create a strong contrast between your object and background. Next add a Threshold TOP and change the COMPARATOR to Greater, then alter the THRESHOLD value to adjust the intensity of this effect. At this point your object should be highlighted in white, while the rest of your image should be transparent.
Tracking
Now that we have isolated our object from it's background we want to look for the white pixels in the image, to determine it's location. To do that we first have to size down our input's resolution, this way we will have less data to analyse while keeping readings accurate. In your network, just before the Threshold TOP, add a Resolution TOP and in the sub-category "Common" select your OUPUT RESOLUTION to be Eighth.
Afterwards add a TopTo CHOP to your threshold and in the sub-category "Crop" select your CROP to be Full Image. This converts the pixels in our image into channels, containing RGBA informations.
Our aim is to determine the position of the coloured pixels, to do so we'll be turning our channels into a table. To your TopTo CHOP node add a ChopTo DAT, this will turn the information retained in our channels into a series of 0s, where the pixels are transparent, and 1s, where the pixels are coloured. You can check your process by covering your camera with your hand, now the table in ChopTo DAT should be showing mostly 1s.
Finally, we want to convert these 1s into X and Y co-ordinates within the image, which acts as our cartesian plane. Add a Constant CHOP and create two channels, let's name these X and Y.
Finally add an Execute DAT, this should not be connected to your network. Right-click on the node to and select "Edit Contents in Textport", delete the current Python code and paste this in:
# Defining begining of execution
def onFrameStart(frame):
if me.time.frame % 5 == 0:
# Referencing operators in our network
source = op('chopto1')
output = op('output'constant1')
# Initializing empty lists for rows and columns
rows = []
cols = []
# Looping through the rows and columns of the 'chopto1' operator
for x in range(source.numRows):
for y in range(source.numCols):
# Checking if the value at position (x, y) in 'chopto1' is equal to 1
if source[x, y] == 1:
# If true, add the row and column indices to the respective lists
rows.append(x)
cols.append(y)
# Calculating the center of the selected points
centerx = (max(rows) - ((max(rows) - min(rows)) / 2)) / source.numRows
centery = (max(cols) - ((max(cols) - min(cols)) / 2)) / source.numCols
# Outputting new values to 'constant' operator
output.par.value0 = centerx - 0.5
output.par.value1 = centery - 0.5
return
In the parameters window of this node, turn on the Frame Start so our python code is executed at the start of every frame. Your object's x and y co-ordinates are now in the Constant CHOP.
You can now link these values to a TOP shape and composite with your original webcam input to view the tracking magic, here more about this process here.
Colour Tracking
Blob Track
AI
For this we'll be looking into x and y co-ordinates.
While our source of input for these different methods will always be a camera, the same workflow will help you work around any sensor using x and y co-ordinates to determine position.