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.
It fails at the last statement:
writer->Update()
Input image 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.