# Automatic femur segmentation edge closing

**URL:** <https://discourse.itk.org/t/automatic-femur-segmentation-edge-closing/428>\
**Category:** Algorithms\
**Created:** [November 17, 2017, 6:09am UTC](https://discourse.itk.org/t/automatic-femur-segmentation-edge-closing/428 "2017-11-17T06:09:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pythonsegmenter](https://discourse.itk.org/user_avatar/discourse.itk.org/pythonsegmenter/32/224_2.png) [@Pythonsegmenter](https://discourse.itk.org/u/Pythonsegmenter)\
**Post date:** [November 17, 2017, 6:09am UTC](https://discourse.itk.org/t/automatic-femur-segmentation-edge-closing/428/1 "2017-11-17T06:09:58Z")

</div>

Hi all,

I’m doing a project in which I need to automatically segment the femur. It seemed like an easy job (great difference between bone and soft tissue) but I’ve been stuck for a while. I’ve already written a basic algorith to segment it which gives some results but they’re not perfect. The biggest problem is that there are some holes in it. I can’t close the wholes by lowering the threshold because than the kneecap and hip get segmented as well. I need to fix this since it should be a mesh on which I can do a Finite element analysis.

I’ll summarize what I’ve done so far:

1. Threshold untill hip is no longer segmented.
2. Threshold femur at a lower threshold (hip will be selected now)
3. Dilate the hip that we got from thresholding in 1.
4. Distract the hip from thresholding operation in 2.
5. Same process is done to remove the kneecap.

The result is quite good, but there are some holes near the femur neck and certainly at the distal condyles. I was thinking about selecting the edge pixels (still need to figure out how to do that) and to plot a spline through them and then the spline would represent the edge and all the rest I would fill up with fill hole. Is there some way to do something similar? Do you think I could do this in Matlab, or will it take ages to process due to longer computing times in Matlab than in the efficient C implementation of SimpleITK? Below you can see an example slice and the 3D model I’ve managed to achieve so far.

 ![image](https://discourse.itk.org/uploads/default/original/1X/f8da26da7f77acd418328839d8b0fadd6f548c94.jpg)  
 ![image](https://discourse.itk.org/uploads/default/original/1X/878f28f5d79e990c3ce2f5baf947794c72c9a518.jpg)

It’s a long post but I hope someone can give me some guidelines on how to continue 🙂

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [November 17, 2017, 2:09pm UTC](https://discourse.itk.org/t/automatic-femur-segmentation-edge-closing/428/2 "2017-11-17T14:09:39Z")

</div>

You could take a look at [this](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1BinaryFillholeImageFilter.html) and [this](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1VotingBinaryIterativeHoleFillingImageFilter.html) filter. An [example](https://itk.org/ITKExamples/src/Segmentation/LabelVoting/IterativeHoleFilling/Documentation.html) how to use the latter in ITK.

---

<div class="post-metadata">

**Author:** ![Pythonsegmenter](https://discourse.itk.org/user_avatar/discourse.itk.org/pythonsegmenter/32/224_2.png) [@Pythonsegmenter](https://discourse.itk.org/u/Pythonsegmenter)\
**Post date:** [November 17, 2017, 3:09pm UTC](https://discourse.itk.org/t/automatic-femur-segmentation-edge-closing/428/3 "2017-11-17T15:09:52Z")

</div>

Thanks! I can’t get the latter to work though. It needs an unsigned int for maximumNumberOfIterations but even if I give him that he keeps showing me this error:

 ![image](https://discourse.itk.org/uploads/default/original/1X/ff53a1eb780f2b44a7b20ce31dcc854218996438.png)

---

<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:** [November 17, 2017, 3:42pm UTC](https://discourse.itk.org/t/automatic-femur-segmentation-edge-closing/428/4 "2017-11-17T15:42:12Z")

</div>

> In [2]: sitk.VotingBinaryIterativeHoleFilling?  
> Signature: sitk.VotingBinaryIterativeHoleFilling(\*args, \*\*kwargs)  
> Docstring:  
> VotingBinaryIterativeHoleFilling(Image image1, VectorUInt32 radius, unsigned int maximumNumberOfIterations=10, unsigned int majorityThreshold=1, double foregroundValue=1.0, double backgroundValue=0.0) → Image
> 
> Fills in holes and cavities by iteratively applying a voting  
> operation.

The error message is referring to argument 2, which is `radius`. The error message says the that SimpleITK will use an std::vector of unsigned ints. You could pass something like `radius=[5,5,5]`.

---

<div class="post-metadata">

**Author:** ![Pythonsegmenter](https://discourse.itk.org/user_avatar/discourse.itk.org/pythonsegmenter/32/224_2.png) [@Pythonsegmenter](https://discourse.itk.org/u/Pythonsegmenter)\
**Post date:** [November 17, 2017, 3:44pm UTC](https://discourse.itk.org/t/automatic-femur-segmentation-edge-closing/428/5 "2017-11-17T15:44:44Z")

</div>

Damn, sorry I got confused with indexing. Thanks!

---

<div class="post-metadata">

**Author:** ![Pythonsegmenter](https://discourse.itk.org/user_avatar/discourse.itk.org/pythonsegmenter/32/224_2.png) [@Pythonsegmenter](https://discourse.itk.org/u/Pythonsegmenter)\
**Post date:** [November 17, 2017, 9:22pm UTC](https://discourse.itk.org/t/automatic-femur-segmentation-edge-closing/428/6 "2017-11-17T21:22:13Z")

</div>

Hi all,

So i made some progress. I found a wrap function (for the 3D model), that works quite nicely. However inorder to decrease the holes untill they were wrapable I had to lower the threshold. Now I accidently start segmenting veins. Any idea’s on how to get these out?  
 ![image](https://discourse.itk.org/uploads/default/original/1X/496e8ef01eff16c481b02552e9b9e32f5716d48c.jpg)  
 ![image](https://discourse.itk.org/uploads/default/original/1X/a9ecc811822c6befbefecfa98e2a6abcc774ba66.jpg)

Thanks!

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [November 18, 2017, 12:48am UTC](https://discourse.itk.org/t/automatic-femur-segmentation-edge-closing/428/7 "2017-11-18T00:48:30Z")

</div>

The eternal conundrum. Lower the threshold to avoid holes in the organ of interest, and as side-effect inadvertently segment some unwanted structure.

What you could to for your case is get [vesselness](https://itk.org/Doxygen/html/classitk_1_1HessianToObjectnessMeasureImageFilter.html) feature, which can help you remove it. But this filter isn’t wrapped for Python, so you might follow @blowekamp’s recommendation [here](https://public.kitware.com/pipermail/community/2015-November/010127.html).

---

<div class="post-metadata">

**Author:** ![Pythonsegmenter](https://discourse.itk.org/user_avatar/discourse.itk.org/pythonsegmenter/32/224_2.png) [@Pythonsegmenter](https://discourse.itk.org/u/Pythonsegmenter)\
**Post date:** [November 18, 2017, 12:56am UTC](https://discourse.itk.org/t/automatic-femur-segmentation-edge-closing/428/8 "2017-11-18T00:56:40Z")

</div>

Hi Dzenan,

Thanks for the advice but if it’s no part of Simpleitk than it will  
probably take me ages to figure out how to write such a json file. I spent  
a couple of days just trying to build Simpleitk (as a matter of fact I  
still didn’t succeed one day I’ll get back to it). I’ll try to mess around  
with the existing filters.

Thanks anyway!
