Tuesday, 15 May 2012

Drawing in Space and Her Favourite Positions



'Drawing in Space' is an experiment I've wanted to try for a while: using OpenCV and a webcam to draw in 3D.  Essentially you wave a coloured square of card around in front of the webcam and my code converts the path your hand takes into a 3D Drawing.  First it filters the image based on a colour you have chosen, selecting all pixels within a colour range determined by the mean and standard deviation of the patch chosen in the calibration phase.  It then finds contours in the filtered image, simplifies these reducing the number of edges, checks if any contours are square (concave, under a certain contained area, and with all internal angles within a given tolerance).  From all the squares found it then chooses which is the most likely square to follow on from the last based on the sum of the square of the magnitude of the delta in each point's position between frames (trying different permutations of the order of corners to account for rotations and mirroring).  If it can't find the square it motion tracks the corners from the last frame.  If any of these are missing it interpolates the data, and if all of them are missing it tracks strong features in the last image (in full colour, unfiltered) and uses a simple nearest neighbour algorithm to associate each corner of the last square with a nearby tracking point (if it was clever it would use some kind of error detection here to eliminate anomalies and then a delaunay triangularisation to interpolate points, but it needs to be lean to run at all).  If all of this fails, the script uses a third order differential on the last few points to extrapolate the next position, filling in the gaps for a few frames until data resumes.   The script also adjusts the colour it is searching for frame by frame to account for changes in light conditions.  Optionally you can also use the chessboard tracking instead of a coloured square.

The drawings are displayed in anaglyph as you draw, and can be exported as OBJ files.  I need some options to adjust the convergence point and so on, but its mostly there now.  A beta version of the code is online here (right click, save target as).  You need matching python 2.7, numpy 1.6.1, and opencv-python 2.4.0 compiled against each other.  If you're running Windows you can get numpy and opencv packages here.




'Her Favourite Positions' is an animation of 186 Portraits and 37 Full Body Poses from my Life Drawing classes at the RCA.  I matched them up frame by frame in AfterFX, and did a little warp correction here and there to get the proportions generally accurate.  I'm quite amazed that this even worked at all and that you can see a ghostly generic photofit face through the mess of lines.