Snake Algorithm - opencv active contour - not working so well

21,140

Solution 1

A typical snake or active contour algorithm converges during a trade-off between 3 kind of cost functions: edge strength/distance (data term), spacing and smoothness (prior terms). Immediately, you may notice a connection to your "nose-problem" - the nose has high curvature. Your snake also have troubles getting into concave regions since this certainly increases its curvature compared to a convex hull.

SOLUTIONS:
A. Since your snake performance isn't better than one of a convex hull, as one of the remedies I would proceed with a simpler convex hull algorithm and then rerun it on its inverted residuals. It will get a nose right and then concavities will turn into convexities in the residuals. Or you can use convexity defect function of openCV instead of working with convexHull.

B. Another fix can be to reduce snake curvature parameter to allow it to curve around the nose sharply. Since you have little noise and you can actually clean it up a bit I see no problem of enforcing some constraints instead of making "softer" trade-offs. Perhaps a head silhouette prior model can help here too.

Below I tried to write my own snake algorithm using various distance transforms and weights of a distance parameter. The conclusion - the parameter matters more than distance metrics and does have some effect (a left picture uses smaller parameter than the right and thus cuts the nose more). The distance from contour (red) is shown with grey, snake is green.

enter image description here left

C. Since your background is almost solid color, invest a bit into cleaning some residual noise (use morphological operations or connected components) and just findContrours() of the clean silhouette. I implemented this last solution below: a first image has noise deleted and the second is just a contour function from openCV. enter image description here enter image description here

Solution 2

If you want to implement by yourself, I recommend the paper "Everything you always wanted to kwon about snakes (but were afraid to ask)", By Jim Ivins and John Porrill.

About the OpenCV implementation, I don't know it very much, but I would you suggest you to:

  • Reduce beta, so that the curvature may be stronger

  • Check the image energy. Maybe the last parameter of the function (scheme) is wrong. There are two possible values: _CV_SNAKE_IMAGE and _CV_SNAKE_GRAD. You set it to 0, if I'm not wrong, and I think 0 means _CV_SNAKE_IMAGE. So, the function will assume the input image is the energy image. Again, I'm not sure how OpenCV implements this function, but I think that when you use _CV_SNAKE_IMAGE the function assumes the input image is a gradient module image. In your case, it could make the snake avoid black regions (interpreted as low gradient module) and seek bright regions. So, try to use _CV_SNAKE_GRAD as your last parameter.

I hope it can help you. Good luck!

Share:
21,140
Marcassin
Author by

Marcassin

New technologies && cognitives sciences/IA student -> C/C++/Java PHP/SQL/html/JS/Python

Updated on November 15, 2020

Comments

  • Marcassin
    Marcassin over 3 years

    I'm actually working on a contour detection for head side. As pictures are taken in front of a white wall, I decided to run a snake (active contour model algorithm) on the picture processed with a threshold.

    Problem is the snake won't fit well around the nose, the mouth, and below the mouth (as you can see in these pictures below).

    //load file from disk and apply threshold
    IplImage* img = cvLoadImage (file.c_str (), 0);
    cvThreshold(img, img, 170, 255, CV_THRESH_BINARY);
    
    float alpha = 0.1; // Weight of continuity energy
    float beta = 0.5; // Weight of curvature energy
    float gamma = 0.4; // Weight of image energy
    
    CvSize size; // Size of neighborhood of every point used to search the minimumm have to be odd
    size.width = 5;
    size.height = 5;
    
    CvTermCriteria criteria;
    criteria.type = CV_TERMCRIT_ITER;  // terminate processing after X iteration
    criteria.max_iter = 10000; 
    criteria.epsilon = 0.1;
    
    // snake is an array of cpt=40 points, read from a file, set by hand
    cvSnakeImage(img, snake, cpt, &alpha, &beta, &gamma, CV_VALUE, size, criteria, 0);
    

    I tried to change the alpha/beta/gamma parameters or iterations number but I didn't find a better result than output show below. I cannot understand why the nose is cut, and face is not fit around the mouth. I have enough points i guess for the curvature, but there still be some lines composed with several (>2) points.

    Input Image : Input Image - threshold

    Output Snake :

    • blue : points set by hand

    • green : output snake

    OutputSnake

    Any help or ideas would be very appreciated. Thanks !