# Floodfill Python ITK

**URL:** <https://discourse.itk.org/t/floodfill-python-itk/2453>\
**Category:** Algorithms\
**Created:** [November 27, 2019, 2:08pm UTC](https://discourse.itk.org/t/floodfill-python-itk/2453 "2019-11-27T14:08:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fraca](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fraca](https://discourse.itk.org/u/fraca)\
**Post date:** [November 27, 2019, 2:08pm UTC](https://discourse.itk.org/t/floodfill-python-itk/2453/1 "2019-11-27T14:08:37Z")

</div>

There’s a function in python to floodfill an image that has been previously segmented?

---

<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 27, 2019, 2:20pm UTC](https://discourse.itk.org/t/floodfill-python-itk/2453/2 "2019-11-27T14:20:51Z")

</div>

What do you mean by `flood fill`? Does [ConnectedComponent](https://itk.org/Doxygen/html/classitk_1_1ConnectedComponentImageFilter.html) filter do what you want?

---

<div class="post-metadata">

**Author:** ![fraca](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fraca](https://discourse.itk.org/u/fraca)\
**Post date:** [November 27, 2019, 2:57pm UTC](https://discourse.itk.org/t/floodfill-python-itk/2453/3 "2019-11-27T14:57:40Z")

</div>

I’m trying to color different regions defined by edges of an image (different regions inside that image).

---

<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 27, 2019, 2:59pm UTC](https://discourse.itk.org/t/floodfill-python-itk/2453/4 "2019-11-27T14:59:18Z")

</div>

Sounds like a job for [LabelToRGBImageFilter](https://itk.org/Doxygen/html/classitk_1_1LabelToRGBImageFilter.html). You might need to segment your image first, which might be easy, hard, or anything in between.

---

<div class="post-metadata">

**Author:** ![fraca](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fraca](https://discourse.itk.org/u/fraca)\
**Post date:** [November 27, 2019, 3:04pm UTC](https://discourse.itk.org/t/floodfill-python-itk/2453/5 "2019-11-27T15:04:57Z")

</div>

Thank you so much for your support

---

<div class="post-metadata">

**Author:** ![fraca](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fraca](https://discourse.itk.org/u/fraca)\
**Post date:** [November 28, 2019, 1:35pm UTC](https://discourse.itk.org/t/floodfill-python-itk/2453/6 "2019-11-28T13:35:23Z")

</div>

I tried to apply a custom color map in my code but i have an error that i don’t understand. Do you have any ideas? I want to apply this [Custom mask](https://itk.org/ITKExamples/src/Filtering/Colormap/CreateACustomColormap/Documentation.html) to my code, because i want to watershed it, after the previous segmentation  
This is a little explication about what i have to do  
 ![Cattura](https://discourse.itk.org/uploads/default/original/2X/e/edb66609cffcd9674f4c767f25d011be4ca6923c.png)

> import sys  
> import itk  
> inputFileName = “C:\Users\fraca\Desktop\IM21”  
> outputFileName = “C:\Users\fraca\Desktop\zerocrossing.mha”  
> Dimension = 2  
> FloatPixelType = itk.ctype(‘float’)  
> FloatImageType = itk.Image[FloatPixelType, Dimension]  
> reader = itk.ImageFileReader[FloatImageType].New()  
> reader.SetFileName(inputFileName)  
> zero=itk.ZeroCrossingBasedEdgeDetectionImageFilter.New(input=reader.GetOutput())  
> watershed = itk.WatershedImageFilter.New(Input=zero.GetOutput())  
> LabeledImageType = type(watershed.GetOutput())  
> PixelType = itk.ctype(‘unsigned char’)  
> RGBPixelType = itk.RGBPixel[PixelType]  
> RGBImageType = itk.Image[RGBPixelType, Dimension]  
> ColormapType = itk.CustomColormapFunction[PixelType, RGBPixelType]  
> colormap = ColormapType.New()  
> random=itk.MersenneTwisterRandomVariateGenerator.New()  
> random.SetSeed(0)  
> redChannel=  
> greenChannel=  
> blueChannel=  
> for i in range(255): {  
> redChannel.append(random.GetUniformVariate(0.0, 1.0)) greenChannel.append(random.GetUniformVariate(0.0, 1.0)) blueChannel.append(random.GetUniformVariate(0.0, 1.0))  
> }  
> colormap.SetRedChannel(redChannel)  
> colormap.SetGreenChannel(greenChannel)  
> colormap.SetBlueChannel(blueChannel)  
> ScalarToRGBColormapFilterType=itk.ScalarToRGBColormapImageFilter[LabeledImageType,RGBImageType]  
> colormapImageFilter=ScalarToRGBColormapFilterType.New()  
> colormapImageFilter.SetColormap(colormap)#colormap  
> colormapImageFilter.SetInput(watershed.GetOutput())  
> colormapImageFilter.Update()  
> WriterType = itk.ImageFileWriter[RGBImageType]  
> writer = WriterType.New()  
> writer.SetFileName(outputFileName)  
> writer.SetInput(colormapImageFilter.GetOutput())  
> writer.Update()

> —\> 55 colormapImageFilter.SetColormap(colormap)  
> 56 colormapImageFilter.SetInput(watershed.GetOutput())  
> 57 #colormapImageFilter.Update()  
> TypeError: in method ‘itkScalarToRGBColormapImageFilterIULL2IRGBUC2\_SetColormap’, argument 2 of type ‘itkScalarToRGBColormapImageFilterIULL2IRGBUC2::ColormapEnumType’

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [December 2, 2019, 5:37pm UTC](https://discourse.itk.org/t/floodfill-python-itk/2453/7 "2019-12-02T17:37:59Z")

</div>

[This example](https://itk.org/ITKExamples/src/Segmentation/Watersheds/MorphologicalWatershedSegmentation/Documentation.html?highlight=watershed), than uses the _MorphologicalWatershedImageFilter_ and _ScalarToRGBColormapImageFilter_ may be helpful.

Note that the _MorphologicalWatershedImageFilter_ behaves much better than the _WatershedImageFilter_, and it is preferred.

---

<div class="post-metadata">

**Author:** ![fraca](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fraca](https://discourse.itk.org/u/fraca)\
**Post date:** [December 4, 2019, 8:40am UTC](https://discourse.itk.org/t/floodfill-python-itk/2453/8 "2019-12-04T08:40:17Z")

</div>

Can you help me to convert these rows of code in python? I tried something but i always obtain some errors

> binaryHeavyGaussianFilterX-\>SetOrder(RecursiveGaussianFilterType::ZeroOrder);  
> binaryHeavyGaussianFilterY-\>SetOrder(RecursiveGaussianFilterType::ZeroOrder);

and

> if (vcl\_fabs(denominator) \< epsilon)  
> {  
> denominator = denominator \> itk::NumericTraits\< PixelType \>::Zero ? 1.0 : -1.0;  
> }

---

<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:** [December 4, 2019, 2:59pm UTC](https://discourse.itk.org/t/floodfill-python-itk/2453/9 "2019-12-04T14:59:48Z")

</div>

Here is the second part:

> [@fraca](#):
>
> if (vcl\_fabs(denominator) \< epsilon)  
> {  
> denominator = denominator \> itk::NumericTraits\< PixelType \>::Zero ? 1.0 : -1.0;  
> }

```Python
if abs(denominator) < epsilon
  if denominator > 0
    denominator = 1.0
  else
    denominator = -1.0

```

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [December 6, 2019, 3:27pm UTC](https://discourse.itk.org/t/floodfill-python-itk/2453/10 "2019-12-06T15:27:05Z")

</div>

> [@fraca](#):
>
> > binaryHeavyGaussianFilterX-\>SetOrder(RecursiveGaussianFilterType::ZeroOrder);

This will be available in Python with ITK 5.1 RC 1 as:

```auto
binaryHeavyGaussianFilterX.SetOrder(itk.GaussianOrderEnum_ZeroOrder)

```
