Daniel’s Blog


Active Contour (Snake) with OpenCV

Posted in Academy Showcase | by Daniel on the August 6th, 2008

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:

  1. manually set 4 markers
  2. wait a little bit … and then you can see 18 new markers. This is the initial contour found by the A* search algorithm.
  3. the ball moves around the the snake algorithm activly moves the markers of the contour

3 Responses to 'Active Contour (Snake) with OpenCV'

Subscribe to comments with RSS or TrackBack to 'Active Contour (Snake) with OpenCV'.

  1. Sunil said,

    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 :-)

  2. belk?s uluçay said,

    on November 22nd, 2009 at 15:02

    I need your snake code is it possible to send to me your code? please help me :(

  3. Daniel said,

    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

Leave a Reply