# PixelAccessor or NeighborhoodAccessor?

**URL:** <https://discourse.itk.org/t/pixelaccessor-or-neighborhoodaccessor/1441>\
**Category:** Engineering\
**Created:** [November 24, 2018, 10:48am UTC](https://discourse.itk.org/t/pixelaccessor-or-neighborhoodaccessor/1441 "2018-11-24T10:48:36Z")\
**Posts on this page:** 9\
**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:** [November 24, 2018, 10:48am UTC](https://discourse.itk.org/t/pixelaccessor-or-neighborhoodaccessor/1441/1 "2018-11-24T10:48:36Z")

</div>

Last Tuesday I implemented another modern C++ range of iterators: [ImageRange](https://github.com/InsightSoftwareConsortium/ITK/pull/218) should support iterating over all pixels of an image buffer. (Still work in progress – WIP.)

Now I wonder if I chose the right accessor to access the pixels of the image. I chose the one returned by [itk::Image::GetPixelAccessor()](https://github.com/InsightSoftwareConsortium/ITK/blob/v5.0b01/Modules/Core/Common/include/itkImage.h#L262). My implementation of [iterator::operator\*()](https://github.com/InsightSoftwareConsortium/ITK/blob/e1dd5ab95bb8c2e4a181c6abd2d090a61249e729/Modules/Core/Common/include/itkImageRange.h#L158) retrieves a reference to the pixel pointed to by `iterator::m_CurrentPixel` by doing [m\_PixelAccessor.Get(\*m\_CurrentPixel)](https://github.com/InsightSoftwareConsortium/ITK/blob/e1dd5ab95bb8c2e4a181c6abd2d090a61249e729/Modules/Core/Common/include/itkImageRange.h#L160). Is that OK? It seems to me that this approach does not support vector images (`itk::VectorImage`). The accessor returned by [itk::VectorImage::GetPixelAccessor()](https://github.com/InsightSoftwareConsortium/ITK/blob/v5.0b01/Modules/Core/Common/include/itkVectorImage.h#L314) appears incompatible with the one from `itk::Image`.

Should I use ITK’s _NeighborhoodAccessor_ instead, as in [ShapedImageNeighborhoodRange](https://github.com/InsightSoftwareConsortium/ITK/blob/v5.0b01/Modules/Core/Common/include/itkShapedImageNeighborhoodRange.h#L563)?

---

<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 26, 2018, 1:06am UTC](https://discourse.itk.org/t/pixelaccessor-or-neighborhoodaccessor/1441/2 "2018-11-26T01:06:01Z")

</div>

Awesome, @Niels_Dekker!! ✨

What happens when `itk::VectorImage` is added to the unit test?

---

<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:** [November 26, 2018, 9:54am UTC](https://discourse.itk.org/t/pixelaccessor-or-neighborhoodaccessor/1441/3 "2018-11-26T09:54:13Z")

</div>

> [@matt.mccormick](#):
>
> Awesome, @Niels_Dekker!! ✨
> 
> What happens when `itk::VectorImage` is added to the unit test?

Thanks @matt.mccormick. An attempt to use `Experimental::ImageRange<itk::VectorImage<int>>` triggers compilation errors:

```
modules\core\common\include\itkimagerange.h(160): error C2660: 'itk::DefaultVectorPixelAccessor<int>::Get': function does not take 1 arguments
modules\core\common\include\itkdefaultvectorpixelaccessor.h(78): note: see declaration of 'itk::DefaultVectorPixelAccessor<int>::Get'

```

So am I using the wrong pixel accessor here, or am I just using the pixel accessor (from `itk::Image::GetPixelAccessor()`) in the wrong way, within the [ImageRange](https://github.com/InsightSoftwareConsortium/ITK/pull/218) class? 🤔 A more interestingly question is, of course: how to do it right?

---

<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:** [November 26, 2018, 2:31pm UTC](https://discourse.itk.org/t/pixelaccessor-or-neighborhoodaccessor/1441/4 "2018-11-26T14:31:42Z")

</div>

Have you looked at the `TImage::AccessorFunctorType` typedef? It is used with the [`ImageConstIterator`](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkImageConstIterator.h#L324).

Will a similar Proxy design pattern for the return “reference” work here too?

---

<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:** [November 26, 2018, 3:30pm UTC](https://discourse.itk.org/t/pixelaccessor-or-neighborhoodaccessor/1441/5 "2018-11-26T15:30:31Z")

</div>

Thanks @blowekamp ! I see now, `AccessorFunctorType` can be used in a more generic way than my initial attempt, which only used `AccessorType` (as returned by `GetPixelAccessor()`). And indeed, it looks like the `Get` and `Set` of `AccessorFunctor` can be used within a “reference-like” proxy class, as with the internal `PixelProxy` from `ShapedImageNeighborhoodRange`.

Still I wonder why there are different accessors for image-based pixel iteration and neighbor iteration. Do you know why? Would it be OK to use the `NeigborhoodAccessor` to iterate over the whole image?

---

<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:** [November 27, 2018, 5:58pm UTC](https://discourse.itk.org/t/pixelaccessor-or-neighborhoodaccessor/1441/6 "2018-11-27T17:58:22Z")

</div>

OK: just committed a new version of [ImageRange](https://github.com/InsightSoftwareConsortium/ITK/pull/218) that _does_ support `itk::VectorImage`:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/218/commits/45571c2dff26611aef29ca57a8f2a7e678d71073>

Still Work In Progress (WIP) though. When ready, I think I’ll squash the WIP commits into one.

The new commit applies the Proxy design pattern, using `TImage::AccessorFunctorType`, as suggested by @blowekamp Please have a look!

With this commit, `ImageRange::iterator::operator*()` returns a proxy, instead of a “real” reference to the current pixel. This is necessary for `itk::VectorImage`, but not for a regular `itk::Image`. With the next version I would like `operator*()` to return a proxy for `itk::VectorImage`, and a reference for a regular `itk::Image`.

---

<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:** [November 27, 2018, 6:21pm UTC](https://discourse.itk.org/t/pixelaccessor-or-neighborhoodaccessor/1441/7 "2018-11-27T18:21:56Z")

</div>

Hey @Niels_Dekker, this looks great.  
Related with this and other patches in this same direction, I assume using `Accessors` is going to hit performance (also it is stated in a comment [here](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkImageConstIterator.h#L326-L330)). Do you think is doable to use SFINAE to only use accessors for VectorImages and such, and to avoid them for “regular” images?

I am not familiar with the internal use of ImageAdaptors, but it might be worth to make the iterators as zero-cost as possible without sacrificing type flexibility. Pinging @blowekamp

---

<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:** [November 27, 2018, 7:45pm UTC](https://discourse.itk.org/t/pixelaccessor-or-neighborhoodaccessor/1441/8 "2018-11-27T19:45:14Z")

</div>

Thatnks for your encouraging reply, Pablo!

> [@phcerdan](#):
>
> Do you think is doable to use SFINAE to only use accessors for VectorImages and such, and to avoid them for “regular” images?

I think it would be possible, at compile-time, possibly even without SFINAE. 🙂

In pseudo-code:

```
if TImage::PixelType is TImage::InternalPixelType
and TImage::AccessorType is DefaultPixelAccessor
and TImage::AccessorFunctor is DefaultPixelAccessorFunctor
then
  operator*() should return a real reference to the pixel
else
  operator*() should return a "pixel proxy"

```

Just have to make sure that that’s indeed the right condition for choosing between a reference and a proxy!

---

<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:** [November 30, 2018, 8:10pm UTC](https://discourse.itk.org/t/pixelaccessor-or-neighborhoodaccessor/1441/9 "2018-11-30T20:10:42Z")

</div>

@phcerdan [Please review](https://github.com/InsightSoftwareConsortium/ITK/pull/252) how I implemented that logic for `ImageRange`, _to only use accessors for VectorImages and such, and to avoid them for “regular” images_:

The compile-time constant bool `SupportsDirectPixelAccess` specifies whether `ImageRange<TImage>` supports direct pixel access for this particular image type (`TImage`): [itkImageRange.h#L86](https://github.com/InsightSoftwareConsortium/ITK/blob/c379552109eae4de776109a91ad8f4e2e6d5e08e/Modules/Core/Common/include/itkImageRange.h#L86) For regular image types, direct pixel access is supported, and the accessor is avoided.

The range has an `OptionalAccessorFunctor`, which is empty (`EmptyAccessorFunctor`) when `SupportsDirectPixelAccess` == true. Otherwise, it is equal to the `AccessorFunctor` of the specified image. [itkImageRange.h#L93](https://github.com/InsightSoftwareConsortium/ITK/blob/c379552109eae4de776109a91ad8f4e2e6d5e08e/Modules/Core/Common/include/itkImageRange.h#L93)

The `OptionalAccessorFunctor` is initialized by `AccessorFunctorInitializer`, which converts to either an `EmptyAccessorFunctor` or a properly initialized `AccessorFunctor`. [itkImageRange.h#L485](https://github.com/InsightSoftwareConsortium/ITK/blob/c379552109eae4de776109a91ad8f4e2e6d5e08e/Modules/Core/Common/include/itkImageRange.h#L485)

So you see, I tried to avoid any overhead from those accessors when `SupportsDirectPixelAccess` is true.

`iterator::operator*()` returns a reference to the pixel when `SupportsDirectPixelAccess` == true, and otherwise, it returns a `PixelProxy` object. [itkImageRange.h#L327](https://github.com/InsightSoftwareConsortium/ITK/blob/c379552109eae4de776109a91ad8f4e2e6d5e08e/Modules/Core/Common/include/itkImageRange.h#L327) (And `PixelProxy` does use the `AccessorFunctor`.)

I hope it’s clear enough!

Pull request opened by @dzenanz: [https://github.com/InsightSoftwareConsortium/ITK/pull/245](https://github.com/InsightSoftwareConsortium/ITK/pull/245)

**Update, Dec 1st, 2018:** The latest `ImageRange` pull request (that should supersede the previous ones) is here:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/252/>
