UPDATE:
My new post: Sparse Field Active Contours
implements quicker, more accurate active contours.

Today, I added demo code for the Hybrid Segmentation project. This segmentation algorithm (in the publications section) can be used to find the boundary of objects in images. This approach uses localized statistics and sometimes gets better results than classic methods. For an example, see the video below: The contour begins as a rectangle, but deforms over time so that it finally forms the outline of the monkey.

This can be used to segment many different classes of image. To try it out, download the demo below and run >>localized_seg_demo

localized_seg.zip

This code is based on a standard level set segmentation; it just optimizes a different energy. I’ve also made a demo which implements the well-known Chan-Vese segmentation algorithm. This technique is similar to the one above, but it looks at global statistics. This makes it more robust to initialization, but it also means that more constraints are placed on the image. Download it and see what you think! Again, unzip the file and run >>region_seg_demo

sfm_chanvese_demo.zip (New! Described Here)
regionbased_seg.zip (old and slow)

This code can be used according to the MIT license. As long as this work is appropriately cited and attributed, and not being used for proprietary or commercial purposes, I’m fully supportive of you using it. Please drop me a line if it helps you!

## 21 thoughts on “Active Contour Matlab Code Demo”

1. zhang le says:

It seems works well on your special photo.It doesn’t output a satisfy result on my photo.I will do some study on your code…

2. Also, it is true that this technique doesn’t work well on all images. Consider some of the other segmentation techniques I show [here] as well as many others in the computer vision literature!

3. vahid says:

hi
dear
i am msc student in iran
i want many code about snake and level set for lip tracking
thanks

4. xiaochunlei says:

Your methods doesn’t work well on other iamges and they cost a lot of time. Is there exist some methods to speed up?Looking forward your new improved!

5. khusairy says:

its a good code but what is alpha in the program coz i didn’t find any alpha value when i go through chan vese formulation is the alpha refer to lamda in Chan vese

6. khusairy: Yes, \alpha = \lambda in this case. Just a simple matter of swapped notation.

7. Med says:

Hi Lankton

Thank you for all these codes very useful.
I have a question concerning the Chan&Vese code. I did not check other implementations, but I wanted to know whay have you used this:
F./max(abs(F))
is it for normalization ?
I want to add another shape prior term, do you think it is preferable I add it to F, then it’s gonna be normalized by this form (F./max(abs(F))) or add it directly to the hole Energu F with an onther coefficient, beta, as alpha..??

Thanks!

8. khusairy says:

Hi Shawn

it’s me again i think the alpha in you program is not lambda but u which as in the equation u*Length(C)

correct me if i’m wrong, thx in advance

regards

9. Elliot says:

hi Shawn
it is cool of you to share so many codes.they’re very useful.But,there seems something wrong in your codes on the documemt localized_seg. i try my best to find the errors(listed as follows),but fail.so put it here .looking forward to your reply,thanks!
……
Error in ==> imshow at 85
[cdata, cdatamapping, clim, map, xdata, ydata, filename, …

Error in ==> localized_seg>showCurveAndPhi at 136
imshow(I,’displayrange’,[0 255]); hold on;

Error in ==> localized_seg at 116
showCurveAndPhi(I,phi,its);

Error in ==> localized_seg_demo at 19
seg = localized_seg(I, m, 400); %– run segmentation

10. I think you can correct this problem by re-writing the showCurveAndPhi function. imshow in older versions of Matlab do not handle all of the argument options that imshow has in newer versions. You could just use:
imshow(I);
imshow(I,’displayrange,'[0 255]);
and it should work.

11. raman says:

I want to use the Active Contour Toolbox, but I’m currently having Matlab 7.1 sp1. The Active contour toolbox says it works with newer versions only. Which version should I buy??

In your paper “localizing region based active Contours, you did not define the kernal which is utilized for histogram separation energy measurement. So can you guide me some detaio in that algorithm….
Thanking You,

13. You can use any smoothing kernel you like. In my case, I used a Gaussian smoothing kernel with a standard deviation of 5.

Sir, i tried to designe code based on histogram separation energy and that part added as third method in your given code i.e. 1st is CV model, 2nd is Yezzi model and 3rd is i my design code, as below:
Jin=I(upts); Hin=hist(Jin);Pin=Hin/length(Jin);, Similarly for exterior part i calculate the probabilty of pixel with in radius of defined circle.
Function is : Bhattachrya Coefficient B= sum(sqrt(Pin(:),Pout(:)));
t11= B*(1/Ain’-1/Aout’)); t21= K*(255-I(idx)); t22= (sqrt(Pout/Pin))/Ain’-(sqrt(Pin/Pout))/Aout’
F=-(u-v)*(t11+t21*t22);
but i am not gettting the result.. So it what wrong i am doing?
Thank You Sir,,

Dear Sir,
i have successfully modified your program for histogram separation based image segmentation. But it takes much time execution. Another thing i want to ask is what are the limitation of your algorithm , so that it is not segmentating object succesfully for other images. I tried much , but i could not get the answer. One request is i am also doing work in the same field. what next i can do, will please guide me.

Thanking You

Great, good work. The execution time is a drawback of the histogram separation energy. I plan to release new codes soon which will be much faster than the implementation you are using.

The paper discusses some of the limitations of the algorithm. From experimenting it seems that it works best in medical applications where the contour can be initialized close to the object. For processing natural images, automatic selection of the localization radius is a challenge that is still an open question.

Earlier i asked about the histogram separation energy in your program of Local region based image segmentation , but if i want to change the Heaviside function , then how can i do in your program , because i don’t find the function for heaviside that is mentioned as in your paper.

18. In my implementations, I typically use $\epsilon=0$. In that case, the Heaviside function is simply 1-sign($\phi$). That cuts down on quite a few computations.

19. Yossi says: