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?
It seems to me that GenerateRectangularImageNeighborhoodOffsets(radius) 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);
GenerateRectangularImageNeighborhoodOffsets 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. But we have just moved them out of that namespace, in ITK’s git repository, June 23! 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 might be interesting for your use case! Example from the Detailed Description section of its documentation:
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:
@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.