Showing posts with label opencv. Show all posts
Showing posts with label opencv. Show all posts

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.


Wednesday, 16 February 2011

Optical Flow and Rotoscopy

Been feeling pretty ill the last few days. Managed to drag myself to the Broadcast Video Expo yesterday at Earls Court. Would love to go again tomorrow to watch some of Arri's lighting seminars, and the DaVinci Resolve colour grading workshops, but probably won't be feeling good enough yet so it'll be another day at home storyboarding and tidying up odds and ends.



Last week I rotoscoped over the intro to 'Down by Law' with one long continuous virtual line using blender. At college Felipe suggested I could do a bit more with the data and maybe find some way of using the video to distort the drawings. I wrote a python script using the OpenCV bindings for Python 2.7 to analyse the optical flow of the original film footage. I based the script on the lkdemo using CalcOpticalFlowPyrLK. The script dumped out a load of text files containing the feature tracking info.

In blender I wrote a second script which warped my 'drawing' (made up of vertices and edges) on a frame by frame basis using the nearest tracked feature point to drag the line around. Effectively the script puts the line's control points (vertices) into voronoi cells which are shifted by the tracked features (like a virtual earthquake). I added in very simple outlier detection checking consistency with the flow of neighbouring points along the rotoscoped line. I had wanted to convert the point cloud of feature track points to a mesh using delaunay triangulation, and then use the animated mesh to deform the string of vertices, but it would probably have been excessively slow in python!

Anyways thanks to Felipe for pushing me that bit further. Meanwhile progress on the scribbler continues - I've added in three more drawing styles (two of them shown below) and a depth of field control. Next step is to work on edge detection (I've got down on paperware how it should work) and adding a bit of stability to make sure no essential parts of an image get left undrawn on any one frame.