RegionOfInterestImageFilter preserving indices

I am using RegionOfInterestImageFilter and want to preserve the indices of the original image in the resampled image. The default seems to be that the sampled region starts at (0,0,0). I have tried

roiFilter->SetRegionOfInterest(regionInputImage);
roiFilter->GetOutput()->SetRegions(regionInputImage);

giving the same region to the input and output of the filter, but the indices still starts at (0,0,0). How do I get the filter to keep the original indices?

Thanks,

George Stetten

The ExtractImageFilter should preserve the original index and origin.

1 Like

Wow! That was fast! It worked! Thanks very much!
George

1 Like

Another possibility:

output = roiFilter->GetOutput();
output->DisconnectPipeline();
output->SetRegions(regionInputImage);
1 Like

ExtractImageFilter seems to work… I can see the same pixel value at the same index for its input and output. My problem now is that I take the output and feed it to a ResampleImageFilter, using the same region parameters (and keeping the spacing the same for now). The regions all seem to match up, but the ResampleImageFilter doesn’t show the data at that same index, just the default pixel value.

Problem solved with the ResampleImageFilter. I needed to explicitly set the origin and direction of the filter output.

2 Likes

Sharing your final code fragment might help someone in the future.

While incomplete, here’s basically how I used ExtractImageFilterType to select a region and then ResampleFilterType to resample at twice the resolution.

//Create an extract image filter, whose output can be resampled
using ExtractImageFilterType =
        itk::ExtractImageFilter<InputImageType, InputImageType>;
ExtractImageFilterType::Pointer m_extractImageFilter;
m_extractImageFilter = ExtractImageFilterType::New();
m_extractImageFilter->SetInput(m_itkInputImage);
m_extractImageFilter->SetExtractionRegion(regionInputImage);
m_extractImageFilter->Update();

//Create a resampler and interpolator at twice the resolution
unsigned short resamplingFactor = 2;

using ResampleFilterType =
        itk::ResampleImageFilter<InputImageType, InputImageType>;
ResampleFilterType::Pointer m_resampler;
for (unsigned int i = 0; i < inputImageNumberOfDimensions; ++i)
{
    spacingResamplerOutput[i]      = m_itkInputImage->GetSpacing()[i] / resamplingFactor;
    startIndexResamplerOutput[i]   = startIndexInputImage[i] * resamplingFactor;
    sizeResamplerOutput[i]         = regionInputImage.GetSize()[i] * resamplingFactor;
}

m_resampler = ResampleFilterType::New();
m_resampler->SetInput(m_extractImageFilter->GetOutput());
using InterpolatorType =
        itk::LinearInterpolateImageFunction<InputImageType, double>;
InterpolatorType::Pointer m_interpolator;
m_interpolator = InterpolatorType::New();
m_resampler->SetInterpolator(m_interpolator);
m_resampler->SetOutputSpacing(spacingResamplerOutput);
m_resampler->SetOutputStartIndex(startIndexResamplerOutput);
m_resampler->SetSize(sizeResamplerOutput);

//These last two lines didn't seem needed, but were what finally made it all work.
m_resampler->SetOutputOrigin(m_itkInputImage->GetOrigin());
m_resampler->SetOutputDirection(m_itkInputImage->GetDirection());

m_resampler->Update();
2 Likes

If you just want to resample and increase the size of the image by an integer factor you may be interesting int the ExpandImageFilter:
https://itk.org/Doxygen/html/classitk_1_1ExpandImageFilter.html

1 Like