# How to get pixels within a radius from a specific pixel?

**URL:** <https://discourse.itk.org/t/how-to-get-pixels-within-a-radius-from-a-specific-pixel/3243>\
**Category:** Algorithms\
**Tags:** itkv5\
**Created:** [June 30, 2020, 5:53pm UTC](https://discourse.itk.org/t/how-to-get-pixels-within-a-radius-from-a-specific-pixel/3243 "2020-06-30T17:53:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aangelos28](https://discourse.itk.org/user_avatar/discourse.itk.org/aangelos28/32/1604_2.png) [@aangelos28](https://discourse.itk.org/u/aangelos28)\
**Post date:** [June 30, 2020, 5:53pm UTC](https://discourse.itk.org/t/how-to-get-pixels-within-a-radius-from-a-specific-pixel/3243/1 "2020-06-30T17:53:39Z")

</div>

Hello,

I was wondering if there is a way to get the indices of pixels within a certain radius from a center pixel with a specific ID. _itk::NeighborhoodIterator_ was the closest thing I found to this, but how can I use it to get the neighbor pixel indices when I only want to do this for specific center pixels in various different locations in the image? This iterator seems great if I want to apply a filter in the entire image, but can it be used to only iterate over pixels with specific indices?

Thank you very much.

---

<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:** [June 30, 2020, 7:01pm UTC](https://discourse.itk.org/t/how-to-get-pixels-within-a-radius-from-a-specific-pixel/3243/2 "2020-06-30T19:01:50Z")

</div>

One option is to create a new _NeighborhoodIterator_ for each center pixel you care about (region with the desired start index and size of [1,1,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:** [June 30, 2020, 10:39pm UTC](https://discourse.itk.org/t/how-to-get-pixels-within-a-radius-from-a-specific-pixel/3243/3 "2020-06-30T22:39:14Z")

</div>

> [@aangelos28](#):
>
> I was wondering if there is a way to get the indices of pixels within a certain radius from a center pixel with a specific ID.

It seems to me that [GenerateRectangularImageNeighborhoodOffsets(radius)](https://itk.org/Doxygen/html/namespaceitk_1_1Experimental.html#a0a99e87e6ee724af07d82c5b0c2993b8) might also be helpful to you. It expects an N-dimensional radius. Suppose your image is 2-D, then you could choose `itk::Size<2>{r, r}` as a 2-dimensional radius, having `r > 0`. (For example, `r = 1` would yield a neighborhood of 3 x 3 pixels, including the center pixel).

For 2-D, `GenerateRectangularImageNeighborhoodOffsets(radius)` returns an `std::vector` of 2-dimensional offsets (`itk::Offset<2>`) relative to your center pixel, denoting the neighborhood with that particular radius.

Suppose you have your center pixel located at `itk::Index<2>` { i, j }. You could then add any of those offsets to that index, to get the index of the corresponding neighbor pixel.

```
// Untested, just for you to get the idea:
std::vector<itk::Index<2>> neighborIndices;
for(auto offset: offsets)
  neighborIndices.push_back(centerIndex + offset);

```

HTH, Niels

---

<div class="post-metadata">

**Author:** ![aangelos28](https://discourse.itk.org/user_avatar/discourse.itk.org/aangelos28/32/1604_2.png) [@aangelos28](https://discourse.itk.org/u/aangelos28)\
**Post date:** [July 3, 2020, 8:46pm UTC](https://discourse.itk.org/t/how-to-get-pixels-within-a-radius-from-a-specific-pixel/3243/4 "2020-07-03T20:46:21Z")

</div>

@dzenanz I tried this approach and it works, however it is not the most intuitive way to have an iterator that iterates over only one element.

@Niels_Dekker This is a great approach and seems more intuitive than using the iterator. Is there a reason why it is in the Experimental namespace?

Thank you both for your help. I was able to implement what I was aiming for.

---

<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 4, 2020, 5:58am UTC](https://discourse.itk.org/t/how-to-get-pixels-within-a-radius-from-a-specific-pixel/3243/5 "2020-07-04T05:58:57Z")

</div>

> [@aangelos28](#):
>
> @Niels_Dekker This is a great approach and seems more intuitive than using the iterator. Is there a reason why it is in the Experimental namespace?

[GenerateRectangularImageNeighborhoodOffsets](https://itk.org/Doxygen/html/namespaceitk_1_1Experimental.html#a0a99e87e6ee724af07d82c5b0c2993b8) is part of a group of functions and classes that were introduced with ITK 5. At the initial release, they were still somewhat experimental, which is why we placed them in [the `Experimental` namespace](https://itk.org/Doxygen/html/namespaceitk_1_1Experimental.html#a0a99e87e6ee724af07d82c5b0c2993b8). But we have just moved them out of that namespace, [in ITK’s git repository, June 23](https://github.com/InsightSoftwareConsortium/ITK/commit/e50b279bcb45dee137690bf7ee59bb78312a5f02)! With the next major release (ITK 5.2.0), they should be directly in `itk` namespace 😃

The group of functions and classes that were originally added to `Experimental` namespace offer C++11 “std style” iteration over the pixel values or the indices of an image. (Similar to how you would iterate over the elements of an `std::vector`.) Especially the [ShapedImageNeighborhoodRange](https://itk.org/Doxygen/html/classitk_1_1Experimental_1_1ShapedImageNeighborhoodRange.html) might be interesting for your use case! Example from the Detailed Description section of [its documentation](https://itk.org/Doxygen/html/classitk_1_1Experimental_1_1ShapedImageNeighborhoodRange.html):

> The following example creates a 3 x 5 neighborhood around pixel [10, 20] and adds 42 to each neighborhood pixel, using a range-based for loop:

```
const ImageType::IndexType location = {10, 20};
const itk::Size<ImageType::ImageDimension> radius = { { 1, 2 } };
const std::vector<OffsetType> offsets = itk::GenerateRectangularImageNeighborhoodOffsets(radius)
itk::ShapedImageNeighborhoodRange<ImageType> neighborhoodRange{ *image, location, offsets };
 
for (auto&& neighborhoodPixel : neighborhoodRange)
{
  neighborhoodPixel = neighborhoodPixel + 42;
}

```

Hmmm, I see now that the example is already without the `Experimental` namespace! Anyway, I hope you got the point! 😃

Kind regards, Niels

---

<div class="post-metadata">

**Author:** ![aangelos28](https://discourse.itk.org/user_avatar/discourse.itk.org/aangelos28/32/1604_2.png) [@aangelos28](https://discourse.itk.org/u/aangelos28)\
**Post date:** [July 11, 2020, 9:29pm UTC](https://discourse.itk.org/t/how-to-get-pixels-within-a-radius-from-a-specific-pixel/3243/6 "2020-07-11T21:29:16Z")

</div>

@Niels_Dekker Sorry the delayed reply! This last example you posted using the `ShapedImageNeighborhoodRange` in combination with `GenerateRectangularImageNeighborhoodOffsets` is perfect if you want to deal with the pixel values. Is there some way to iterate over the indices of the pixels in the range instead of their values using a `ShapedImageNeighborhoodRange`? Maybe a second iterator that returns the neighbor indices would be helpful.

Thank you a lot.
