Active Contour (Snake) with OpenCV
A couple of weeks ago we had to develop a small computer vision program. The main task was to introduce ourself to OpenCV. The second goal was to implent the A* search algorithm which should search for an inital contour. In the last step the snake algorithm should track the objects contour while the object moves around.
First I calculate the gradient (see Sobel operator) of an input image. Below you can see the gray scaled gradient image of an golf ball. Darker areas mean there is a smooth color transition in the input image. Bright areas mean there is a hard color transion. For example from black to white.
Then the user manually sets 3 or more points that marks a basic contour. At next the A* search algorithm trys to find the contour of the object. In my implementation I have used the euclidien distance as “heuristic estimate” of the distance to the goal and the gradient information for the path-costs.
Here a small video capture of the complete workflow:
- manually set 4 markers
- wait a little bit … and then you can see 18 new markers. This is the initial contour found by the A* search algorithm.
- the ball moves around the the snake algorithm activly moves the markers of the contour

on May 13th, 2009 at 22:36
Hey Daniel, That looks pretty impressive…..
Any way I can grab the A* search code off you…. please
on November 22nd, 2009 at 15:02
I need your snake code is it possible to send to me your code? please help me
on November 22nd, 2009 at 17:17
Hi Belkis! This isn’t my snake code. I used the opencv library for this. Have a look on