# 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:** 20\
**Page:** 1

<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:** [April 30, 2018, 6:02pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/1 "2018-04-30T18:02:40Z")

</div>

Hereby I would like explain the idea behind the patch I submitted last Saturday, [ENH: Added ImageNeighborhoodPixelAccessPolicy for custom border extrapolation](http://review.source.kitware.com/#/c/23366/)

I guess you have seen, last week, the [ShapedImageNeighborhoodRange](https://github.com/Kitware/ITK/commit/fd8b105a2fb0f51eb993145fb02db91226607789) class for iteration over a neighborhood of pixels was merged onto the master branch of ITK. ShapedImageNeighborhoodRange has one specific way to deals with neighborhood pixel locations outside the image boundaries: it replicates the image border, just like [itk::ZeroFluxNeumannBoundaryCondition](https://itk.org/Doxygen/html/classitk_1_1ZeroFluxNeumannBoundaryCondition.html). This border extrapolation method was implemented by its [operator\*()](https://github.com/Kitware/ITK/blob/fd8b105a2fb0f51eb993145fb02db91226607789/Modules/Core/Common/include/itkShapedImageNeighborhoodRange.h#L380) overload.

The new patch allows ITK users and developers to specify a different border extrapolation approach. It adds an extra template parameter, _TImageNeighborhoodPixelAccessPolicy_, to ShapedImageNeighborhoodRange, which specifies the “pixel access policy”: it specifies both the access to the pixel data and the border extrapolation method.

By default, _BorderReplicatingImageNeighborhoodPixelAccessPolicy_ is used. But anyone can provide a custom policy class, as long as it has a GetPixelValue, a SetPixelValue, and a constructor, as follows:

```
template <typename TImage> class MyPolicy
{
  using NeighborhoodAccessorFunctorType = typename TImage::NeighborhoodAccessorFunctorType;
  using PixelType = typename TImage::PixelType;
  using InternalPixelType = typename TImage::InternalPixelType;
  using ImageDimensionType = typename TImage::ImageDimensionType;
  static constexpr ImageDimensionType ImageDimension = TImage::ImageDimension;

public:
  MyPolicy(
    const Size<ImageDimension> & imageSize,
    const OffsetType& offsetTable,
    const NeighborhoodAccessorFunctorType& neighborhoodAccessor,
    const IndexType& pixelIndex) ITK_NOEXCEPT

  PixelType GetPixelValue(
    const InternalPixelType* const imageBufferPointer) const ITK_NOEXCEPT;

  void SetPixelValue(
    InternalPixelType* const imageBufferPointer,
    const PixelType& pixelValue) const ITK_NOEXCEPT;
};

```

Please have a look at how BorderReplicatingImageNeighborhoodPixelAccessPolicy was implemented: [http://review.source.kitware.com/#/c/23366/4/Modules/Core/Common/include/itkBorderReplicatingImageNeighborhoodPixelAccessPolicy.h](http://review.source.kitware.com/#/c/23366/4/Modules/Core/Common/include/itkBorderReplicatingImageNeighborhoodPixelAccessPolicy.h)

In a similar way, other boundary conditions could also be implemented. With zero-cost runtime overhead, because the selection of the policy class is done at compile-time 🙂

Of course, I think it would be nice if this patch could still be included with ITK 5.0.0. But I can understand it it’s now too late in the game. It also depends on whether users would want to have this feature quickly. What do you think?

[Update, May 4, 2018: adapted text to [Patch Set 4](http://review.source.kitware.com/#/c/23366/4).]

---

<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:** [April 30, 2018, 6:49pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/2 "2018-04-30T18:49:17Z")

</div>

Regarding the reply from @matt.mccormick at at [http://review.source.kitware.com/#/c/23366/3](http://review.source.kitware.com/#/c/23366/3)

I chose the first part of the class name _BorderReplicatingImageNeighborhoodPixelAccessPolicy_, based on the term “border replication”, that is used by OpenCV (which defines an enum [BORDER\_REPLICATE](https://docs.opencv.org/3.4.1/d2/de8/group __core__ array.html#ga209f2f4869e304c82d07739337eae7c5), described at their [tutorial](https://docs.opencv.org/3.4.0/dc/da3/tutorial_copyMakeBorder.html)) and MATLAB (whose _imfilter_ function has a [border replication option](https://blogs.mathworks.com/steve/2007/06/15/border-replication-option-of-imresize/)). On the other hand, I never really understood the term “zero-flux Neumann boundary condition”. Do you still think this border extrapolation method is best described by “zero-flux Neumann”?

The other part of the name that I chose, _PixelAccessPolicy_ was based on the fact that it is a _policy class_, very much like the policy classes Andrei Alexandrescu presented in his book _Modern C++ Design_, [https://en.wikipedia.org/wiki/Policy-based\_design](https://en.wikipedia.org/wiki/Policy-based_design) And it specifies the pixel access, by providing a _GetPixelValue_ and a _SetPixelValue_ member function. (Internally _BorderReplicatingImageNeighborhoodPixelAccessPolicy_ uses the _NeighborhoodAccessor_ of the image, but that’s an implementation detail!)

If you still don’t like the name _BorderReplicatingImageNeighborhoodPixelAccessPolicy_ after this explanation, please let me know if you see other, more technical flaws in the design as well! Because in that case, things might need to be changed anyway… 🤔

---

<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:** [April 30, 2018, 7:40pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/3 "2018-04-30T19:40:19Z")

</div>

> [@Niels\_Dekker](#):
>
> Of course, I think it would be nice if this patch could still be included with ITK 5.0.0. But I can understand it it’s now too late in the game. It also depends on whether users would want to have this feature quickly. What do you think?

There will be other alpha’s and release candidates for ITK 5, and other opportunities past ITK 5.0.0 – we should not rush it in.

> [@Niels\_Dekker](#):
>
> I chose the first part of the class name BorderReplicatingImageNeighborhoodPixelAccessPolicy, based on the term “border replication”, that is used by OpenCV (which defines an enum BORDER\_REPLICATE, described at their tutorial) and MATLAB (whose imfilter function has a border replication option). On the other hand, I never really understood the term “zero-flux Neumann boundary condition”. Do you still think this border extrapolation method is best described by “zero-flux Neumann”?

Yes, this is zero-flux Neumann behavior. We need to consistently use ITK naming conventions and semantics within ITK to avoid confusion.

> [@Niels\_Dekker](#):
>
> The other part of the name that I chose, PixelAccessPolicy was based on the fact that it is a policy class, very much like the policy classes Andrei Alexandrescu presented in his book Modern C++ Design, [Modern C++ Design - Wikipedia](https://en.wikipedia.org/wiki/Policy-based_design) And it specifies the pixel access, by providing a GetPixelValue and a SetPixelValue member function. (Internally BorderReplicatingImageNeighborhoodPixelAccessPolicy uses the NeighborhoodAccessor of the image, but that’s an implementation detail!)

_Policy_ could be included in the name or in the comments. A lesson learned from [`itk::ImageBoundaryCondition`](https://itk.org/Doxygen/html/classitk_1_1ZeroFluxNeumannBoundaryCondition.html) is that object-oriented programming makes policy design potentially problematic after we start creating something complex. In particular, it can be easy to create incompatibilities because classes or their parents or their members were templated with different policies – but then an attempt is made to use these complex object systems together, and it causes issues. But, this could be mitigated by:

1. Do not provide a default template parameter for the proxy. This will encourage being explicit with the policy in a consistent way, and it should reduce the number of times when the policy is the default and when the policy was explicitly set to something other than the default.
2. State in the documentation that this class should only be used as a template parameter and not as an instance. Enforce this if possible, with whatever mechanisms C++ provides (remove default constructors, etc.) This will help avoid issues when a templated class is passed a pointer to a type with a different policy.

---

<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:** [May 1, 2018, 3:38pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/4 "2018-05-01T15:38:12Z")

</div>

Thanks for your reply, Matt!

Personally I think the patch [ENH: Added ImageNeighborhoodPixelAccessPolicy for custom border extrapolation](http://review.source.kitware.com/#/c/23366/3) is almost ready now, but it’s OK to me to postpone the feature until somebody actually needs it!

Honestly I always found both terms “boundary condition” and “zero-flux Neumann boundary condition” quite confusing. They seem to be from _physics_, rather than _image processing_. That’s why I searched for different terms, and found “border extrapolation” and “border replication”, which seem much clearer to me. But I guess you feel differently… no problem!

Anyway, even if the patch doesn’t make it into the next version of ITK, it can still serve as a “proof of principle”, showing that this feature can be implemented any time later, with zero run-time overhead 🙂

---

<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:** [May 1, 2018, 10:27pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/5 "2018-05-01T22:27:01Z")

</div>

> [@Niels\_Dekker](#):
>
> Honestly I always found both terms “boundary condition” and “zero-flux Neumann boundary condition” quite confusing. They seem to be from physics, rather than image processing. That’s why I searched for different terms, and found “border extrapolation” and “border replication”, which seem much clearer to me. But I guess you feel differently… no problem!

I like “border”, too, but for the codebase to be readable and understandable, consistency throughout the toolkit has to take precedent over our personal preferences.

---

<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:** [July 12, 2018, 12:33pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/6 "2018-07-12T12:33:40Z")

</div>

Update: Here’s an alternative patch, hoping to bypass our unresolved naming discussion (“border replication” versus “zero-flux Neumann”), by just leaving the default behavior unnamed:

[ENH: Allowed custom PixelAccessPolicy for ShapedImageNeighborhoodRange](http://review.source.kitware.com/#/c/23564)

Although I still think the name _BorderReplicatingImageNeighborhoodPixelAccessPolicy_ is fine (as I proposed at [http://review.source.kitware.com/#/c/23366](http://review.source.kitware.com/#/c/23366)), I find it more important to allow user customization of this policy, _one way or another_.

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [July 12, 2018, 3:16pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/7 "2018-07-12T15:16:34Z")

</div>

Hi Niels! Awesome work on the iterators, I am looking forward to using them in the wild!

> [@Niels\_Dekker](#):
>
> unresolved naming discussion (“border replication” versus “zero-flux Neumann”),

It doesn’t seem unresolved. Your pull-request has received reasoned feedback about why zero flux is preferred.

> [@Niels\_Dekker](#):
>
> by just leaving the default behavior unnamed:

Why unnamed? Why is ZeroFlux the unnamed default? If any of the boundary conditions make sense (to me) as default would be the constant boundary condition with a background pixel value, but this has the problem of specifying the background value.  
So I agree with @matt.mccormick, relying on default parameters has caused and will cause problems in future filters when they use these iterators but do not provide an interface to change the BoundaryCondition, just because there is a default the creators haven’t thought about it.

Maybe, to help with the usage, and also with the name, could we create type alias?

```auto
template<typename TImageType>
using ZeroFluxNeumannNeighborhoodRange<TImageType> = ShapedImageNeighborhoodRange<TImageType, ZeroFluxNeumannBoundaryCondition>

template<typename TImageType>
using BorderReplicatingNeighborhoodRange<TImageType> = ShapedImageNeighborhoodRange<TImageType, ZeroFluxNeumannBoundaryCondition>

template<typename TImageType>
using ConstantBoundaryNeighborhoodRange<TImageType> = ShapedImageNeighborhoodRange<TImageType, ConstantBoundaryCondition>

```

However, what if the boundary condition or boundary condition policy use run-time arguments? Like in the case of the [itk::ConstantBoundaryCondition](https://itk.org/Doxygen/html/classitk_1_1ConstantBoundaryCondition.html)?

---

<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:** [July 12, 2018, 5:01pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/8 "2018-07-12T17:01:02Z")

</div>

Thanks for your reply, Pablo!

> [@phcerdan](#):
>
> Your pull-request has received reasoned feedback about why zero flux is preferred.

Sorry, but didn’t I give a reasonable explanation on why I prefer the term “border replication”? Of course, anyone of you can rename the policy class to something you prefer (if possible, in a separate commit).

> [@phcerdan](#):
>
> However, what if the boundary condition or boundary condition policy use run-time arguments? Like in the case of the [itk::ConstantBoundaryCondition](https://itk.org/Doxygen/html/classitk_1_1ConstantBoundaryCondition.html)?

Good question. I thought about this before, allowing to user to add run-time arguments tweak the pixel access. But I felt that it could make the design of the range class too complicated. Also I wasn’t sure if it would be an important use case. Is ConstantBoundaryCondition really used like that, in practice? I mean, isn’t the _constant value_ used for ConstantBoundaryCondition usually known _at compile-time_, in practice?

By the way, I used the term _policy class_ to indicate that it is a template argument that tweaks the pixel access entirely at compile-time. I think that’s according to the definition of a policy class in C++: [Modern C++ Design - Wikipedia](https://en.wikipedia.org/wiki/Policy-based_design) If the class should also support run-time arguments, the term _policy class_ might not be entirely appropriate anymore 🤔

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [July 12, 2018, 8:25pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/9 "2018-07-12T20:25:19Z")

</div>

> [@Niels\_Dekker](#):
>
> Sorry, but didn’t I give a reasonable explanation on why I prefer the term “border replication”?

> [@Niels\_Dekker from http://review.source.kitware.com/#/c/23366/3](#):
>
> Regarding the name of the policy class that I included with the patch, can we please just agree to disagree? I have carefully chosen a class name that I think is clear and correct. Personally I wouldn’t like to rename it to something like “zero-flux Neumann boudary condition”, because I don’t understand that term well enough. But of course, you guys could just rename the class at any time, in a follow-up commit (not in my name). OK?

In my view, not really Niels, you prefer one name over the other, I can understand that and even agree that the name is easier to grasp. But you don’t seem to put any weight or importance in the reasons given by the reviewers: consistency over the toolkit. There are currently these classes, named after that boundary condition:

```bash
./Modules/Filtering/ImageGrid/include/itkZeroFluxNeumannPadImageFilter.h
./Modules/Core/Common/include/itkZeroFluxNeumannBoundaryCondition.h

```

Should we deprecate them, and rename them to `BorderReplicating`? Or should both terms live at the same time, adding extra complexity? I don’t think it is worth the pain either, but we should  
add BorderReplicating to the docs for people coming from others frameworks for sure.

These are the classes using ZeroFlux: `ack --cpp -m1 "ZeroFlux" | grep -v test `

```bash
Modules/Segmentation/LabelVoting/include/itkVotingBinaryHoleFillingImageFilter.hxx
Modules/Segmentation/LabelVoting/include/itkVotingBinaryImageFilter.hxx
Modules/Segmentation/LabelVoting/include/itkBinaryMedianImageFilter.hxx
Modules/Segmentation/LevelSetsv4/include/itkUpdateWhitakerSparseLevelSet.hxx
Modules/Segmentation/LevelSetsv4/include/itkLevelSetEquationCurvatureTerm.h
Modules/Segmentation/LevelSetsv4/include/itkUpdateShiSparseLevelSet.hxx
Modules/Segmentation/LevelSetsv4/include/itkUpdateMalcolmSparseLevelSet.hxx
Modules/Segmentation/LevelSetsv4/include/itkLevelSetEquationAdvectionTerm.h
Modules/Segmentation/LevelSetsv4/include/itkLevelSetEquationPropagationTerm.h
Modules/Segmentation/LevelSetsv4/include/itkLevelSetEquationLaplacianTerm.h
Modules/Filtering/ImageGrid/include/itkZeroFluxNeumannPadImageFilter.h
Modules/Segmentation/LevelSetsv4/include/itkBinaryImageToLevelSetImageAdaptor.hxx
Modules/Filtering/ImageGrid/include/itkZeroFluxNeumannPadImageFilter.hxx
Modules/Filtering/MathematicalMorphology/include/itkGrayscaleGeodesicDilateImageFilter.hxx
Modules/Filtering/DisplacementField/include/itkDisplacementFieldJacobianDeterminantFilter.hxx
Modules/Filtering/Denoising/include/itkPatchBasedDenoisingBaseImageFilter.h
Modules/Filtering/GPUImageFilterBase/include/itkGPUNeighborhoodOperatorImageFilter.h
Modules/Filtering/MathematicalMorphology/include/itkGrayscaleGeodesicErodeImageFilter.hxx
Modules/Filtering/Smoothing/include/itkBoxUtilities.h
Modules/Filtering/Smoothing/include/itkMeanImageFilter.hxx
Modules/Filtering/CurvatureFlow/include/itkCurvatureFlowFunction.h
Modules/Filtering/Smoothing/include/itkMedianImageFilter.hxx
Modules/Filtering/CurvatureFlow/include/itkBinaryMinMaxCurvatureFlowFunction.h
Modules/Filtering/AnisotropicSmoothing/include/itkVectorAnisotropicDiffusionFunction.hxx
Modules/Filtering/AnisotropicSmoothing/include/itkScalarAnisotropicDiffusionFunction.hxx
Modules/Filtering/Convolution/include/itkFFTConvolutionImageFilter.h
Modules/Filtering/Convolution/include/itkConvolutionImageFilter.h
Modules/Filtering/Convolution/include/itkConvolutionImageFilterBase.h
Modules/Filtering/FFT/include/itkFFTPadImageFilter.h
Modules/Filtering/DistanceMap/include/itkContourDirectedMeanDistanceImageFilter.hxx
Modules/Core/FiniteDifference/include/itkFiniteDifferenceFunction.h
Modules/Filtering/ImageFilterBase/include/itkNoiseImageFilter.hxx
Modules/Filtering/ImageGradient/include/itkGradientImageFilter.hxx
Modules/Filtering/ImageFilterBase/include/itkNeighborhoodOperatorImageFilter.h
Modules/Filtering/ImageGradient/include/itkVectorGradientMagnitudeImageFilter.hxx
Modules/Filtering/ImageGradient/include/itkGradientMagnitudeImageFilter.hxx
Modules/Filtering/CurvatureFlow/include/itkMinMaxCurvatureFlowFunction.h
Modules/Filtering/ImageFeature/include/itkZeroCrossingImageFilter.hxx
Modules/Core/TestKernel/include/itkTestingComparisonImageFilter.hxx
Modules/Filtering/ImageFeature/include/itkCannyEdgeDetectionImageFilter.hxx
Modules/Filtering/ImageFeature/include/itkSobelEdgeDetectionImageFilter.hxx
Modules/Filtering/ImageFeature/include/itkBilateralImageFilter.hxx
Modules/Filtering/ImageFeature/include/itkLaplacianSharpeningImageFilter.hxx
Modules/Filtering/ImageFeature/include/itkSimpleContourExtractorImageFilter.hxx
Modules/Filtering/ImageFeature/include/itkLaplacianImageFilter.hxx
Modules/Core/ImageFunction/include/itkMeanImageFunction.h
Modules/Core/ImageFunction/include/itkVarianceImageFunction.h
Modules/Core/ImageFunction/include/itkCovarianceImageFunction.h
Modules/Core/ImageFunction/include/itkVectorMeanImageFunction.h
Modules/Core/ImageFunction/include/itkSumOfSquaresImageFunction.h
Modules/Core/ImageFunction/include/itkScatterMatrixImageFunction.h
Modules/Core/ImageFunction/include/itkMedianImageFunction.h
Modules/Core/ImageFunction/include/itkWindowedSincInterpolateImageFunction.h
Modules/Core/Common/include/itkZeroFluxNeumannBoundaryCondition.h
Modules/Core/Common/include/itkConstNeighborhoodIterator.h
Modules/Core/Common/include/itkShapedNeighborhoodIterator.h
Modules/Core/Common/include/itkConstShapedNeighborhoodIterator.hxx
Modules/Core/Common/include/itkZeroFluxNeumannBoundaryCondition.hxx
Modules/Core/Common/include/itkNeighborhoodIterator.h
Modules/Core/Common/include/itkConstShapedNeighborhoodIterator.h
Modules/Nonunit/Review/include/itkWarpHarmonicEnergyCalculator.hxx
Modules/Filtering/ImageFeature/include/itkCannyEdgeDetectionImageFilter.h
Modules/Filtering/ImageFeature/include/itkDerivativeImageFilter.hxx
Modules/Registration/Common/include/itkGradientDifferenceImageToImageMetric.h

```

It would be cool to have the best of both approaches, consistency and a clear name. Maybe it is worth to explore a type alias, but again, not sure if it is worth, the user can just make a 1 to 1 map: `BorderReplicating <-> ZeroFluxNeumann`.

---

<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:** [July 13, 2018, 4:51pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/10 "2018-07-13T16:51:41Z")

</div>

So what name would you propose for the class that I named _BorderReplicatingImageNeighborhoodPixelAccessPolicy_ at [http://review.source.kitware.com/#/c/23366](http://review.source.kitware.com/#/c/23366) ? _ZeroFluxNeumannBoundaryConditionPixelAccessPolicy_?

I’m interested to hear if it’s important to you to allow _run-time arguments_ to tweak the pixel access, as the constant value of itk::ConstantBoundaryCondition. My feeling is that the constant used with itk::ConstantBoundaryCondition is usually zero, or at least a _compile-time_ constant. Did you see other use cases?

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [July 13, 2018, 5:15pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/11 "2018-07-13T17:15:16Z")

</div>

> [@Niels\_Dekker](#):
>
> So what name would you propose for the class that I named _BorderReplicatingImageNeighborhoodPixelAccessPolicy_ at [http://review.source.kitware.com/#/c/23366](http://review.source.kitware.com/#/c/23366) ? _ZeroFluxNeumannBoundaryConditionPixelAccessPolicy_ ?

That sounds good to me.

> [@Niels\_Dekker](#):
>
> I’m interested to hear if it’s important to you to allow _run-time arguments_ to tweak the pixel access, as the constant value of itk::ConstantBoundaryCondition. My feeling is that the constant used with itk::ConstantBoundaryCondition is usually zero, or at least a _compile-time_ constant. Did you see other use cases?

You are right, maybe it is not important to generalize to accept run-time argument, for the case of itk::ConstantBoundaryCondition, I think providing compile-time values: `Zero`, `One`, `Min`, and `Max` (from `itk:NumericalLimits<TImage::PixelType>`) should cover 99% of the use cases of the ConstantBoundaryCondition.

So, `Max/Min/Zero/One/CostantBoundaryConditionPixelAccessPolicy` or similar?

---

<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:** [July 16, 2018, 4:51pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/12 "2018-07-16T16:51:41Z")

</div>

> [@phcerdan](#):
>
> So, `Max/Min/Zero/One/ConstantBoundaryConditionPixelAccessPolicy` or similar?

Sure! The constant value could also be passed as an extra template argument to the policy class. That’s probably fine for most use cases, but it has a limitation, of course: then the constant value must be an _integer_. (C++ does not support float or double as template argument.)

Another possibility could be to allow the user to specify a _functor_ as an extra template argument to the policy class. This functor could then produce the _constant value_ for the policy, and would be called by `PixelAccessPolicy::GetPixelValue` whenever the index is outside the image borders. Something like this:

```
template <typename TImage, typename TFunctor>
class ConstantBoundaryConditionPixelAccessPolicy
{
public:
  PixelType GetPixelValue(const InternalPixelType* const imageBufferPointer) const ITK_NOEXCEPT
  {
    return
      m_IsOutsideImageBorders ?
      TFunctor{}() :
      m_NeighborhoodAccessor.Get(imageBufferPointer + m_PixelIndexValue);
  }
  ...
};

```

With a user-supplied functor like this:

```
struct MyFunctor
{
    double operator()() const ITK_NOEXCEPT
    {
        return 0.01;
    }
};

```

Then again, it would be handy to have some pre-defined Max/Min/Zero/One policies, as you’re suggesting.

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [July 16, 2018, 7:19pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/13 "2018-07-16T19:19:49Z")

</div>

Nice! The functor would solve the case when the user wants a specific value. Following the example you posted, I was originally thinking in a simpler case with no extra template parameter.

```cpp
template <typename TImage>
class MaxConstantBoundaryConditionPixelAccessPolicy
{
public:
  PixelType GetPixelValue(const InternalPixelType* const imageBufferPointer) const ITK_NOEXCEPT
  {
    return
      m_IsOutsideImageBorders ?
      NumericTraits<typename TImage::PixelType>::max() :
      m_NeighborhoodAccessor.Get(imageBufferPointer + m_PixelIndexValue);
  }
  ...
};

```

```cpp
template <typename TImage>
class ZeroConstantBoundaryConditionPixelAccessPolicy
{
public:
  PixelType GetPixelValue(const InternalPixelType* const imageBufferPointer) const ITK_NOEXCEPT
  {
    return
      m_IsOutsideImageBorders ?
      NumericTraits<typename TImage::PixelType>::ZeroValue() :
      m_NeighborhoodAccessor.Get(imageBufferPointer + m_PixelIndexValue);
  }
  ...
};

```

One question, why a functor, and not a static function, or a member?

---

<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:** [July 16, 2018, 9:43pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/14 "2018-07-16T21:43:24Z")

</div>

> [@phcerdan](#):
>
> why a functor, and not a static function, or a member?

First of all, I just hope we can avoid adding an extra run-time parameter (the “constant value”) to the constructor of `ShapedImageNeighborhoodRange<TImage, TPixelAccessPolicy>`. That should be possible as long as we may assume that the value of the argument is known at compile-time, in practice. But then this “constant value” should be encoded somehow in the _type_ of the PixelAccessPolicy.

When Functor1 and Functor2 are two different functor classes, producing different “constant values”, the following two template instantiations would then specify the corresponding pixel access policies, without having to add extra runtime parameters:

```
ConstantBoundaryConditionPixelAccessPolicy<ImageType, Functor1>
ConstantBoundaryConditionPixelAccessPolicy<ImageType, Functor2>

```

Ideally, people should be able to specify such a functor by a _lambda expression_, but unfortunately in C++, the type of a lambda does have a default-constructor. (The `GetPixelValue` function I posted here does `TFunctor{}` so `TFunctor` needs to be default-constructible.)

How would you implement this feature (the ability to tweak the pixel access by such a “constant value”), using a static function or a member?

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [July 16, 2018, 9:56pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/15 "2018-07-16T21:56:59Z")

</div>

Thanks for the explanation, I know that the goal is to avoid run-time parameters. it was just a minor question out of curiosity on why do you prefer a functor over something like:

```cpp
template<TPixelType>
struct MyConstant255Value
{
    static TPixelType Value()
    {
      return static_cast<TPixelType>(255)
    }
}

```

```cpp
ConstantBoundaryConditionPixelAccessPolicy<ImageType, MyConstant255Value>

```

Is there any pros or cons or one approach over the other, or they are just syntactically different, but with the same result?

---

<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:** [July 17, 2018, 2:33pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/16 "2018-07-17T14:33:01Z")

</div>

> [@phcerdan](#):
>
> it was just a minor question out of curiosity on why do you prefer a functor over something like:
> 
> ```auto
> template<TPixelType>
> struct MyConstant255Value
> {
> static TPixelType Value()
> {
> return static_cast<TPixelType>(255)
> }
> }
> 
> ```

I think it’s a good question, actually 🤔 I think your approach with the static `Value()` member function should also work fine. I just thought that the `TFunctor` approach would be more generic, more generally usable. Ideally, I would like it to accept any _type_ of function or function object that can be called with zero arguments, `f()`, and returns a value that can be converted to a pixel value. My initial idea was that it should also accept lambda’s, like this:

```
ConstantBoundaryConditionPixelAccessPolicy<ImageType, decltype([]{ return 255; })>

```

But then I realized that lambda’s aren’t DefaultConstructible, at least not according to the current C++ standard. While `TFunctor` has to be DefaultConstructible! So that won’t work for now.

To be continued…

---

<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:** [July 19, 2018, 5:09pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/17 "2018-07-19T17:09:33Z")

</div>

So far we discussed two different ways to support specifying the constant value for a policy class like `ConstantBoundaryConditionPixelAccessPolicy` by means of a type template argument:

**`TFunctor`:** Allowing the policy to retrieve the constant value by `TFunctor{}()`.

**`TStaticValueClass`:** Allowing the policy to retrieve the constant value by `TStaticValueClass::Value()`.

Actually there’s a third possibility which I find quite reasonable, and quite appealing:

**`TValueConvertible`:** Allowing the policy to retrieve the constant value, just by `TValueConvertible{}`. (Without the extra parentheses needed for `TFunctor{}()`). `TValueConvertible{}` should then be convertible to the pixel value.

For a simple zero-constant policy and a simple built-in pixel type, `TValueConvertible` could just be `int`, as the expression `int{}` denotes the integer value _zero_. But in general, such a convertible type could be written, for instance, as follows:

```
struct My255ValueConvertible
{
  template <typename TPixelType> 
  operator TPixelType() const
  {
    return static_cast<TPixelType>(255);
  }
};

```

What do you think?

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [July 23, 2018, 1:37pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/18 "2018-07-23T13:37:59Z")

</div>

I don’t see much difference, Niels! These structs would probably only be used as template parameters of the Policies, so… whatever interface sounds good, the third option with the template in `operator()` is slightly strange for me, how are you calling it?

```auto
My255ValueConvertible my255;
int myValue = my255.operator()<int>

```

I am more familiar with:

```cpp
int myValue = My255ValueStatic<int>::Value();

```

or, if you want to put the template in the operator:

```cpp

struct MyConstant255Value
{
    template<TPixelType>
    static TPixelType Value()
    {
      return static_cast<TPixelType>(255)
    }
}

```

```auto
int myValue = My255ValueStatic::Value<int>();

```

But as I said, I think the all the options look good for providing the compile time value.

---

<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:** [July 23, 2018, 2:11pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/19 "2018-07-23T14:11:35Z")

</div>

Thanks for your reply, Pablo.

> [@phcerdan](#):
>
> the third option with the template in `operator()` is slightly strange for me, how are you calling it?
> 
> ```auto
> My255ValueConvertible my255;
> int myValue = my255.operator()<int>
> 
> ```

No, simply as follows:

```
My255ValueConvertible my255;
int myValue = my255;

```

You may try it out at [My255ValueConvertible for ITK, C++ (clang) - rextester](http://rextester.com/CWQJ19410)😃

---

<div class="post-metadata">

**Author:** ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)\
**Post date:** [July 23, 2018, 2:16pm UTC](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879/20 "2018-07-23T14:16:02Z")

</div>

That’s super nice! 😁

[Next page](https://discourse.itk.org/t/custom-border-extrapolation-of-shapedimageneighborhoodrange-by-imageneighborhoodpixelaccesspolicy/879.md?page=2)
