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

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

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

I with the PhysicalPointImageSource 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 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 for this.

1 Like

Could you start with a numpy array, fill in the region, and then wrap it to a SimpleITK image using sitk.GetImageFromArray? Just an idea, not sure about time or space complexity.

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). Is that also possible in simpleITK?

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


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.

2 Likes

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

2 Likes

Wow, cool, that needed some digestion :wink: 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…

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

1 Like

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.

1 Like

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.

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.:

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:

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

to

rec= sitk.Paste(img, box)

PS: The link that should point to the procedural interface on:
https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1PhysicalPointImageSource.html#details
points to itself.

Thank you for the report. Could you please create a Github issues to the issue will not be forgotten.