# active contour without edge

**URL:** https://discourse.itk.org/t/active-contour-without-edge/1932
**Category:** Algorithms
**Tags:** python
**Created:** [May 31, 2019, 11:14pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932 "2019-05-31T23:14:16Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Jianxu\_Chen](https://discourse.itk.org/user_avatar/discourse.itk.org/jianxu_chen/32/809_2.png) [@Jianxu\_Chen](https://discourse.itk.org/u/Jianxu_Chen)
#### Post date: [May 31, 2019, 11:14pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/1 "2019-05-31T23:14:16Z")

</div>

I am trying to build a segmentation module using the following two algorithms.

[https://www.insight-journal.org/browse/publication/322](https://www.insight-journal.org/browse/publication/322)  
[http://insight-journal.org/browse/publication/323](http://insight-journal.org/browse/publication/323)

Is it possible for me to download the source C++ code published there and re-build them locally so that it can be used in the new ITK 5.0 pythonic way?

Many thanks,  
Jianxu

---

<div class="post-metadata">

### Author: ![jhlegarreta](https://discourse.itk.org/user_avatar/discourse.itk.org/jhlegarreta/32/476_2.png) [@jhlegarreta](https://discourse.itk.org/u/jhlegarreta)
#### Post date: [June 1, 2019, 1:36am UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/2 "2019-06-01T01:36:01Z")

</div>

Not sure whether they have been integrated/renamed in ITK, since ITK core does have a few filters implementing level set and active contour methods. You’d may be need to have a look at the `Review` folder of the legacy releases, and the release notes. Other experienced users of such filters may be able to confirm.

If the core filters do not match your needs, and these do, chances are that you’ll need to update them (i.e. you are likely to get build warnings, errors) since they are 10 years old, tested against 10-year old compilers, and ITK 5.0 is quite different from ITK 3.x, which was most probably used at the time.

But that is definitely a worthwhile effort that will benefit the community 👍. Making them available as a remote module would be an option.

Also, if you want to use them in Python, you’ll need to write the Python wrappings, since they are lacking the Python wrappers.

HTH.

---

<div class="post-metadata">

### Author: ![Jianxu\_Chen](https://discourse.itk.org/user_avatar/discourse.itk.org/jianxu_chen/32/809_2.png) [@Jianxu\_Chen](https://discourse.itk.org/u/Jianxu_Chen)
#### Post date: [June 1, 2019, 3:19pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/3 "2019-06-01T15:19:19Z")

</div>

@jhlegarreta, thanks for your swift reply. that is very helpful.

indeed, i found the following one as an ITK ReviewModule.

[https://itk.org/Doxygen/html/classitk\_1\_1ScalarChanAndVeseLevelSetFunction.html](https://itk.org/Doxygen/html/classitk_1_1ScalarChanAndVeseLevelSetFunction.html)

i think this is what I am looking for. What does ITK ReviewModule imdicate? means they are not ready to use, or can be used but just not in core yet?

thanks!

---

<div class="post-metadata">

### Author: ![jhlegarreta](https://discourse.itk.org/user_avatar/discourse.itk.org/jhlegarreta/32/476_2.png) [@jhlegarreta](https://discourse.itk.org/u/jhlegarreta)
#### Post date: [June 2, 2019, 3:29am UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/4 "2019-06-02T03:29:34Z")

</div>

You’re welcome @Jianxu_Chen !

The `Review` module contain classes that were initially submitted to the Insight Journal, and were/are in theory in the process of being reviewed/tested. These modules are available when the corresponding CMake flag is set to `ON`.

These classes eventually make it to the toolkit’s core when they have been thoroughly tested/their review period is considered to have been sufficient.

So to answer to your question:

- Yes, they are ready to be used. But you may experience some glitches, depending on your data, your platform or your requirements. If you do, providing fixes is more than welcome.
- They are not part of the core, but they may eventually be. Even if they dwell in the `Review` module, they are equally relevant and important to the toolkit.

HTH.

---

<div class="post-metadata">

### Author: ![Jianxu\_Chen](https://discourse.itk.org/user_avatar/discourse.itk.org/jianxu_chen/32/809_2.png) [@Jianxu\_Chen](https://discourse.itk.org/u/Jianxu_Chen)
#### Post date: [June 2, 2019, 3:32pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/5 "2019-06-02T15:32:34Z")

</div>

thanks for the info @jhlegarreta. i would be happy to be a tester of this module and related ones.

i just poked it around. do you know an instruction on how to compile or install itk with review modules turned on? i did a regular pip install to install itk. but it seems like the module cannot be found through python.

Or, maybe the review modules are installed, but just don’t have a python wrapper? any advice? if there is a general instruction on building python wrapper for itk modules, i would be happy to implement one.

thanks again,  
jianxu

---

<div class="post-metadata">

### Author: ![jhlegarreta](https://discourse.itk.org/user_avatar/discourse.itk.org/jhlegarreta/32/476_2.png) [@jhlegarreta](https://discourse.itk.org/u/jhlegarreta)
#### Post date: [June 2, 2019, 4:24pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/6 "2019-06-02T16:24:50Z")

</div>

I’d dare to say that the PyPI wheels do not include the review modules; @fbudin please correct me if I’m wrong.

In that case, you’ll need to build the package yourself. The [ITKPythonPackage](https://github.com/InsightSoftwareConsortium/ITKPythonPackage) repository may be useful in that task. [Uploading the package](https://pypi.org/search/?q=itk) may benefit others !

In order to know if the class you are looking for has a Python wrapping, you’ll need to have a look at the `wrapping` folder of the `Review` module. In this case, the class you are interested in [is wrapped](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Nonunit/Review/wrapping/itkScalarChanAndVeseSparseLevelSetImageFilter.wrap). Testing and proposing fixes as necessary is welcome.

HTh

---

<div class="post-metadata">

### Author: ![Jianxu\_Chen](https://discourse.itk.org/user_avatar/discourse.itk.org/jianxu_chen/32/809_2.png) [@Jianxu\_Chen](https://discourse.itk.org/u/Jianxu_Chen)
#### Post date: [June 2, 2019, 4:29pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/7 "2019-06-02T16:29:18Z")

</div>

great! thank you so much! i will test it out and keep people updated on my progress.

---

<div class="post-metadata">

### Author: ![Jianxu\_Chen](https://discourse.itk.org/user_avatar/discourse.itk.org/jianxu_chen/32/809_2.png) [@Jianxu\_Chen](https://discourse.itk.org/u/Jianxu_Chen)
#### Post date: [June 3, 2019, 8:08am UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/8 "2019-06-03T08:08:42Z")

</div>

@jhlegarreta, update:

I read through the instructions and tried to build ITK and the python wrappers. But, I kept getting the following error:

CMake Warning at Wrapping/Generators/Python/CMakeLists.txt:14 (message):  
Python executable (“3.6.1”) and library ("") version mismatch.

what I set:

PYTHON\_EXECUTABLE /home/jianxuc/anaconda3/bin/python  
PYTHON\_INCLUDE\_DIR /home/jianxuc/anaconda3/include  
PYTHON\_LIBRARY /home/jianxuc/anaconda3/lib/python3.6  
PYTHON\_LIBRARY\_DEBUG PYTHON\_LIBRARY\_DEBUG-NOTFOUND  
PY\_SITE\_PACKAGES\_PATH /home/jianxuc/anaconda3/lib/python3.6/site-packages/

To my understanding, cmake does not recognize the library version. Any thoughts or suggestions?

Thanks!

---

<div class="post-metadata">

### Author: ![jhlegarreta](https://discourse.itk.org/user_avatar/discourse.itk.org/jhlegarreta/32/476_2.png) [@jhlegarreta](https://discourse.itk.org/u/jhlegarreta)
#### Post date: [June 3, 2019, 1:07pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/9 "2019-06-03T13:07:03Z")

</div>

Not sure about it (I seldom build the Python wrap), but the `PYTHON_LIBRARY_DEBUG` variable may need to be set also.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [June 3, 2019, 1:53pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/10 "2019-06-03T13:53:47Z")

</div>

Hello,

Just an FYI, the classes submitted in that old IJ article were a predecessor the the [“ITKLevelSetsv4”](https://itk.org/Doxygen/html/group__ITKLevelSetsv4.html).

Here is an example of using the newer levelset framework for “Chan and Vesel” segmentation:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Segmentation/LevelSetsv4/test/itkLevelSetEquationChanAndVeseExternalTermTest.cxx>

IMHO, this is a class in review that likely should go into a remote/external module because it’s successor is already in ITK.

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [June 3, 2019, 3:17pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/11 "2019-06-03T15:17:06Z")

</div>

Thanks @blowekamp for pointing @Jianxu_Chen to the newer implementation. The new classes are not wrapped in Python (there is not `wrapping` folder [here](https://github.com/InsightSoftwareConsortium/ITK/tree/master/Modules/Segmentation/LevelSetsv4)). To make these classes avaiable in Python, one would need create a pull-request to add those wrapping files. Information about how to create wrapping files can be found in the [Software Guide](https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch9.html#x55-1520009.5) and by looking at existing wrapping files for other modules (in the `wrapping` subfolder).

@blowekamp: If the filter in the Review folder is a duplicate, it would indeed make sense to remove it from ITK. Since we just released ITK v5, it is probably too late to just remove it, but moving it to an remote module seems to me to be the appropriate solution.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [June 3, 2019, 3:21pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/12 "2019-06-03T15:21:09Z")

</div>

SimpleITK has the [“ScalarChanAndVeseDenseLevelSet”](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1ScalarChanAndVeseDenseLevelSetImageFilter.html) already in the distribution.

---

<div class="post-metadata">

### Author: ![Jianxu\_Chen](https://discourse.itk.org/user_avatar/discourse.itk.org/jianxu_chen/32/809_2.png) [@Jianxu\_Chen](https://discourse.itk.org/u/Jianxu_Chen)
#### Post date: [June 3, 2019, 5:26pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/13 "2019-06-03T17:26:12Z")

</div>

@blowekamp thanks for pointing that out. That’s really helpful.

But, I am having a hard time to find any instruction on how to use it. What I found is [http://insightsoftwareconsortium.github.io/SimpleITK-Notebooks/Python\_html/300\_Segmentation\_Overview.html](http://insightsoftwareconsortium.github.io/SimpleITK-Notebooks/Python_html/300_Segmentation_Overview.html)

But, it does not have a demo of ScalarChanVese, only saying it is supported. I tried a few different ways, my current one is

lsFilter = sitk.ScalarChanAndVeseDenseLevelSetImageFilter()  
lsFilter.SetMaximumRMSError(0.02)  
lsFilter.SetNumberOfIterations(20)  
ls = lsFilter.Execute(init\_levelset, itk\_img\_smooth)

But, I cannot any public member function to set other parameters like number of iterations and RMSError. I think there should be some paramter to control the curvature? Anyway, above code still gives me

Traceback (most recent call last):  
File “test\_ac.py”, line 110, in   
ls = lsFilter.Execute(init\_levelset, itk\_img\_smooth)  
File “/home/jianxuc/anaconda3/lib/python3.6/site-packages/SimpleITK/SimpleITK.py”, line 58659, in Execute  
return \_SimpleITK.ScalarChanAndVeseDenseLevelSetImageFilter\_Execute(self, \*args)  
NotImplementedError: Wrong number or type of arguments for overloaded function ‘ScalarChanAndVeseDenseLevelSetImageFilter\_Execute’.  
Possible C/C++ prototypes are:  
itk::simple::ScalarChanAndVeseDenseLevelSetImageFilter::Execute(itk::simple::Image const &,itk::simple::Image const &)  
itk::simple::ScalarChanAndVeseDenseLevelSetImageFilter::Execute(itk::simple::Image const &,itk::simple::Image const &,double,uint32\_t,double,double,double,double,double,double,double,double,itk::simple::ScalarChanAndVeseDenseLevelSetImageFilter::HeavisideStepFunctionType,bool)

Other info:  
raw is a np.float32 3D numpy array.

itk\_img = itk.GetImageFromArray(raw)  
gradientAnisotropicDiffusionFilter = itk.GradientAnisotropicDiffusionImageFilter.New(itk\_img)  
gradientAnisotropicDiffusionFilter.SetNumberOfIterations(5)  
gradientAnisotropicDiffusionFilter.SetTimeStep(0.0625)  
gradientAnisotropicDiffusionFilter.SetConductanceParameter(1.2)  
gradientAnisotropicDiffusionFilter.Update()  
itk\_img\_smooth = gradientAnisotropicDiffusionFilter.GetOutput()

Any thoughts? Or any advice on documentation or intructions?

Many thanks!

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [June 3, 2019, 5:58pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/14 "2019-06-03T17:58:52Z")

</div>

Hello,

We are in need of writing more SimpleITK LevelSet examples. I’ll look into this one.

What is the output of `lsFilter.SetCurvatureWeight?` or `dir(lsFilter)`?

From your error message, I’m going to guess that `init_levelset` is not a SimpleITK image. Minimal example are very important for debuting and staring problems.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [June 3, 2019, 8:40pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/15 "2019-06-03T20:40:44Z")

</div>

Here is a brief example:

```auto
img = sitk.ReadImage("cthead1.png", sitk.sitkFloat32)

fastMarching = sitk.FastMarchingBaseImageFilter()
fastMarching.AddTrialPoint([160,190,0])
radius = 10
fastMarching.SetStoppingValue(radius+5)
initial_ls = fastMarching.Execute(img*0+1)-radius

lsFilter = sitk.ScalarChanAndVeseDenseLevelSetImageFilter()
lsFilter.SetMaximumRMSError(0.0)
lsFilter.SetNumberOfIterations(500)
lsFilter.SetLambda1(1)
lsFilter.SetLambda2(1)
lsFilter.SetEpsilon(1.0)
lsFilter.SetCurvatureWeight(1.0)
lsFilter.SetAreaWeight(0.0)
lsFilter.SetReinitializationSmoothingWeight(0.0)
lsFilter.SetVolume(0.0)
lsFilter.SetVolumeMatchingWeight(0.0)
lsFilter.SetHeavisideStepFunction(lsFilter.AtanRegularizedHeaviside)
output = lsFilter.Execute(initial_ls, img)

```

This segments the whole head from the background. I was hoping to be able to just segment the cerebellum region in the the slice, but I’d need to review the paper again.

Hope this helps!

---

<div class="post-metadata">

### Author: ![Jianxu\_Chen](https://discourse.itk.org/user_avatar/discourse.itk.org/jianxu_chen/32/809_2.png) [@Jianxu\_Chen](https://discourse.itk.org/u/Jianxu_Chen)
#### Post date: [June 3, 2019, 11:39pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/16 "2019-06-03T23:39:48Z")

</div>

hi @blowekamp, thanks a lot. it is really helpful. After tweaking the image types a little bit in my problem, it is working now.

I found that the links in page below are wrong.

[https://simpleitk.readthedocs.io/en/master/Documentation/docs/source/filters.html](https://simpleitk.readthedocs.io/en/master/Documentation/docs/source/filters.html)

When I clicked the ScalarChanVese filter, it links to the ReviewModule, instead of the simple Module. In the ReviewModule, the list of functions .SetXXXXX() is very different. That is what made me so confused at the beginning.

Anyway, it makes sense to me now. I am trying to prepare a more exhaisted list of demos for different level set modules and will share it. Hope it can he helpful for other people.

Thanks again,  
Jianxu

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [June 4, 2019, 2:07pm UTC](https://discourse.itk.org/t/active-contour-without-edge/1932/17 "2019-06-04T14:07:34Z")

</div>

The links from SimpleITK’s [filter](https://simpleitk.readthedocs.io/en/master/Documentation/docs/source/filters.html) index [should](https://github.com/SimpleITK/SimpleITK/issues/762) go to SimpleITK doxygen. Thanks for the report.

Here is SimpleITK doxygen:  
[https://itk.org/SimpleITKDoxygen/html/classitk\_1\_1simple\_1\_1ScalarChanAndVeseDenseLevelSetImageFilter.html](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1ScalarChanAndVeseDenseLevelSetImageFilter.html)

Many of the additional methods to the simpelitk filter come from the underlying [LevelSetFunction](https://itk.org/Doxygen/html/classitk_1_1ScalarChanAndVeseLevelSetFunction.html).

I look forward to your demos!
