# Could not run basic example code from ITK Resample Image filter

**URL:** https://discourse.itk.org/t/could-not-run-basic-example-code-from-itk-resample-image-filter/4071
**Category:** Beginner Questions
**Tags:** filter
**Created:** [April 25, 2021, 1:18pm UTC](https://discourse.itk.org/t/could-not-run-basic-example-code-from-itk-resample-image-filter/4071 "2021-04-25T13:18:35Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![fonis](https://discourse.itk.org/user_avatar/discourse.itk.org/fonis/32/2046_2.png) [@fonis](https://discourse.itk.org/u/fonis)
#### Post date: [April 25, 2021, 1:18pm UTC](https://discourse.itk.org/t/could-not-run-basic-example-code-from-itk-resample-image-filter/4071/1 "2021-04-25T13:18:35Z")

</div>

Using ITK v5.2.

The basic resample filter example fails.  
Error: “Requested region is (at least partially) outside the largest possible region.”

The code below is basically a cleaned up version of [ResampleImageFilter.cxx](https://itk.org/Doxygen/html/Examples_2Filtering_2ResampleImageFilter_8cxx-example.html).  
It fails at the last statement:

> writer-\>Update()

Input image [BrainProtonDensitySlice.png](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/Data/BrainProtonDensitySlice.png) is of size 217x181.  
Specified output size is 300x300 set in the filter by calling:

> filter-\>SetSize().

What am I missing? Filter output size and input image size can be different, right?

Any help will be appreciated.

```
        #include "itkResampleImageFilter.h"
        #include "itkAffineTransform.h"
        #include "itkNearestNeighborInterpolateImageFunction.h"

        #include "itkImage.h"
        #include "itkImageFileReader.h"
        #include "itkImageFileWriter.h"

        #include "itkPNGImageIO.h"

        // image dim and object types
        constexpr unsigned int Dimension = 2;
        using InputPixelType = unsigned char;
        using OutputPixelType = unsigned char;
        using InputImageType = itk::Image<InputPixelType, Dimension>;
        using OutputImageType = itk::Image<OutputPixelType, Dimension>;
        using FilterType = itk::ResampleImageFilter<InputImageType, OutputImageType>;
        using TransformType = itk::AffineTransform<double, Dimension>;
        using InterpolatorType = itk::NearestNeighborInterpolateImageFunction<InputImageType, double>;

        //create filter
        auto filter = FilterType::New();

        auto transform = TransformType::New();
        transform->SetIdentity();
        filter->SetTransform(transform);

        auto interpolator = InterpolatorType::New();
        filter->SetInterpolator(interpolator);

        filter->SetDefaultPixelValue(0); //values outside the extent of input image

        const double spacing[Dimension] = { 1.0, 1.0 };
        filter->SetOutputSpacing(spacing);

        const double origin[Dimension] = { 0.0, 0.0 };
        filter->SetOutputOrigin(origin);

        InputImageType::DirectionType direction;
        direction.SetIdentity();
        filter->SetOutputDirection(direction);

        // set image input 
        auto imageIOTypePNG = itk::PNGImageIO::New();

        using ReaderType = itk::ImageFileReader<InputImageType>;
        using WriterType = itk::ImageFileWriter<OutputImageType>;

        auto reader = ReaderType::New();
        reader->SetImageIO(imageIOTypePNG);
        reader->SetFileName(<input_image_path>);

        auto writer = WriterType::New();
        writer->SetImageIO(imageIOTypePNG);
        writer->SetFileName(<output_image_path>);

        InputImageType::SizeType size;
        size[0] = 300;
        size[1] = 300;
        filter->SetSize(size);

        // connect input reader, filter and writer
        filter->SetInput(reader->GetOutput());
        writer->SetInput(filter->GetOutput());

        //default output: exampleAction = 0
        writer->Update();

```

The problem is solved after making the following code changes.

```
filter->SetInput(reader->GetOutput());
InputImageType::SizeType size;
size[0] = 300;
size[1] = 300;
filter->SetSize(size);
filter->UpdateLargestPossibleRegion();

```

I would like to make additional request for a general comment on the related ITK concepts like “Requested region”, when to call Update() and when to call UpdateLargestPossibleRegion() etc.

---

<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: [April 26, 2021, 8:57pm UTC](https://discourse.itk.org/t/could-not-run-basic-example-code-from-itk-resample-image-filter/4071/2 "2021-04-26T20:57:20Z")

</div>

This is a cross-post from [StackOverflow](https://stackoverflow.com/questions/67242760/could-not-run-basic-example-code-from-itk-resample-image-filter/67269507).

Adding `filter->Update();` before `writer->Update();` avoids the crash. I tested with current master (5.2+).

Running the `ResampleImageFilter.cxx` example works (aside from a minor [argument count comparison bug](https://github.com/InsightSoftwareConsortium/ITK/pull/2513)). Maybe `SetImageIO` somehow interferes with the pipeline update? If so, please report this as a [bug](https://github.com/InsightSoftwareConsortium/ITK/issues).

Side note: you don’t need to explicitly specify an IO, unless you want to use IO’s special features.
