# Calculate volume of largest and minimum ellipse around a segmentation

**URL:** <https://discourse.itk.org/t/calculate-volume-of-largest-and-minimum-ellipse-around-a-segmentation/2667>\
**Category:** Algorithms\
**Tags:** dicom, python\
**Created:** [January 31, 2020, 3:39pm UTC](https://discourse.itk.org/t/calculate-volume-of-largest-and-minimum-ellipse-around-a-segmentation/2667 "2020-01-31T15:39:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ral\_san](https://discourse.itk.org/user_avatar/discourse.itk.org/ral_san/32/236_2.png) [@ral\_san](https://discourse.itk.org/u/ral_san)\
**Post date:** [January 31, 2020, 3:39pm UTC](https://discourse.itk.org/t/calculate-volume-of-largest-and-minimum-ellipse-around-a-segmentation/2667/1 "2020-01-31T15:39:53Z")

</div>

Is there any function in SimpleITK similar to the OpenCV function FitEllipse --\> [https://docs.opencv.org/3.4.9/de/d62/tutorial\_bounding\_rotated\_ellipses.html](https://docs.opencv.org/3.4.9/de/d62/tutorial_bounding_rotated_ellipses.html) ?

What I am trying to do is to calculate the volume of the largest and minimal ellipsoid volume related to a segmented tumor.  
Something like this figure, where 1 is my tumor segmentation and 2 and 3 would be the ellipsoid volumes that I need to calculate.

I am not looking for code but just for some direction. ![Picture1](https://discourse.itk.org/uploads/default/original/2X/b/b2a81800bbf9b1f1447ed65341f887e943035a82.png)

Thank you for your help.

---

<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:** [January 31, 2020, 3:47pm UTC](https://discourse.itk.org/t/calculate-volume-of-largest-and-minimum-ellipse-around-a-segmentation/2667/2 "2020-01-31T15:47:38Z")

</div>

I don’t think there is an ellipse fitting class. What you could look into is [LabelShapeStatisticsImageFilter](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1LabelShapeStatisticsImageFilter.html) and [LabelGeometryImageFilter](https://itk.org/Doxygen/html/classitk_1_1LabelGeometryImageFilter.html), and possibly their source code.

---

<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:** [January 31, 2020, 4:21pm UTC](https://discourse.itk.org/t/calculate-volume-of-largest-and-minimum-ellipse-around-a-segmentation/2667/3 "2020-01-31T16:21:00Z")

</div>

I am not sure how well defined or unique the “minimal” ellipsoid would be.

In the [LabelShapeStatisticsImageFilter](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1LabelShapeStatisticsImageFilter.html), the principle axis are computed. There are several derived parameters from that you could look into. The original [Insight Journal article](https://hdl.handle.net/1926/584) may be the best reference.

Beyond those existing attributes you would need to create your own. A method needs to be added to `LabelShapStatisticsImageFilter` to get a list of indices or points for a given label. In the mean time something like `np.where(sitk.GetArrayViewFromImage(lableImage))` could provide similar functionality.

---

<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:** [January 31, 2020, 9:40pm UTC](https://discourse.itk.org/t/calculate-volume-of-largest-and-minimum-ellipse-around-a-segmentation/2667/4 "2020-01-31T21:40:13Z")

</div>

Here is the PR adding the new functionality to SimpleITK:

> <https://github.com/SimpleITK/SimpleITK/pull/960>

---

<div class="post-metadata">

**Author:** ![ral\_san](https://discourse.itk.org/user_avatar/discourse.itk.org/ral_san/32/236_2.png) [@ral\_san](https://discourse.itk.org/u/ral_san)\
**Post date:** [February 14, 2020, 10:21am UTC](https://discourse.itk.org/t/calculate-volume-of-largest-and-minimum-ellipse-around-a-segmentation/2667/5 "2020-02-14T10:21:31Z")

</div>

I think I managed to solve this issue.

> <https://gist.github.com/raluca-san/5d8bd4dcb3278b8e2f6dfa959614f9c8>

What is missing is extracting the surface points from a segmentation mask. That could be extracted using the `SimpleITK.LabelContour(img, fullyConnected=False)`.  
Also the spacing information is missing, so the ellipsoid should be adjusted by placing it on the correct grid.  
This can be achieved `scikit-image.draw.ellipsoid`[https://scikit-image.org/docs/dev/api/skimage.draw.html#skimage.draw.ellipsoid](https://scikit-image.org/docs/dev/api/skimage.draw.html#skimage.draw.ellipsoid).

Is it possible to propose adding this functionality to the official SimpleITK library?

---

<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:** [February 14, 2020, 1:35pm UTC](https://discourse.itk.org/t/calculate-volume-of-largest-and-minimum-ellipse-around-a-segmentation/2667/6 "2020-02-14T13:35:10Z")

</div>

Thanks for sharing your solution. But the problem is somewhat specialized. Moreover, ITK and SimpleITK are written in C++ so you would have to translate your solution into C++ for incorporation.

---

<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:** [February 14, 2020, 2:56pm UTC](https://discourse.itk.org/t/calculate-volume-of-largest-and-minimum-ellipse-around-a-segmentation/2667/7 "2020-02-14T14:56:00Z")

</div>

Thanks for sharing your implementation of computing the optimal ellipsoid.

I believe you are asking to get the points from a contour in SimpleITK.

The above linked PR was merged and you can download the [latest binaries](https://github.com/SimpleITK/SimpleITK/releases/tag/latest) to use it.

Here is sample usage:

```auto

In [4]: labels = sitk.ReadImage("/scratch/blowekamp/SimpleITK/SimpleITK-build/ExternalData/Testing/Data/Input/2th_cthead1.png")                                                                                                                             

In [5]: sitk.LabelContour(labels, fullyConnected=False)                                                                                                                                                                                                     
Out[5]: <SimpleITK.SimpleITK.Image; proxy of <Swig Object of type 'std::vector< itk::simple::Image >::value_type *' at 0x7f8052bdbc00> >

In [6]: contours = sitk.LabelContour(labels, fullyConnected=False)                                                                                                                                                                                          

In [7]: shape = sitk.LabelShapeStatisticsImageFilter()                                                                                                                                                                                                      

In [8]: shape.Execute(contours)                                                                                                                                                                                                                             

In [9]: shape.GetLabels()                                                                                                                                                                                                                                   
Out[9]: (100, 200)

In [10]: idx = shape.GetIndexes(100)      

```
