# Why ResampleImageFilter is slow?

**URL:** <https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217>\
**Category:** Uncategorized\
**Created:** [August 21, 2018, 1:23pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217 "2018-08-21T13:23:34Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [August 30, 2018, 2:39pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/21 "2018-08-30T14:39:16Z")

</div>

Well done, @grothausmann.roman!

Good idea to exclude `itk::SpecialCoordinatesImage`.

We can switch over the interpolator type to apply the appropriate padding, i.e. 1 for nearest neighbor, 2 for linear, etc. It can be applied with [`PadByRadius`](https://itk.org/Doxygen/html/classitk_1_1ImageRegion.html#a9f5dfc41ecf00b5a9d20ff36c362a10f) followed by [`Crop`](https://itk.org/Doxygen/html/classitk_1_1ImageRegion.html#ace7e0498fd5434e808036febf142ff7d) with the `LargestPossibleRegion` of the input.

As was done with `itk::WarpImageFilter`, we should check all corners, instead of just the “lower-left” and “upper-right” corners. Consider this case, for example:

![AffineRotated](https://discourse.itk.org/uploads/default/original/1X/310d4ad39cb6e8a2bc546e6ae2dde4a9a28347c3.png)

The lack of a regard in `WarpImageFilter` for the interpolator could be a bug. We should recommend using `itk::ResampleImageFilter` in the `WarpImageFilter` documentation and even consider deprecating it if `ResampleImageFilter` can do the job of `WarpImageFilter`. We should focus our efforts on optimizing and maintaining `ResampleImageFilter`…

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [August 30, 2018, 2:52pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/22 "2018-08-30T14:52:25Z")

</div>

Thanks @matt.mccormick for your feedback, I agree and will try to get that realized next week.

---

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [September 7, 2018, 12:24pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/23 "2018-09-07T12:24:15Z")

</div>

Triggered by this thread, I also had a closer look at `itk::ResampleImageFilter`, and found a possible way to improve its performance. Please review my patch: [PERF: Made ResampleImageFilter::CastPixelWithBoundsChecking faster](http://review.source.kitware.com/#/c/23696)

My colleague Denis (@denis.p.shamonin) tested the patch with the benchmark he is currently working on, and observed more than 15% reduction of the runtime duration of a `ResampleImageFilter::Update()` call, for 3D images.

With 2D images we observed even more than 20% reduction of the runtime duration, e.g., by running the following example, using Visual Studio 2015 x64 Release:

```
#include <itkResampleImageFilter.h>
#include <chrono>
#include <iostream>

int main()
{
  using ImageType = itk::Image<double>;
  const auto image = ImageType::New();
  const ImageType::SizeType size = { { 25000, 25000 } };
  const ImageType::RegionType region{ size };
  image->SetLargestPossibleRegion(region);
  image->SetBufferedRegion(region);
  image->Allocate(true);

  const auto resampleImageFilter =
      itk::ResampleImageFilter<ImageType, ImageType>::New();
  resampleImageFilter->SetInput(image);
  resampleImageFilter->SetSize(size);

  using namespace std::chrono;
  const auto timePointBeforeUpdate = high_resolution_clock::now();

  resampleImageFilter->Update();
  
  const auto timePointAfterUpdate = high_resolution_clock::now();
  const auto durationSeconds =
    duration_cast<duration<double>>(timePointAfterUpdate - timePointBeforeUpdate);

  std::cout << "Duration: " << durationSeconds.count() << " seconds" << std::endl;
}

```

So please check [http://review.source.kitware.com/#/c/23696](http://review.source.kitware.com/#/c/23696)

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [September 7, 2018, 3:50pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/24 "2018-09-07T15:50:20Z")

</div>

> [@Niels\_Dekker](#):
>
> So please check [http://review.source.kitware.com/#/c/23696](http://review.source.kitware.com/#/c/23696)

This is awesome! 🎸👏

Do you think we can improve performance further SIMD operations via an interface like [xsimd](https://github.com/QuantStack/xsimd)?

---

<div class="post-metadata">

**Author:** ![denis.p.shamonin](https://discourse.itk.org/user_avatar/discourse.itk.org/denis.p.shamonin/32/112_2.png) [@denis.p.shamonin](https://discourse.itk.org/u/denis.p.shamonin)\
**Post date:** [September 18, 2018, 9:48am UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/25 "2018-09-18T09:48:10Z")

</div>

Hi @dzenanz, @matt.mccormick, @blowekamp

I’ve created the benchmarking for the ITK ResampleImageFilter: [Resample benchmark](https://github.com/dpshamonin/ITKPerformanceBenchmarking/blob/master/examples/Filtering/ResampleBenchmark.cxx)

The resample benchmark includes combinations of image dimensions, interpolators, extrapolators, transforms and their combinations and etc. Therefore I’ve added some common registrations scenarios for the benchmarking in the [CMakeLists](https://github.com/dpshamonin/ITKPerformanceBenchmarking/blob/master/examples/Filtering/CMakeLists.txt) using the **add\_resample\_benchmark** macro.

You can still check if you would like to benchmark different scenarios.

In general benchmark is complete and has all the things I want to know about its performance. The documentation included, it is self contained and I would like to keep it this way.

Please check the [Pull request](https://github.com/InsightSoftwareConsortium/ITKPerformanceBenchmarking/pull/56)

Regards, Denis

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [September 18, 2018, 10:09pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/26 "2018-09-18T22:09:46Z")

</div>

Awesome, @denis.p.shamonin! Looks fantastic.

---

<div class="post-metadata">

**Author:** ![warfield](https://discourse.itk.org/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@warfield](https://discourse.itk.org/u/warfield)\
**Post date:** [September 20, 2018, 6:58pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/27 "2018-09-20T18:58:22Z")

</div>

That looks great!  
Did you come to any conclusion about what is slow and what is fast ?

I read the code, but I didn’t see if you published any benchmarks using it ?

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [September 25, 2018, 12:43pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/28 "2018-09-25T12:43:11Z")

</div>

Many thanks @dzenanz for this hint. For just exercising this test I run `ctest -R ResampleImageTest2` (as described here: [https://itk.org/pipermail/insight-users/2014-January/049528.html](https://itk.org/pipermail/insight-users/2014-January/049528.html)), is that still correct?  
For the simple case of just scaling as in the ResampleImageTests my changes to the itkResampleImageFilter do yield the same results when using streaming but for my own 3D test data the third axis is extended by one slice. So I guess I should extend the new tests into at least 3D. Is there any test for itkResampleImageFilter that testes its performance on 3D input?

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [September 25, 2018, 1:28pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/29 "2018-09-25T13:28:27Z")

</div>

Folowing @dzenanz idea to reuse ResampleImageTest2 I created a streaming version of it: ResampleImageTest2s. In commit

> <https://github.com/romangrothausmann/ITK/commit/fac74aab1dbe44cdaefb72977070921404d7a06a>

  
of branch [test\_RSI-SDI](https://github.com/InsightSoftwareConsortium/ITK/compare/master...romangrothausmann:test_RSI-SDI) the verification that streaming was used leads to the result that it was not, i.e. `Streamed pipeline was executed 1 times which was not the expected number 8 of times.`  
Is the chosen approach wrong?

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [September 25, 2018, 8:03pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/30 "2018-09-25T20:03:12Z")

</div>

@grothausmann.roman we may need to write to a MetaImage file, `.mha` extension, since this is one of the file output formats that supports streaming.

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [September 26, 2018, 9:27am UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/31 "2018-09-26T09:27:45Z")

</div>

Many thanks @matt.mccormick for that hint, had not thought about this in the heat of the action;-) It seems that was the problem, but I have a hard time to get the newly created MHAs into the TestingData “system”. The newest instructions I could find seem to be these:  
[https://itk.org/Wiki/ITK/How\_to\_make\_a\_Release#Archive\_ExternalData](https://itk.org/Wiki/ITK/How_to_make_a_Release#Archive_ExternalData)  
but how to test the tests before doing any uploading? Basically I am stuck at how to populate Testing/Data/Baseline/BasicFilters/ based on the commited MD5-file (manually created) and the (manually copied) e.g. ExternalData/Objects/MD5/758cd1949c26ff52d03d8036c5c629de.

---

<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 26, 2018, 12:32pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/32 "2018-09-26T12:32:25Z")

</div>

I think that newest instructions are [here](https://itk.org/Wiki/ITK/Git/Develop/Data). The link you gave are instructions for creating a release. Those are old instructions ITK maintainers have been writing for themselves - the releases are done infrequently and humans are forgetful creatures 😃

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [September 26, 2018, 12:42pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/33 "2018-09-26T12:42:15Z")

</div>

Trying to use the code of ImageAlgorithm::EnlargeRegionOverBox for this, I am facing the problem that an `#include "itkIdentityTransform.h"` in `itkImageAlgorithm.hxx` leads to a `fatal error: itkIdentityTransform.h: No such file or directory`. Why is it not found? Are `itkImageAlgorithm.hxx` and `itkIdentityTransform.h` in different “levels”?

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [September 26, 2018, 1:23pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/34 "2018-09-26T13:23:38Z")

</div>

Many thanks @dzenanz, got it worked out with those instructions, at least locally. Oddly, in the current state the non-linear transform test case also apparently succeeds in streaming, which I was expecting not to be the case. Is [`monitor->VerifyAllInputCanStream(1)`](https://github.com/InsightSoftwareConsortium/ITK/commit/71a665c5a68ca7d9b11984d8be0b2fa947534723#diff-8aa2bf916025440064f4ee778c16cebcR205) not the way to test if streaming failed? It reports:  
`Streamed pipeline was executed 8 times which was not the expected number 1 of times.`

---

<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 26, 2018, 3:13pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/35 "2018-09-26T15:13:00Z")

</div>

`itkIdentityTransform.h` is in module `ITKTransform`, whereas `itkImageAlgorithm.hxx` is in `ITKCommon`. As Common is the most basic module, it cannot depend on other modules - all the other modules depend on it. However, Common’s tests can, and do depend on `ITKTransform`. Yes, you have to take care of such things when changing low level plumbing 😃

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [October 22, 2018, 1:53pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/36 "2018-10-22T13:53:38Z")

</div>

I’m trying to create a test that compares the output of ResampleImageFilter without streaming with the result when streaming is employed. To avoid streaming limitations of a specific file format reader, I create and use a [ramp image](https://github.com/romangrothausmann/ITK/blob/11067874f2f6b66a03b72ae5571a6955bf92acf6/Modules/Filtering/ImageGrid/test/itkResampleImageTest7.cxx#L69-L74). However, [setting the number of stream divisions to 1](https://github.com/romangrothausmann/ITK/blob/11067874f2f6b66a03b72ae5571a6955bf92acf6/Modules/Filtering/ImageGrid/test/itkResampleImageTest7.cxx#L99-L117) leads to the error  
`PipelineMonitorImageFilter (0x55b0de6a2740): The input filter's BufferedRegion is not contained by LargestPossibleRegion`  
Setting it to [8 divisions](https://github.com/romangrothausmann/ITK/blob/11067874f2f6b66a03b72ae5571a6955bf92acf6/Modules/Filtering/ImageGrid/test/itkResampleImageTest7.cxx#L122-L140) then leads to the error:  
`Streamed pipeline was executed 0 times which was not the expected number 8 of times.`  
What could be the problem here?  
How to test that streaming did not take place?

PS: In [itkWarpImageFilterTest2.cxx](https://github.com/InsightSoftwareConsortium/ITK/blob/71a665c5a68ca7d9b11984d8be0b2fa947534723/Modules/Filtering/ImageGrid/test/itkWarpImageFilterTest2.cxx#L117-L138) the filter is executed twice (with a different displacement field) but the outputs of each run are not disconnected from the pipeline (`DisconnectPipeline`), so to my understanding `result1` and `result2` point to the very same output and will always match. Is that intended?

---

<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 22, 2018, 2:07pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/37 "2018-10-22T14:07:13Z")

</div>

> [@grothausmann.roman](#):
>
> However, [setting the number of stream divisions to 1](https://github.com/romangrothausmann/ITK/blob/11067874f2f6b66a03b72ae5571a6955bf92acf6/Modules/Filtering/ImageGrid/test/itkResampleImageTest7.cxx#L99-L117) leads to the error  
> `PipelineMonitorImageFilter (0x55b0de6a2740): The input filter's BufferedRegion is not contained by LargestPossibleRegion`

I would suggest compiling in Debug mode, and turning PipelineMonitorImageFilter-\>DebugOn(). This will enable the monitor filter to print a bunch of debugging information to help diagnose the problem. You should also look at the [source](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/TestKernel/include/itkPipelineMonitorImageFilter.hxx#L104-L108) for the error message.

> [@grothausmann.roman](#):
>
> Setting it to [8 divisions](https://github.com/romangrothausmann/ITK/blob/11067874f2f6b66a03b72ae5571a6955bf92acf6/Modules/Filtering/ImageGrid/test/itkResampleImageTest7.cxx#L122-L140) then leads to the error:  
> `Streamed pipeline was executed 0 times which was not the expected number 8 of times.`  
> What could be the problem here?

Looking at the code in your test, it appears that you are only changing the number of streaming divisions. You are not modifying the PipelineMonitorImageFilter, nor the ResampleImageFilter. Since both these filters have produced outputs, and the modify time of the output is new then the filters, neither on of these filters needs to execute.

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [October 22, 2018, 2:33pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/38 "2018-10-22T14:33:08Z")

</div>

Many thanks @blowekamp for your quick reply and for pointing out the source for the second problem. What would be the best way to enforce re-execution of the ResampleImageFilter without modifying any of its processing settings? Basically I want it to execute exactly as in the first run except that it should be exercised with streaming.

---

<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 22, 2018, 2:38pm UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/39 "2018-10-22T14:38:56Z")

</div>

Just update the modified time with a call to [itk::Object::Modified()](https://itk.org/Doxygen/html/classitk_1_1Object.html#a33101e349626ac58ced6682b061d8277).

---

<div class="post-metadata">

**Author:** ![grothausmann.roman](https://discourse.itk.org/user_avatar/discourse.itk.org/grothausmann.roman/32/582_2.png) [@grothausmann.roman](https://discourse.itk.org/u/grothausmann.roman)\
**Post date:** [October 23, 2018, 9:03am UTC](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/40 "2018-10-23T09:03:39Z")

</div>

Many thanks Brad for that hint, which did the trick. However, for `numStreamDiv= 8` the pipeline is now executed only `1 time` but not multiple times.  
Also, I’m still stuck on how to successfully not stream with the monitor-streamer pipeline without an image reader. I rebased my tests onto v4.13.1 [(daab8e5e5)](https://github.com/romangrothausmann/ITK/commit/daab8e5e5e118a73b00502d9923cf702ae481403) to make sure my changes to ResampleImageFilter do not cause the problem, but even with the plain ResampleImageFilter of v4.13.1 I get `The input filter's BufferedRegion is not contained by LargestPossibleRegion` and the RequestedRegion size is empty [0, 0].

Is it expected that the monitor-streamer pipeline does not work without a reader-source that can stream?

The only test that I could find that uses `SetNumberOfStreamDivisions( 1 )` uses a writer (not the StreamingImageFilter) and checks for success without PipelineMonitorImageFilter (itkVTKImageIOStreamTest.cxx).

[Previous page](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217.md?page=1)

[Next page](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217.md?page=3)
