# 3d segmentation refinement

**URL:** <https://discourse.itk.org/t/3d-segmentation-refinement/4993>\
**Category:** Beginner Questions\
**Tags:** 3d, segmentation, python\
**Created:** [May 18, 2022, 12:54pm UTC](https://discourse.itk.org/t/3d-segmentation-refinement/4993 "2022-05-18T12:54:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AmericaBG](https://discourse.itk.org/user_avatar/discourse.itk.org/americabg/32/1925_2.png) [@AmericaBG](https://discourse.itk.org/u/AmericaBG)\
**Post date:** [May 18, 2022, 12:54pm UTC](https://discourse.itk.org/t/3d-segmentation-refinement/4993/1 "2022-05-18T12:54:26Z")

</div>

Hi!  
I’ve a 3d mask segmentation that I would like to improve by using some method. For example, something like use the mask like a “seed” and refine the segmentation with the original image information.

Original image  
 ![image](https://discourse.itk.org/uploads/default/original/2X/6/62b6ff5f2ed05fba0846a0f381fdf7f371aeba61.jpeg)  
Segmentation that I want to improve  
 ![image](https://discourse.itk.org/uploads/default/original/2X/b/b9d5c15836b8a41fdca6be33b7d39b38b1a18695.png)

Could you help me?  
Thank you very much 🙂

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [May 18, 2022, 1:09pm UTC](https://discourse.itk.org/t/3d-segmentation-refinement/4993/2 "2022-05-18T13:09:57Z")

</div>

Hello @AmericaBG,

Possibly look into using a watershed based segmentation approach.

Not sure if you are using SimpleITK or ITK. If SimpleITK, [this Jupyter notebook](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/32_Watersheds_Segmentation.ipynb) illustrates the use of watershed based segmentation. [This notebook](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/35_Segmentation_Shape_Analysis.ipynb) is a complete workflow, from image to shape analysis. The relevant section there is titled **Seed based watershed segmentation**.

---

<div class="post-metadata">

**Author:** ![AmericaBG](https://discourse.itk.org/user_avatar/discourse.itk.org/americabg/32/1925_2.png) [@AmericaBG](https://discourse.itk.org/u/AmericaBG)\
**Post date:** [May 19, 2022, 6:51am UTC](https://discourse.itk.org/t/3d-segmentation-refinement/4993/3 "2022-05-19T06:51:16Z")

</div>

Thank you very much @zivy .  
I’m going to try and I’ll said you about the result 🙂

---

<div class="post-metadata">

**Author:** ![AmericaBG](https://discourse.itk.org/user_avatar/discourse.itk.org/americabg/32/1925_2.png) [@AmericaBG](https://discourse.itk.org/u/AmericaBG)\
**Post date:** [May 19, 2022, 9:08am UTC](https://discourse.itk.org/t/3d-segmentation-refinement/4993/4 "2022-05-19T09:08:32Z")

</div>

Employing the images that I’ve shown before in the next expression:

ws=sitk.MorphologicalWatershedFromMarkers(  
**img** , **marker\_img** (mask), markWatershedLine=True, fullyConnected=False)

I obtain a 3D matrix full of 1’s. Do you know what could be the problem?

I’ve tried by applying _sitk.GradientMagnitude_ to the original image prior to the _sitk.MorphologicalWatershedFromMarkers_ function, but it’s the same, a 3D matrix full of 1’s as the result.

Thank you!

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [May 19, 2022, 5:34pm UTC](https://discourse.itk.org/t/3d-segmentation-refinement/4993/5 "2022-05-19T17:34:29Z")

</div>

Hello @AmericaBG,

To better understand the watershed based segmentation in ITK, highly recommend reading the [corresponding Insight Journal paper](https://www.insight-journal.org/browse/publication/92).

Is the initial segmentation a binary image? The initial segmentation visualization has multiple colors that all belong to the foreground so a bit suspicious. This could be just a visualization issue. Looks like the default matplotlib color map, viridis and I’d expect gray `matplotlib.pyplot.cm.Greys_r`.

In any case, the seed points should be unique integer labels. Please play a bit with the examples in the watershed notebook so that you have a good understanding of what the inputs are and how settings affect the resulting watershed segmentation.
