# fill a region (square, ellipse) of an image with specific value in SimpleITK

**URL:** <https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688>\
**Category:** Beginner Questions\
**Tags:** simpleitk\
**Created:** [February 19, 2018, 3:14pm UTC](https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688 "2018-02-19T15:14:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [February 19, 2018, 3:14pm UTC](https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688/1 "2018-02-19T15:14:43Z")

</div>

Hello,

Is there an elegant way in sitk to fill a region e.g. square/ellipse given index and size of an image with a specific value using python?

Many thanks for any help or hints  
Roman

---

<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 20, 2018, 4:04pm UTC](https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688/2 "2018-02-20T16:04:44Z")

</div>

I would say that there is not elegant way to do in currently in SimpleITK.

I with the [`PhysicalPointImageSource`](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1PhysicalPointImageSource.html) class, which generates an image of the physical location at each pixel, I have evaluated some implicit functions utilizing overloaded mathematical operators to avoid iterating over each pixel. One example is a [`Mandelbrot`](https://en.wikipedia.org/wiki/Mandelbrot_set) test.

However, I think being able to “render” basic spacial object would be a nice feature. You might want to create a feature request on the [Github repository](https://github.com/SimpleITK/SimpleITK) for this.

---

<div class="post-metadata">

**Author:** ![ctrueden](https://discourse.itk.org/user_avatar/discourse.itk.org/ctrueden/32/38_2.png) [@ctrueden](https://discourse.itk.org/u/ctrueden)\
**Post date:** [February 20, 2018, 7:18pm UTC](https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688/3 "2018-02-20T19:18:51Z")

</div>

Could you start with a numpy array, [fill in the region](https://stackoverflow.com/a/37123933/1207769), and then wrap it to a SimpleITK image using `sitk.GetImageFromArray`? Just an idea, not sure about time or space complexity.

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [February 21, 2018, 10:26am UTC](https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688/4 "2018-02-21T10:26:14Z")

</div>

Thanks for your replies. I thought there might be a special (elegant) way in simpleITK like e.g. the use of operators to do image thresholding. Just looking at filling an (ITK) region (index, size), in plain ITK one could use the `ThresholdImageFilter` and use `SetRequestedRegion` (as in [http://itk.org/Wiki/ITK/Examples/ImageProcessing/ImageFilterOnARegion](http://itk.org/Wiki/ITK/Examples/ImageProcessing/ImageFilterOnARegion)). Is that also possible in simpleITK?

---

<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 21, 2018, 2:10pm UTC](https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688/5 "2018-02-21T14:10:56Z")

</div>

Here is an example of using the PhysicalPointSource and whole image computation, I’ll let you decide if it is “elegant” or not:

```auto

In [1]: import SimpleITK as sitk

In [2]: img = sitk.PhysicalPointSource( sitk.sitkVectorFloat32, [100,100])

In [3]: (xmin, xmax, ymin, ymax) = (10, 25, 35, 80)

In [5]: imgx = sitk.VectorIndexSelectionCast(img, 0)

In [6]: imgy = sitk.VectorIndexSelectionCast(img, 1)

In [7]: box = (imgx>xmin) & (imgx<xmax) & (imgy>ymin) & (imgy<ymax)

In [8]: center = (40,40)

In [9]: radius = 30

In [10]: circle = (sitk.Sqrt((imgx-center[0])**2 + (imgy-center[1])**2))<radius

```

With SimpleITK, you would just use some variant of the CropImage filter followed by the PasteImageFilter to accomplish a similar thing as that example.

---

<div class="post-metadata">

**Author:** ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)\
**Post date:** [February 21, 2018, 2:31pm UTC](https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688/6 "2018-02-21T14:31:39Z")

</div>

In the SimpleITK Hello World example, I make a smiley face using a series of Gaussian blobs and thresholding. You can see the code here:

[http://simpleitk.readthedocs.io/en/latest/Examples/HelloWorld/Documentation.html](http://simpleitk.readthedocs.io/en/latest/Examples/HelloWorld/Documentation.html)

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [February 21, 2018, 4:09pm UTC](https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688/7 "2018-02-21T16:09:12Z")

</div>

Wow, cool, that needed some digestion 😉 It surely qualifies as simpleITK/python magic and definitely has some elegance (elegance and magic not being well defined…)  
So line 7 thresholds img in the regions defined in line 3 and similar for line 10.  
Even though knowing how to read this, it does need some more time to start thinking in simpleITK’s world of possibilities…

> [@blowekamp](#):
>
> With SimpleITK, you would just use some variant of the CropImage filter followed by the PasteImageFilter to accomplish a similar thing as that example.

So there is no direct way to make use of `SetRequestedRegion` in simpleITK?

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [February 21, 2018, 4:15pm UTC](https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688/8 "2018-02-21T16:15:19Z")

</div>

That is also a very helpful example demonstrating the power of simpleITK. I especially like the possibility to compare with other languages. It shows how compact the code can be, especially under Python, R and TCL. Apparently, TCL needs least lines and chars.  
I would suggest to give it a more descriptive name because I would not look for a “hello world” example to find code that draws a smiley with Gaussian blobs.

---

<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 21, 2018, 6:10pm UTC](https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688/9 "2018-02-21T18:10:42Z")

</div>

> [@grothausmann.roman](#):
>
> So there is no direct way to make use of SetRequestedRegion in simpleITK?

It is a feature of SimpleITK that the pipeline is not exposed. For the object oriented interface we use the style `Execute(input1, input2 etc... )` to indicate immediate execution. This approach is much less error prone, but it does require different approaches than traditional ITK.

If you just want to execute some filter on a subregion, you can utilize the pythonic operator too. You could do something like `output = sitk.Median(inputImg[xmin:xmax,ymin:ymax]`. There would be a boundary issue, that you could choose to ignore or handle manually if you so choose.

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [February 22, 2018, 2:05pm UTC](https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688/10 "2018-02-22T14:05:27Z")

</div>

Thanks for that clarification. I do like the idea of using the pythonic []  
operator, though it seems to need sikt.Paste to get the result that can be  
achieved with `SetRequestedRegion` in ITK (a method I always found very impressive), e.g.:

```auto
import SimpleITK as sitk
img= sitk.Image([100,100], sitk.sitkUInt8)
box= img[5:85,10:90] == 0
rec= sitk.Paste(img, box, sourceSize=list(box.GetSize()), sourceIndex=[0,0], destinationIndex=list(map(int,box.GetOrigin())))

```

It seems that `sourceIndex` can be skipped but `destinationIndex` and `sourceSize` are essential. Would it be possible to initialize these parameters to `PasteImageFilter` with the `index` and `size` of the image to paste?  
That would shorten:

```auto
rec= sitk.Paste(img, box, list(box.GetSize()), [0,0], list(map(int,box.GetOrigin())))

```

to

```auto
rec= sitk.Paste(img, box)

```

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [February 23, 2018, 10:43am UTC](https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688/11 "2018-02-23T10:43:19Z")

</div>

PS: The link that should point to the procedural interface on:  
[https://itk.org/SimpleITKDoxygen/html/classitk\_1\_1simple\_1\_1PhysicalPointImageSource.html#details](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1PhysicalPointImageSource.html#details)  
points to itself.

---

<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 23, 2018, 1:26pm UTC](https://discourse.itk.org/t/fill-a-region-square-ellipse-of-an-image-with-specific-value-in-simpleitk/688/12 "2018-02-23T13:26:56Z")

</div>

Thank you for the report. Could you please create a [Github issues](https://github.com/SimpleITK/SimpleITK/issues) to the issue will not be forgotten.
