# Custom border extrapolation of ShapedImageNeighborhoodRange by ImageNeighborhoodPixelAccessPolicy

**URL:** <https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879>\
**Category:** Engineering\
**Created:** [April 30, 2018, 6:02pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879 "2018-04-30T18:02:39Z")\
**Posts on this page:** 1\
**Showing post:** 29

<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:** [November 7, 2018, 6:31pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/29 "2018-11-07T18:31:11Z")

</div>

> [@Niels\_Dekker](#):
>
> > Using Visual C++ 2017 64-bit Release, I observed that the range-based neighborhood iteration implemented by [http://review.source.kitware.com/#/c/23795/7](http://review.source.kitware.com/#/c/23795/7) can be more than twice as fast as the “old-school” iteration 😀
> 
> For the record, it was only _ **after** _ I wrote those words, that I found a way to significantly improve the performance of the “old-school” iteration (using Visual C++ 2017 Release): [PERF: Made ConstNeighborhoodIterator::GetPixel(i) much faster](http://review.source.kitware.com/#/c/23813/)

@Niels_Dekker Well done! 👏 ✨ To add more data 🖖 to the Visual C++ 2017 Release results, here are some metrics created on Linux with GCC 7.3.0 and ICC (the Intel Compiler) 19.0.0.120. These results are created for this [ultrasound application](https://github.com/KitwareMedical/ITKUltrasound/pull/98) and [your Gist](https://gist.github.com/N-Dekker/eb94ba817b2a16e75d5e7c3fe54ec66f).

 ![ITKUltrasound%20Xcorr](https://discourse.itk.org/uploads/default/original/1X/37e4e192b056aec75a411cfb58b08bce48d1db1d.png)

They are very interesting! All we can conclude is that neither implementation is consistently faster across compilers! 😲

That said, the potential of these new image range classes to be used with STL algorithms is quite compelling because of potential future benefits:

- [C++17 Parallel STL execution policies](https://en.cppreference.com/w/cpp/experimental/parallelism)
- [Intel `parallelstl` TBB-backed implementation of the above](https://github.com/intel/parallelstl)
- [OpenCL / SYCL GPU / FPGA](https://www.khronos.org/sycl/) implementation of parallel STL via [ComputeCPP](https://www.codeplay.com/products/computesuite/computecpp) or [triSYCL](https://github.com/Xilinx/triSYCL)
- [AMD support](https://github.com/parmance/par_offload)
- [Potential NVidia support](https://www.phoronix.com/scan.php?page=news_item&px=NVIDIA-Interest-Parallel-Algos)

Granted that we would have to figure out a good way to control data synchronization in the GPU case.

> [@phcerdan](#):
>
> The usage:
> 
> ```auto
> // The equivalent in ITK 5.0 + this patch + this
> // Using itk::Experimental::ShapedImageNeighborhoodRange:
> using RangeType = ShapedImageNeighborhoodRange< ImageType,
> ConstantBoundaryImageNeighborhoodPixelAccessPolicy<ImageType> >;
> 
> RangeType range{ *image, location, offsets}; // same constructor as before
> range->GetReferencePixelAccessPolicy()->SetConstant(paddingValue); // we set the value at run time on the policy
> 
> ```

+1 to this API – setting the constant value separately from the range initialization is easier to follow, in my opinion. That said, this is not implemented, and maybe we could implement it in the future if it is really compelling. As an `itk::Experimental` namespaced feature, we should keep experimenting with nice things!

Let’s merge [the patch](https://github.com/InsightSoftwareConsortium/ITK/pull/105), which @hjmjohnson has kindly migrated to GitHub! 🐙

---

_[View the full topic](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879)._
