# Watershed from markers

**URL:** <https://discourse.slicer.org/t/watershed-from-markers/3155>\
**Category:** Development\
**Created:** [June 12, 2018, 6:45pm UTC](https://discourse.slicer.org/t/watershed-from-markers/3155 "2018-06-12T18:45:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vegoria](https://avatars.discourse-cdn.com/v4/letter/v/85f322/32.png) [@vegoria](https://discourse.slicer.org/u/vegoria)\
**Post date:** [June 12, 2018, 6:45pm UTC](https://discourse.slicer.org/t/watershed-from-markers/3155/1 "2018-06-12T18:45:15Z")

</div>

Hi!  
I’m making my first steps with developing slicer extention and I’ve stuck in one place…  
I’ve got MR of sacroiliac. I’m trying to process segmentation of bones using watershed from markers.

I’ve got something like that:

> import SimpleITK as sitk  
> import numpy as np
> 
> para = {‘sigma’:2}  
> itkimg = sitk.GetImageFromArray(imageArray)  
> itkimg = sitk.DiscreteGaussian(itkimg, para[‘sigma’])  
> itkimg = sitk.GradientMagnitude(itkimg)
> 
> lineimg = sitk.MorphologicalWatershedFromMarkers(itkimg, itkmarker, markWatershedLine=True)

where imageArray is numpy array which contains image.

but I need “itkmarker” image - it should be binary image which contains markers… And I have no idea how to get it. Or maybe there is simplier way to run that algorithm? I’ll be grateful for any tips.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 12, 2018, 7:33pm UTC](https://discourse.slicer.org/t/watershed-from-markers/3155/2 "2018-06-12T19:33:25Z")

</div>

ITK watershed segmentation is implemented in Watershed effect in SegmentEditorExtraEffects extension. The source code is available here: [https://github.com/lassoan/SlicerSegmentEditorExtraEffects/blob/master/SegmentEditorWatershed/SegmentEditorWatershedLib/SegmentEditorEffect.py](https://github.com/lassoan/SlicerSegmentEditorExtraEffects/blob/master/SegmentEditorWatershed/SegmentEditorWatershedLib/SegmentEditorEffect.py)

You are free to clone this implementation and modify it to suit your needs, or you can send pull request with proposed enhancements.

You can use this segmentation method without segment editor user interface as shown in these examples: [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How\_to\_run\_segment\_editor\_effects\_from\_a\_script](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_run_segment_editor_effects_from_a_script)
