Saturday, 28 February 2009

Python Color Tracking

Color tracking is simple in teory, but hard to do in practice.
It's all about finding the pixels that fit's the properties you are looking for.
Using OpenCV and Python, makes the process alot easier:
You can download OpenCV for Python here:
It's OpenSource as you might guessed.

Connecting to your camera is as simple as:

# Create a Window
cvNamedWindow("MyWindow", 1)
# Connect to the camera
capture = cvCreateCameraCapture(0)
while 1:
# Grab the current frame from the camera
frame = cvQueryFrame(capture)
# Show the current image in MyWindow
cvShowImage("MyWindow", frame)
# Wait a bit and check any keys has been pressed
key = cvWaitKey(10)

To do some simple color-tracking, create a mask with all the pixels withing a given range of colors:

# Create a 8-bit 1-channel image with same size as the frame
color_mask = cvCreateImage(cvGetSize(frame), 8, 1)

# Specify the minimum / maximum colors to look for:
min_color = (180, 20, 200)
max_color = (255, 255, 255)

# Find the pixels within the color-range, and put the output in the color_mask
cvInRangeS(frame, cvScalar(*min_color), cvScalar(*max_color), color_mask)

OpenCV works in BGR color-space. HSV or Lab color-space might be a better choice

To convert the frame into HSV use:
cvCvtColor(frame, frame, CV_BGR2HSV)

Just remember to set the color-space back to BGR before displaying the image:
cvCvtColor(frame, frame, CV_HSV2BGR)

Next, split the mask into seperated - connected parts, by finding the contour:

storage = cvCreateMemStorage(0)
c_count, contours = cvFindContours (color_mask, storage, d=CV_CHAIN_APPROX_NONE)
# Go trough each contour
for contour in contours.hrange():
# Do some filtering

From here it's just a matter of filtering out the contours you don't want. Some might be too large or too small, or maybe you only want convex shapes:

# Get the size of the contour
size = abs(cvContourArea(contour))

# Is convex
is_convex = cvCheckContourConvexity(contour)

When you've got the contours you want, find the center-coordinates:

# Find the bounding-box of the contour
bbox = cvBoundingRect( contour, 0 )

# Calculate the x and y coordinate of center
x, y = bbox.x+bbox.width*0.5, bbox.y+bbox.height*0.5

Now that's teory.. In the real-world, you got lightning, shadows, noise, and so on. But i hope you got the basic idea how to do color-tracking by now.
For a more complex tracker, you'll might wan't to look at Motion-Segmentation, Background statistics, Dilate, Erode, FloodFill, PolyApprox...
It's all in OpenCV.

Here's a demo video of the color-tracker in action. I'm using Qt as GUI, and a Pickle-Socket that sends the computed data to Maya in form of a dictionary.
I use a Maya-Plane that represent my screen in 3D-space, so that it's easy to adjust the relationship between screen-space and 3d-space.
The Maya-plane can have any number of reference-objects that are constrained to the 3d-model. The tracked objects will automaticly constrain to a reference object, when it get's close enough.

Wednesday, 4 June 2008

HP Video

A short video i have created as part of a school assignment.
The assignment was to create a video like the Hewlett Package commercials .
There's a lot of errors i know :), I haven't had much time to make it, about 3-4 short days.
It's created in Maya using a lot of different deformers and composited in Fusion.

Monday, 2 June 2008

Animation Pipeline

I'll be working on a new pipeline for 3D-Animation this month as part of a school-project.
Follow the development here:

Sunday, 1 June 2008

Maya Wiimote

This video shows me controlling a cube in Maya with candle-light using a wii-remote.
It's created by using Python and pyBluez to connect to the wii-remote with bluetooth and bind it to Maya.
Each Wiimote has an infrared-camera in it, and since candle-light emits infrared light, it's possible to track the motion of the candles.
This could be extended to create a simple and cheep motion-capture device.

Tuesday, 17 April 2007

Hotkey Manager

This is one of my farvorit scripts right now.
It's a UI that let's me create hotkeys, menus and triggers very quickly..
I haven't completely finished it yet, put my plan is to use this UI to setup Maya to work more intutive..
For example, having hotkeys and menus change depending on what i'm doing at the moment..
The idea is to create a faster workflow by letting Maya guess what I might wan't to do now, depending on what i did before, and then change my hotkey/menu setup.
It also let me setup Triggers very quickly, which are commands that automaticly run when 'things happens'.. For example, to have a script that runs each time a new transform node is created or when a connection is made...

Monday, 2 April 2007

Camera Navigator

Uses a Photoshop-like control to pan/zoom the 3d-view.
The camera never moves or changes perspective, it's just like zooming/tracking on a 2d picture.
" "

Wednesday, 28 March 2007

History Tweaker

The changes will be added to the object Construction-History.
This might also be useful for when creating blendshapes.
Watch the Video!
" "