File Exchange

## Snake : Active Contour

version 1.7.0.0 (40.9 KB) by Dirk-Jan Kroon

### Dirk-Jan Kroon (view profile)

Snake Segmentation (Kass et al), 2D/3D including GVF and balloon force (Easy to read code)

Updated 08 Nov 2011

These functions implements the basic snake segmentation contour, as introduced by Kass et al.

Algorithm:
A snake is an active (moving) contour, in which the points are attracted by edges and other image boundaries. To keep the contour smooth, a membrane and thin plate energy is used as contour regularization.

Implementation:
All code is well commented, and is probably easy readable. It includes,
- A balloon force, which is not in the original paper
- Binary image segmentation result,
- The "Gradient Vector Flow" (GVF) method.
- 3D implementation, including parameter to keep the mesh from self-intersecting

Try the example in the help of function Snake.m !

Literature:
- Michael Kass, Andrew Witkin and Demetri TerzoPoulos "Snakes Active Contour Models", 1987
- Jim Invins and John Porril, "Everything you always wanted to know about snakes (but were afraid to ask)
- Chenyang Xu and Jerry L. Prince, "Gradient Vector Flow: A New external force for Snakes
- Christoph Lurig, Leif Kobbelt, Thomas Ertl, "Hierachical solutions for the Deformable Surface Problem in Visualization"

### Cite As

Dirk-Jan Kroon (2020). Snake : Active Contour (https://www.mathworks.com/matlabcentral/fileexchange/28149-snake-active-contour), MATLAB Central File Exchange. Retrieved .

Maxime Nore

kusmae16

### kusmae16 (view profile)

Gurjot Singh Sandhu

### Gurjot Singh Sandhu (view profile)

Gurjot Singh Sandhu

### Gurjot Singh Sandhu (view profile)

I am trying to use the algorithm to extract the instantaenous frequency of audio signals. The snake always connects to a closed structure, for example a loop. Any idea what I could do, to get a non closed curve?

creation games

### creation games (view profile)

Guilherme Correia

VeeEee Tech

VeeEee Tech

hanwen kang

### hanwen kang (view profile)

nice，can directly be used, thx! good work.

BG

### BG (view profile)

To obtain a separate kernel in two dimensions, consider using an SVD of the filter taps. The scaled outer product for the top one is best in a least squares sense. For higher dimensions, it is possible to consider a higher order SVD with tensors.

xinlanweimei

Wael Emish

### Wael Emish (view profile)

i have problem in result......the efficiency of segmented image is very low...can you help me??

snehal jaipurkar

### snehal jaipurkar (view profile)

Sir how to save the segmented image after applying snake model in Matlab??

song yongbin

J de Ruijter

anggi nego

### anggi nego (view profile)

corentin tisserand

### corentin tisserand (view profile)

Hi, I'm trying the example in Snake2D.
When I launch my programm, there is an error which is : 'undefined variable or function J'. Indeed, when I look at the script:
Irgb(:,:,1)=I;
Irgb(:,:,2)=I;
Irgb(:,:,3)=J;
figure, imshow(Irgb,[]);
hold on; plot([O(:,2);O(1,2)],[O(:,1);O(1,1)]);
[O,J]=Snake2D(I,P,Options);
the point That Irgb(:,:,3)=J; concerns me, since J is an output of Snake2D, so how could to define it?

Sreenath Balakrishnan

Turgut S.

dinial utami

Chris Volpe

### Chris Volpe (view profile)

Hi Dirk-Jan,

Thanks for sharing this. I'm trying to get a better understanding of this so that I can modify it to suit a specific application. While examining SnakeMoveIteration2D.m, I see the following:

ssx = gamma*P(:,1) + Fext1(:,1) + Fext2(:,1);
ssy = gamma*P(:,2) + Fext1(:,2) + Fext2(:,2);

Gamma is the time step. I can't understand why you would multiply the POINTS THEMSELVES by the time step. A Gamma of anything other than unity would seem to produce non-sensible results. I would have expected something more along the lines of:

ssx = P(:,1) + gamma*(Fext1(:,1) + Fext2(:,1));
ssy = P(:,2) + gamma*(Fext1(:,2) + Fext2(:,2));

The reason I'm looking into this is that I'm trying to provide an additional energy function that exhibits a preference for straight lines and right-angle bends in the resulting snake.

chong yang

Marlon

shivangi b

### shivangi b (view profile)

please tell is getsnake command in MATLAB2010Ra ?
getsnake command is not avaliable in MATLAB2015Ra.please tell is any other command for getsnake in MATLAB2015Ra ?? my id is shivangisbv@gmail.com

Marlon

### Marlon (view profile)

vasanthselvakumar R

### vasanthselvakumar R (view profile)

i need to segment the specific area of pixel in an image.i prefer the seeded region growing method please explain the idea about the code srg

li li

### li li (view profile)

this is very useful for me .thank you.

Zizhao Zhang

keerthi v

kavindra jain

### kavindra jain (view profile)

after the energy optimized output red line area how can i extract my selected image automatically

Yousef Mazaheri

### Yousef Mazaheri (view profile)

Dirk-Jan,

If I want to determine register (rather than segment) can I simply take the Eext values from ExternalForceImage2D and insert them into the bspline_transform (from your b-splines program)?

Thanks

Meghana Dinesh

### Meghana Dinesh (view profile)

I tried the code (22 lines, as it is, without editing) mentioned under "Example, Basic:" in "Snake2D.m". This is the error I am getting:

To use 'interp', you might need:
interp - Control System Toolbox
interp - Signal Processing Toolbox
interp - System Identification Toolbox

Error in InterpolateContourPoints2D (line 32)
O(:,1)=interp([P(end-3:end,1);P(:,1);P(:,1);P(1:4,1)],10);

Error in Snake2D (line 127)
P=InterpolateContourPoints2D(P,Options.nPoints);

Your File Information says only Image Processing Toolbox is required to use your submitted code.

Is there anything I am doing wrong? I just want to detect contour of an example image I have in:
http://stackoverflow.com/questions/27418314/extract-co-ordinates-from-detected-contour

Regards.

Ishaan Markale

### Ishaan Markale (view profile)

Hi,

I am trying to use the snake2D file on the testimage provided. However, my MATLAB is crashing when I use the test code.
Can anyone provide a reason as to why this is happening and any poosible solutions.
Thanks!

Jr-Shin Chen

### Jr-Shin Chen (view profile)

"Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)" is because you are calling the function in the snake2D.m or snake3D.m itself. It will cause endless loop.
So, try to copy the example codes and paste to Matlab command window. Then you'll get the results!!

Christian

### Christian (view profile)

In SnakeMoveIteration3D.m I had to change the last line
FV.vertices=V;
to
FV.vertices=real(V);
to avoid an errror.

I'm using isosurface to to mesh a hemisphere and then reduce the number of vertices with reducepatch.m

rain6304

### rain6304 (view profile)

How do I implement the function in MATLAB? I am just beginning to use MATLAB and need some help! Thanks!

Christian

### Christian (view profile)

Great to learn how the snake works and how the parameter influence the result! Thank you very much!

Chris

### Chris (view profile)

Hi, I am trying to run Snake3D but keep getting the following error:
"Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your available stack space can

Error in Snake3D"

Does anyone know how to fix this?

IDRIS AHMED

Y Simson

### Y Simson (view profile)

Signal Processing ToolBox is required for the interp() function in K=InterpolateContourPoints2D(P,nPoints)

32sthide

### 32sthide (view profile)

I don't understand how you calculate the internal force in 3d model. And I can't find a reference that supports your calculation. Can you help me?

narjes

### narjes (view profile)

Hi, i try to use this code but i have a problem with "interp1 "

# ??? In an assignment A(:,matrix) = B, the number of elements in the subscript of A and the number
of columns in B must be the same.

Error in ==> C:\MATLAB6p5\work\InterpolateContourPoints2D.m
On line 40 ==> K(:,1) = interp1(dis,O(:,1),linspace(0,dis(end),nPoints*2));#

can you help me please !!!!!!

Ahmed

### Ahmed (view profile)

i am try to work on active contour, but i fail although i tried different models of snake in MATLAB such as:
1-snake2D().m
2-snake().m
3-ac_segmentation().m
4-snakedeform().m

but unfortunately neither one of these methods is worked with me

please i want someone who have good experience in using it to help me how to use it.
Note:i am working on segmentation of medical H&E stains images and i need strongly to use snake to segment the nuclei(foreground) from background.

Ahmed

### Ahmed (view profile)

i am try to work on active contour, but i fail although i tried different models of snake in MATLAB such as:
1-snake2D().m
2-snake().m
3-ac_segmentation().m
4-snakedeform().m

but unfortunately neither one of these methods is worked with me

please i want someone who have good experience in using it to help me how to use it.
Note:i am working on segmentation of medical H&E stains images and i need strongly to use snake to segment the nuclei(foreground) from background.

Teresa

### Teresa (view profile)

Hi,
I also have a problem with the interp error and don't know how to get rid of it. I tried interp2, interp, interpn. I think interp2 works but then I get an error in
K(:,1) = interp2(dis,O(:,1),linspace(0,dis(end),nPoints*2));
K(:,2) = interp2(dis,O(:,2),linspace(0,dis(end),nPoints*2));

Does anyone find a solution and can help me?

Ahmed

Inge Schie

Agnes

### Agnes (view profile)

Hi,

For the issue, perhaps you should try with

O(:,1)=interp([P(end-3:end,1);P(:,1);P(1:4,1)]',10)';
O(:,2)=interp([P(end-3:end,1);P(:,2);P(1:4,2)]',10)';
O=O(41:end-39,:);

Because I don't have any issue with it.
Thanks for the great work.

Felix

### Felix (view profile)

Does somebody figure out how to get rid of the interp error?
I tried interpn and interp2 but was not able to get the code running.
Can somebody help me?

Error is in
InterpolateContourPoints2D

O(:,1)=interp2([P(end-3:end,1);P(:,1);P(:,1);P(1:4,1)],10);
O(:,2)=interp2([P(end-3:end,2);P(:,2);P(:,2);P(1:4,2)],10);

I tried to run the basic Example of the Snake2D but encountered this Problem with interp function

Dmitry Sorokin

### Dmitry Sorokin (view profile)

Hi.

Found a bug in InterpolateContourPoints2D possibly caused by using 'interp' function.

I think incorrect work of this basis function might lead to wrong functionality of the whole Shakes algorithm.

I can send you the example via email.

nebi

### nebi (view profile)

Hi. what is the (P, Options) parameters for snake input. could you explain those. thanks

Lacey

### Lacey (view profile)

Great work, and thanks for commenting so well! I'm looking to use your code, or something like it, to extract a face from the background of an image. Is there a way to use your code to train a model using a small number of training images, so the model can then be used on other images? I'd like to not have to manually click the points on the image every time. Any help would be appreciated! Thanks!

Hi,

I'm a MS student doing study of segmentation with paramétric snake, i search the information of Multi-Target Parametric Active Contours
Thank you very much.

Joy King

### Joy King (view profile)

It's excellent. Thank you very much.

Qasim ul-haque

### Qasim ul-haque (view profile)

??? Error: File: Snake2D.m Line: 1 Column: 24
Unexpected MATLAB expression.

??? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your available stack space can

Error in ==> imformats>find_in_registry

i am getting this error.Help?

Walid

### Walid (view profile)

the code does not find interp

arron lacey

### arron lacey (view profile)

Can the GVF field be altered to create a magnetic field?

subrajeet

KYAW KYAW

### KYAW KYAW (view profile)

Dear Dirk,

I am newbie in ultrasound imaging even i had few experiences in image processing and analysis other programming languages.

Currently, I am looking for a boundary detection of noisy ultrasound images but I couldn't manage to find the right one until now.I tried out using your code but the problem is still there and please advise me.

Thanks and best regards

Jack

### Jack (view profile)

Hi,
Thank you very much for your great job! It is very usefull for my master thesis, but I would like to ask some questions about your toolbox.

1) If I try your example in Snake3D, the 3D Model (SphereMesh) is growing up to the out of boundary. How could it be corrected?

2) Should this 3D Model always a increasing deformable model or could it be shrinking model too?

3)Is this parameter "Delta" for baloon force always necesarry for GVF?

4)I couldnt find the parameter "Kappa" in the papers in the Literature. Could you please put the paper which contains an equation with "Kappa"?

Dirk-Jan Kroon

### Dirk-Jan Kroon (view profile)

*Dan, the snake.m file is renamed to Snake2D.m in this version.

Dan

### Dan (view profile)

I agree with Terrence.
Where is the snake.m file?

Marcos Belmonte

### Marcos Belmonte (view profile)

Thanks very much, I'll try it

Dirk-Jan Kroon

### Dirk-Jan Kroon (view profile)

*Marcos Belmonte
The Triangulated Patch FV, can be constructed with the "isosurface function" on a (logical) 3D volume of a rough segmentation of your object.

Marcos Belmonte

### Marcos Belmonte (view profile)

Hi, congratulations on your work. I'm in a similar situation than Hsien-Chi Kuo. I want to segment more or less spherical objects in 3D but I don't know how to generate "FV". Is it necessary to build a trianguled mesh, can't it be spherical?
Thanks very much.

Hsien-Chi Kuo

### Hsien-Chi Kuo (view profile)

Hi,

I'm a MS student doing tumor segmentation on 3D images.
Would you please tell me how to generate "FV" for snake3D?
Thank you very much.

too ym

### too ym (view profile)

Hi,

% Interpolate points inbetween
O(:,1)=interp([P(end-3:end,1);P(:,1);P(:,1);P(1:4,1)],10);
O(:,2)=interp([P(end-3:end,1);P(:,2);P(:,2);P(1:4,2)],10);

In function InterpolateContourPoints2D, is it the 1 in O(:,2) should be write as 2.

O(:,2)=interp([P(end-3:end,2);P(:,2);P(:,2);P(1:4,2)],10);

Correct me if i am wrong and thank you for your great job.

Yuanming Suo

### Yuanming Suo (view profile)

Great tool. Well documented. Good for use to understand parametric deformable model.

Vivek

### Vivek (view profile)

Hi,
i'm Vivek, doing my masters project in BioMedical image processing,
i need to use the snake for analyzing and predicting the growth of cancer tumors, could you please let me know, how to run this illustrated example in Matlab 7.8.0 R2009?

vivek.acm@gmail.com

David

### David (view profile)

well done, easy to read and understand.

terrence pong

### terrence pong (view profile)

where can i find the snake.m file to start?