# Functional Interface (Python) to itk::ExtractImageFilter, itk.extract\_image\_filter(img, ...) — What Are The Rest Of The Parameters?

**URL:** <https://discourse.itk.org/t/functional-interface-python-to-itk-extractimagefilter-itk-extract-image-filter-img-what-are-the-rest-of-the-parameters/7248>\
**Category:** Uncategorized\
**Created:** [October 9, 2024, 1:21am UTC](https://discourse.itk.org/t/functional-interface-python-to-itk-extractimagefilter-itk-extract-image-filter-img-what-are-the-rest-of-the-parameters/7248 "2024-10-09T01:21:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)\
**Post date:** [October 9, 2024, 1:21am UTC](https://discourse.itk.org/t/functional-interface-python-to-itk-extractimagefilter-itk-extract-image-filter-img-what-are-the-rest-of-the-parameters/7248/1 "2024-10-09T01:21:01Z")

</div>

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:

```auto
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?

---

<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:** [October 9, 2024, 1:52pm UTC](https://discourse.itk.org/t/functional-interface-python-to-itk-extractimagefilter-itk-extract-image-filter-img-what-are-the-rest-of-the-parameters/7248/2 "2024-10-09T13:52:44Z")

</div>

Take a look at [filter documentation](https://itk.org/Doxygen/html/classitk_1_1ExtractImageFilter.html), 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.

---

<div class="post-metadata">

**Author:** ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)\
**Post date:** [November 10, 2024, 4:29am UTC](https://discourse.itk.org/t/functional-interface-python-to-itk-extractimagefilter-itk-extract-image-filter-img-what-are-the-rest-of-the-parameters/7248/3 "2024-11-10T04:29:28Z")

</div>

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

```auto
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?

---

<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 11, 2024, 4:52pm UTC](https://discourse.itk.org/t/functional-interface-python-to-itk-extractimagefilter-itk-extract-image-filter-img-what-are-the-rest-of-the-parameters/7248/4 "2024-11-11T16:52:13Z")

</div>

Looking at [wrapping specification](https://github.com/InsightSoftwareConsortium/ITK/blob/v5.4.0/Modules/Core/Common/wrapping/itkExtractImageFilter.wrap#L10-L12), 3D → 1D extraction seems like the first defined one, so functional interface is probably picking that one. Try the object oriented interface, something like:

```py
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()

```

---

<div class="post-metadata">

**Author:** ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)\
**Post date:** [November 13, 2024, 5:24am UTC](https://discourse.itk.org/t/functional-interface-python-to-itk-extractimagefilter-itk-extract-image-filter-img-what-are-the-rest-of-the-parameters/7248/5 "2024-11-13T05:24:19Z")

</div>

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

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…
