Python sitk ExtractImageFilter -- basic usage help??

Hello Robert!

Thank you for reposting your question to discourse. It is much easier to post here, and provide links, and good formatting in the forum :slight_smile:

To preserve the same dimension you need to use the RegionOfInterest filter or the python bracket operator with slicing:

  1. RegionOfInterest
newim=sitk.RegionOfInterest(rawimg,(20,30,40),(0,0,0))
  1. Python Slicing
newim=rawimg[0:20,0:30,0:40]

The slicing operations can also be used to reduce dimensions, sub-sample with steps, and even flip the image all while preserving the physical location of the pixels!

There are various filters in ITK and SimpleITK that do similar things. This includes the Extract, Crop, RegionOfInterest and Slice image filters.

With SimpleITK you can’t specify the output image type, so some choices were made. What is unique about the Extract filter when compared to the others is that is can reduce the dimension of the input image to the output. So the SimpleITK Extract filter reduces the dimension by when when it can So in does 4D->3D, 3D->2D and 2D->2D ( since SimpleITK does not have 1D images). So this error message coming for itk::ExtractImageFilter is because it expect the output dimension to be 2D, therefore it expects one dimension of the size to be 0.

I’d specifically LIKE to maintain the origin of the region as the origin of the original image and in general it could be a 4-d (time-series) image as input too so I beleive ExtractImageFilter is the right choice?

All of these filters preserve the physical location of the pixels. The physical location of the region to extract does not change. So in general this should not be a concern. Since you have used C++ ITK, I’ll delve into an additional detail of SimpleITK. One simplification made, it that a SimpleITK image always has a starting index of [0]. So if the output of a ITK filter produces an image with a non-zero starting index, SimpleITK adjusted the starting index to zero, and fixes the origin appropriately. This still maintains the physical location of the pixel. It just removes a complication, that is rarely a benefit, and complicated the interface, requiring the index to be signed.

Hope that helps!

1 Like