# RegionOfInterestImageFilter preserving indices

**URL:** <https://discourse.itk.org/t/regionofinterestimagefilter-preserving-indices/6228>\
**Category:** Engineering\
**Created:** [September 25, 2023, 4:53pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-preserving-indices/6228 "2023-09-25T16:53:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![GeorgeStetten](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/97f17d/32.png) [@GeorgeStetten](https://discourse.itk.org/u/GeorgeStetten)\
**Post date:** [September 25, 2023, 4:53pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-preserving-indices/6228/1 "2023-09-25T16:53:04Z")

</div>

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

---

<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:** [September 25, 2023, 4:57pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-preserving-indices/6228/2 "2023-09-25T16:57:44Z")

</div>

The ExtractImageFilter should preserve the original index and origin.

---

<div class="post-metadata">

**Author:** ![GeorgeStetten](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/97f17d/32.png) [@GeorgeStetten](https://discourse.itk.org/u/GeorgeStetten)\
**Post date:** [September 25, 2023, 5:21pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-preserving-indices/6228/3 "2023-09-25T17:21:38Z")

</div>

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

---

<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:** [September 25, 2023, 7:01pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-preserving-indices/6228/4 "2023-09-25T19:01:02Z")

</div>

Another possibility:

```cpp
output = roiFilter->GetOutput();
output->DisconnectPipeline();
output->SetRegions(regionInputImage);

```

---

<div class="post-metadata">

**Author:** ![GeorgeStetten](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/97f17d/32.png) [@GeorgeStetten](https://discourse.itk.org/u/GeorgeStetten)\
**Post date:** [September 25, 2023, 8:01pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-preserving-indices/6228/5 "2023-09-25T20:01:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GeorgeStetten](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/97f17d/32.png) [@GeorgeStetten](https://discourse.itk.org/u/GeorgeStetten)\
**Post date:** [October 3, 2023, 12:50am UTC](https://discourse.itk.org/t/regionofinterestimagefilter-preserving-indices/6228/6 "2023-10-03T00:50:56Z")

</div>

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

---

<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:** [October 3, 2023, 1:04pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-preserving-indices/6228/7 "2023-10-03T13:04:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GeorgeStetten](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/97f17d/32.png) [@GeorgeStetten](https://discourse.itk.org/u/GeorgeStetten)\
**Post date:** [October 3, 2023, 1:31pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-preserving-indices/6228/8 "2023-10-03T13:31:00Z")

</div>

## 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();

```

---

<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:** [October 3, 2023, 1:50pm UTC](https://discourse.itk.org/t/regionofinterestimagefilter-preserving-indices/6228/9 "2023-10-03T13:50:53Z")

</div>

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](https://itk.org/Doxygen/html/classitk_1_1ExpandImageFilter.html)
