# Extract multiple 2D slices from a 3D volume.

**URL:** https://discourse.itk.org/t/extract-multiple-2d-slices-from-a-3d-volume/5058
**Category:** Beginner Questions
**Tags:** itk, itkv5, cpp
**Created:** [June 6, 2022, 1:01pm UTC](https://discourse.itk.org/t/extract-multiple-2d-slices-from-a-3d-volume/5058 "2022-06-06T13:01:32Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mzukovec](https://discourse.itk.org/user_avatar/discourse.itk.org/mzukovec/32/2732_2.png) [@mzukovec](https://discourse.itk.org/u/mzukovec)
#### Post date: [June 6, 2022, 1:01pm UTC](https://discourse.itk.org/t/extract-multiple-2d-slices-from-a-3d-volume/5058/1 "2022-06-06T13:01:32Z")

</div>

Hi all.

I can’t solve the image extraction error (`invalidRequestedRegionError`) if I try to extract 2 different slices using the same `ExtractImageFilter` from a given volume.  
In my program, I want to use a single `ExtractImageFilter` to dynamically extract individual slices from a 3D DICOM image that I load at the beginning.

The minimal reproducible example is as follows:

```auto
#include "itkImage.h"
#include "itkExtractImageFilter.h"

using PixelType = int;
using ImageType = itk::Image<PixelType, 3>;

int main()
{
    // Create empty 3D image
    ImageType::IndexType start;
    start[0] = 0;
    start[1] = 0;
    start[2] = 0;

    ImageType::SizeType size;
    size[0] = 512;
    size[1] = 512;
    size[2] = 512;

    ImageType::RegionType region;
    region.SetSize(size);
    region.SetIndex(start);

    // Allocate empty image
    auto image = ImageType::New();
    image->SetRegions(region);
    image->Allocate();
    image->Update();

    // Create slice extraction filter
    auto sliceExtractor = itk::ExtractImageFilter<ImageType, ImageType>::New();
    sliceExtractor->SetInput(image);

    // Extract single slice [z = 10].
    start[2] = 10;
    size[2] = 1; 
    ImageType::RegionType inputRegion = image->GetBufferedRegion();
    inputRegion.SetSize(size);
    inputRegion.SetIndex(start);
    sliceExtractor->SetExtractionRegion(inputRegion);
    sliceExtractor->Update();
    auto slice_0 = sliceExtractor->GetOutput();

    // Extract another slice [z = 50].
    start[2] = 50;
    inputRegion.SetSize(size);
    inputRegion.SetIndex(start);
    sliceExtractor->SetExtractionRegion(inputRegion);
    sliceExtractor->Update(); // Error happens here.
    auto slice_1 = sliceExtractor->GetOutput();

    return 0;
}

```

I’m compiling with VS2022 and using the ITK 5.0.0.

---

<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: [June 6, 2022, 3:13pm UTC](https://discourse.itk.org/t/extract-multiple-2d-slices-from-a-3d-volume/5058/2 "2022-06-06T15:13:22Z")

</div>

If you add `UpdateLargestPossibleRegion` before or instead of `Update`, the error goes away:

```cpp
  sliceExtractor->UpdateLargestPossibleRegion(); // We need to update the largest region first
  sliceExtractor->Update(); // else error happens here.

```

The error happens because the new requested region and old largest region have the same size but different starting index ([code](https://github.com/InsightSoftwareConsortium/ITK/blob/v5.0.0/Modules/Core/Common/include/itkImageBase.hxx#L415-L416)), so there is no overlap.

I am not sure whether this is a limitation or a bug. @blowekamp might have more background.

---

<div class="post-metadata">

### Author: ![mauigna06](https://discourse.itk.org/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mauigna06](https://discourse.itk.org/u/mauigna06)
#### Post date: [June 11, 2022, 1:36pm UTC](https://discourse.itk.org/t/extract-multiple-2d-slices-from-a-3d-volume/5058/3 "2022-06-11T13:36:32Z")

</div>

I thimk you can also do it with sitk with less code

Hope it helps

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [June 13, 2022, 3:59pm UTC](https://discourse.itk.org/t/extract-multiple-2d-slices-from-a-3d-volume/5058/4 "2022-06-13T15:59:44Z")

</div>

> [@dzenanz](#):
>
> I am not sure whether this is a limitation or a bug. @blowekamp might have more background.

This is a limitation. This first time `Update` is called the DataObject’s requested region is automatically update to the largest possible region if not set or zero sized. The `UpdateLargestPosslbeRegion` method explicitly sets the requested region to the largest possible. I have previously proposed that ITK’s examples should use the `UpdateLargestPossibleRegion` over the prior as it is more explicit.

For the curious the relevant code is here:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/f3c12edaf9cef07dbc34107e1a8aec9859204116/Modules/Core/Common/include/itkImageBase.hxx#L214-L243>
