# RegionOfInterestImageFilter to extract image slice from volume

**URL:** https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787
**Category:** Beginner Questions
**Tags:** itk, python
**Created:** [February 7, 2022, 11:21am UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787 "2022-02-07T11:21:58Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![SofiaCV](https://discourse.itk.org/user_avatar/discourse.itk.org/sofiacv/32/2525_2.png) [@SofiaCV](https://discourse.itk.org/u/SofiaCV)
#### Post date: [February 7, 2022, 11:21am UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/1 "2022-02-07T11:21:59Z")

</div>

Hello,

I’d like to extract a 2D slice from a MRI NIfTI file. I believe the RegionOfInterestImageFilter function in itk will do the job:

[https://itk.org/ITKExamples/src/Filtering/ImageGrid/ExtractRegionOfInterestInOneImage/Documentation.html](https://itk.org/ITKExamples/src/Filtering/ImageGrid/ExtractRegionOfInterestInOneImage/Documentation.html)

However, I’m having difficulties using this function in Python. I’ve tried writing the code this way, without success:

```auto
image_slice11 = itk.region_of_interest_filter(image, [512,512,1], [0,0,11])

```

Can you provide advice on how that should be implemented?

Best regards!

---

<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: [February 7, 2022, 2:59pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/2 "2022-02-07T14:59:51Z")

</div>

It feels like you are confusing Region of Interest with [ExtractImageFilter](https://itk.org/Doxygen/html/classitk_1_1ExtractImageFilter.html). What happens if you replace RoI by Extract in your code?

---

<div class="post-metadata">

### Author: ![SofiaCV](https://discourse.itk.org/user_avatar/discourse.itk.org/sofiacv/32/2525_2.png) [@SofiaCV](https://discourse.itk.org/u/SofiaCV)
#### Post date: [February 7, 2022, 3:55pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/3 "2022-02-07T15:55:47Z")

</div>

Many thanks for the suggestion, @dzenanz!

When I replace the function in the line of code, I get the error below.

```auto
image_slice11 = itk.extract_image_filter(fixed, [512,512,1], [0,0,11])

TypeError: Expecting argument of type itkImageF3 or itkImageSourceIF3.
Additional information:
Wrong number or type of arguments for overloaded function 'itkImageToImageFilterIF3IF3_SetInput'.
  Possible C/C++ prototypes are:
    itkImageToImageFilterIF3IF3::SetInput(itkImageF3 const *)
    itkImageToImageFilterIF3IF3::SetInput(unsigned int,itkImageF3 const *)

```

I don’t understand what’s the problem, since I’ve checked and the input image ‘fixed’ is of type ‘itkImageF3’. Could it be that I’m setting the other inputs wrong?

I appreciate your help. Best!

---

<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: [February 7, 2022, 9:13pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/4 "2022-02-07T21:13:02Z")

</div>

What you seem to want is a subset of [this example](https://itk.org/ITKExamples/src/Filtering/ImageGrid/ProcessA2DSliceOfA3DImage/Documentation.html). The example uses the more verbose functional syntax, but you could try updating it to procedural syntax.

---

<div class="post-metadata">

### Author: ![SofiaCV](https://discourse.itk.org/user_avatar/discourse.itk.org/sofiacv/32/2525_2.png) [@SofiaCV](https://discourse.itk.org/u/SofiaCV)
#### Post date: [February 10, 2022, 12:39pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/5 "2022-02-10T12:39:17Z")

</div>

Thank you, for sharing this example!

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [February 14, 2022, 10:04pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/6 "2022-02-14T22:04:25Z")

</div>

Another option to be aware of: a) use `itk.xarray_from_image`, b) do the slicing with pandas/numpy syntax, c) then `itk.image_from_xarray`. This may be more convenient while preserving spatial metadata.

---

<div class="post-metadata">

### Author: ![SofiaCV](https://discourse.itk.org/user_avatar/discourse.itk.org/sofiacv/32/2525_2.png) [@SofiaCV](https://discourse.itk.org/u/SofiaCV)
#### Post date: [February 18, 2022, 11:36am UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/7 "2022-02-18T11:36:48Z")

</div>

Thank you @matt.mccormick, for suggesting this convenient alternative!

---

<div class="post-metadata">

### Author: ![SofiaCV](https://discourse.itk.org/user_avatar/discourse.itk.org/sofiacv/32/2525_2.png) [@SofiaCV](https://discourse.itk.org/u/SofiaCV)
#### Post date: [February 22, 2022, 4:34pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/8 "2022-02-22T16:34:20Z")

</div>

Dear @matt.mccormick,

I’ve implemented your suggestion to extract a single 2D slice, using the following lines of code:

```auto
fixed_array = itk.xarray_from_image(fixed)

fixed_array_slice = fixed_array[11,:,:]

fixed_slice = itk.image_from_xarray(fixed_array_slice)

```

Unfortunately, I got the error:

```auto
ValueError: Array does not have the expected shape

```

Could it be because fixed\_array\_slice has dimensions (512, 512) and itk.image\_from\_xarray is expecting a 3D array? How can I solve this?

---

<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: [February 22, 2022, 6:33pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/9 "2022-02-22T18:33:40Z")

</div>

Does `xarray` have `expand_dims`? Or another way to do the equivalent of `fixed_array_slice = np.expand_dims(fixed_array_slice, 0)`? This assumes that `xarray` metadata is for 3D image, while `fixed_array_slice` has 2D shape.

---

<div class="post-metadata">

### Author: ![SofiaCV](https://discourse.itk.org/user_avatar/discourse.itk.org/sofiacv/32/2525_2.png) [@SofiaCV](https://discourse.itk.org/u/SofiaCV)
#### Post date: [February 22, 2022, 9:55pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/10 "2022-02-22T21:55:13Z")

</div>

Thank you @dzenanz! I’ve tried `expand_dims` and it worked correctly this way:

```auto
fixed_array_slice.expand_dims(dim = ["z"], axis = 0)

```

However, I want the image created after `image_from_xarray` to be a 2D object of dimensions (512,512), instead of the resulting (1,512,512). Is there a way to remove the z dimension from an image, converting from 3D to 2D?

---

<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: [February 22, 2022, 10:49pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/11 "2022-02-22T22:49:25Z")

</div>

`itk.extract_image_filter` reduces dimension 😃

What happens if you change  
`image_slice11 = itk.extract_image_filter(fixed, [512,512,1], [0,0,11])`  
into  
`image_slice11 = itk.extract_image_filter(fixed, [512,512,0], [0,0,11])`  
Size of the region along Z axis might need to be 0 instead of 1.

Scratch that. Looking at the [example](https://itk.org/ITKExamples/src/Filtering/ImageGrid/ProcessA2DSliceOfA3DImage/Documentation.html), you will need to provide `ExtractionRegion`, not size+index. Take a look at [CreateAnImageRegion](https://itk.org/ITKExamples/src/Core/Common/CreateAnImageRegion/Documentation.html).

---

<div class="post-metadata">

### Author: ![SofiaCV](https://discourse.itk.org/user_avatar/discourse.itk.org/sofiacv/32/2525_2.png) [@SofiaCV](https://discourse.itk.org/u/SofiaCV)
#### Post date: [February 23, 2022, 11:43am UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/12 "2022-02-23T11:43:35Z")

</div>

I have adapted the examples you’ve shared, in order to extract slice 11 from the 3D image and then set the dimensions of the image to 2D (512,512) :

```auto
indx_slice = 11
extractFilter = itk.ExtractImageFilter.New(fixed)
extractFilter.SetDirectionCollapseToSubmatrix()

# set up the extraction region [one slice]
inputRegion = fixed.GetBufferedRegion()
size = inputRegion.GetSize()
size[2] = 1 # we extract along z direction
print(size)
start = inputRegion.GetIndex()
print(start)
sliceNumber = indx_slice
start[2] = sliceNumber

Dimension = 2

size = itk.Size[Dimension]()
size.Fill(512)

index = itk.Index[Dimension]()
index.Fill(1)

RegionType = itk.ImageRegion[Dimension]
desiredRegion = RegionType()
desiredRegion.SetIndex(index)
desiredRegion.SetIndex(size)

extractFilter.SetExtractionRegion(desiredRegion)
extractFilter.Update()
fixed_slice = extractFilter.GetOutput()

```

The error I’m getting is:

```auto
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/folders/zt/fmlplr_j0054qnlh5slzkxbm0000gn/T/ipykernel_4255/3506052614.py in <module>
     32 #desiredRegion.SetIndex(start)
     33 
---> 34 extractFilter.SetExtractionRegion(desiredRegion)
     35 extractFilter.Update()
     36 fixed_slice = extractFilter.GetOutput()

TypeError: in method 'itkExtractImageFilterIF3IF3_SetExtractionRegion', argument 2 of type 'itkImageRegion3'

```

Can you help me identify what I’m doing wrong?

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)
#### Post date: [February 23, 2022, 12:34pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/13 "2022-02-23T12:34:14Z")

</div>

> [@SofiaCV](#):
>
> ```auto
> desiredRegion.SetIndex(index)
> desiredRegion.SetIndex(size)
> 
> ```

Hi @SofiaCV, little bug there, use `SetSize(size)` instead of `SetIndex(size`).

The problem you are having is that your `fixed` image is 3D but your region is 2D, and the filter cannot accept a region of different dimension.

Try something like:

```python
Dimension = fixed.GetImageDimension()
size = itk.Size[Dimension]()
size.Fill(512)
size[2] = 1
index = itk.Index[Dimension]()
index.Fill(0)
index[2] = sliceNumber

desiredRegion = itk.ImageRegion[Dimension]
desiredRegion.SetSize(size)
desiredRegion.SetIndex(index)
extractFilter.SetExtractionRegion(desiredRegion)
...

```

Not sure now if `size[2]` needs to be `1` or `0`, but try it out as @dzenan suggested.

---

<div class="post-metadata">

### Author: ![SofiaCV](https://discourse.itk.org/user_avatar/discourse.itk.org/sofiacv/32/2525_2.png) [@SofiaCV](https://discourse.itk.org/u/SofiaCV)
#### Post date: [February 28, 2022, 10:34am UTC](https://discourse.itk.org/t/regionofinterestimagefilter-to-extract-image-slice-from-volume/4787/14 "2022-02-28T10:34:20Z")

</div>

Hi @phcerdan, thank you for noticing it! Here is the code after the suggestions:

```auto
Dimension = fixed.GetImageDimension()
indx_slice = 11
extractFilter = itk.ExtractImageFilter.New(fixed)
extractFilter.SetDirectionCollapseToIdentity()

inputRegion = fixed.GetBufferedRegion()
size = inputRegion.GetSize()
size[2] = 1 # we extract along z direction
start = inputRegion.GetIndex()
sliceNumber = indx_slice
start[2] = sliceNumber

RegionType = itk.ImageRegion[Dimension]
desiredRegion = RegionType()
desiredRegion.SetIndex(start)
desiredRegion.SetSize(size)

extractFilter.SetExtractionRegion(desiredRegion)
extractFilter.Update()
fixed_slice = extractFilter.GetOutput()

```

It worked! Thank you all for your input.
