Functional Interface (Python) to itk::ExtractImageFilter, itk.extract_image_filter(img, ...) — What Are The Rest Of The Parameters?

I’m trying to play with the functional Python interface to ITK filters, but I’m a bit lost in trying to figure out what parameters to use.

For example, this doesn’t make things clear:

help(itk.extract_image_filter)
Help on function extract_image_filter in module itk.itkExtractImageFilterPython:
extract_image_filter(*args: Union[ForwardRef('itk.ImageBase'), collections.abc.Buffer, numpy._typing._array_like._SupportsArray[numpy.dtype[Any]], numpy._typing._nested_sequence._NestedSequence[numpy._typing._array_like._SupportsArray[numpy.dtype[Any]]], bool, int, float, complex, str, bytes, numpy._typing._nested_sequence._NestedSequence[Union[bool, int, float, complex, str, bytes]]], direction_collapse_to_strategy=Ellipsis, extraction_region: 'itk.ImageRegion' = Ellipsis, **kwargs) -> Union[ForwardRef('itk.ImageBase'), collections.abc.Buffer, numpy._typing._array_like._SupportsArray[numpy.dtype[Any]], numpy._typing._nested_sequence._NestedSequence[numpy._typing._array_like._SupportsArray[numpy.dtype[Any]]], bool, int, float, complex, str, bytes, numpy._typing._nested_sequence._NestedSequence[Union[bool, int, float, complex, str, bytes]], Tuple[Union[ForwardRef('itk.ImageBase'), collections.abc.Buffer, numpy._typing._array_like._SupportsArray[numpy.dtype[Any]], numpy._typing._nested_sequence._NestedSequence[numpy._typing._array_like._SupportsArray[numpy.dtype[Any]]], bool, int, float, complex, str, bytes, numpy._typing._nested_sequence._NestedSequence[Union[bool, int, float, complex, str, bytes]]], ...]]
    Proxy of C++ itkExtractImageFilterIRGBUC2IRGBUC2 class.

Is there a way to figure this out?

Take a look at filter documentation, specifically “Public Member Functions” section. For this filter, the parameters should be direction_collapse_to_strategy and extraction_region, plus all the inherited stuff such as in_place etc.

@dzenanz — thank you for the suggestion to look at the docs, but it didn’t get me very far, because there’s nothing there about the arguments to the functional ITK Python itk.extract_image_filter function.

I’ve played with it and this works swimmingly well:

out = itk.extract_image_filter(image, region)

where image is of type itkImageUC3, and region is of type itkImageRegion3, while output image type is the same as input type, itkImageUC3.

Then, if I wanted to collapse the last dimension to get myself a 2D output, and if I were using itk.ExtractImageFilter in a C++ script, I’d set the last component in the region size to 0, in addition to specifying the output image type as itkImageUC2.

How do I achieve this, extract a 2D image from 3D input, with the extract_image_filter function in ITK Python?

(The step with setting last size component to 0 is only part of the game here, where the error message indicates it is necessary to somehow specify the output image type:

RuntimeError: C:\P\IPP\ITK-source\ITK\Modules\Core\Common\include\itkExtractImageFilter.hxx:94:
ITK ERROR: ExtractImageFilter(000001AC69C2D220): The number of zero sized dimensions in
the input image Extraction Region is not consistent with the dimensionality of the output image.
Expected the extraction region size ([125, 125, 0]) to contain 0 zero sized dimensions to collapse.

If I understand this error message, it contradicts Doxygen specifications for ExtractImageFilter, where it says that zero-valued component in region size indicates the dimension to collapse.)

In other words, is it possible to use extract_image_filter function in ITK Python to collapse dimensions, please?

Looking at wrapping specification, 3D → 1D extraction seems like the first defined one, so functional interface is probably picking that one. Try the object oriented interface, something like:

image3D = itk.Image[itk.UC, 3]
image2D = itk.Image[itk.UC, 2]
filter = itk.ExtractImageFilter[image3D, image2D].New(image)
filter.SetExtractionRegion(region)
filter.Update()
out = filter.GetOutput()

Yes, OO way works just fine, although it needs filter.SetDirectionCollapseToSubmatrix(), otherwise it refuses to run.

I’m really interested in leveraging the functional interface, given that it is there. They seem a lot more fun to use, if you know what I mean. :nerd_face:

It appears maybe a bit more work/refinement is required there, including documentation for all those fabulous itk.xxx_image_filter oh-so-very-yummy-and-functional functions (and their friends) to let them rule the world if ITK Python everything…

1 Like