Monday, 6 February 2012

Webcam mouse interaction in X11

After taking a class in Computer Vision not so long ago, I often find myself straining to think of a cool project that takes advantage of what I have learned.  Finally, after a solid week of banging my head against other schoolwork, I decided I should take a break and code up something fun.  In other news, I have no idea what fun is.

My initial goal was to take input from a webcam, track the users finger, and use it to move the mouse cursor.  Pretty ambitious!   Although the finger is still a long ways off, I have it tracking red rings.  Code available here.

Notably, this software was writtin in C, and relies on the X Windowing system (ie you need to be running linux) as well as the OpenCV library.

to compile:

gcc webcam.c -o webcam.out `pkg-config --cflags opencv` `pkg-config --libs opencv` -lX11

and to run:

sudo ./webcam.out

Under my current configuration, I had to use sudo to access my webcam - I suspect I just dont have the proper permissions on my regular account.

So, how the code essentially works:
  • First, it translates the RGB webcam images into HSV-space
  • Then, it does some thresholding in HSV-space to select only specific hues of Red, IE, to match the color of my test ring.
  • After this, it uses the Hough transform to find all hollow circles in the image, within a specified range of radii.
  • As it should only find one object in the image that matches these parameters (hue,saturation,value,radius), it takes the x,y coordinates provided by the Hough transform, and uses them to move the mouse pointer.

Unless you have a red ring with an approximate 1cm radius, you will not benefit from this code - video to come soon!

Mad Propzz to Ahmad Byagowi for his diligent help -   With his OpenCV expertise, we were able to get this running in under 2 hours!


  1. Please fix the link to download the source code. thanks

  2. Download link is not ok. Please fix it again.