Dealing with image blocks

Hello folks,

Recently I had to make some algorithms (C++) that deal with modifying image subregions, the sort that would have been done in a couple of lines with the easy switch to numpy and array slicing capacities in Python.
For example, if Segm3D is a binary 3D image with an initial 2D shape on the SelectedIndex slice:
for i in range(SelectedIndex+1, Segm3D.shape[2]+1):
...Segm3D[:,:,i] = Erosion(Segm3D[:,:,i-1])
for i in range(SelectedIndex-1, -1, -1):
...Segm3D[:,:,i] = Erosion(Segm3D[:,:,i+1])

That not the first time I’m facing such problems ; I usually achieve what I want with a mix of image iterators, Get/SetPixel methods, or even with explicit slicing through ROI/Extract filters ; but I feel like there must be a better way to do it for better readability and efficiency.

So I wanted to know how you guys usually handle that kind of code? Are you using features like std::valarray or Boost.MultiArray?


1 Like

Hello Tim,

SimpleITK uses the SliceImageFilter( to implement much of slice based indexing, but does not yet support setting via slices.

For your particular case it looks like you are trying to do slice by slice processing or process your 3d volume as a series of 2d slices. For this case there is the SliceBySliceImageFilter( If you have alternative ways to process chunks of your volume you could use that as an example.

@tim-evain something to investigate is the use of xtensor with ITK.

CC: @phcerdan

I didn’t knew this filter, thank for the hint :+1:. I was doing almost the same thing through the use of RegionOfInterestImageFilter with iteration on the ImageRegion. But after that, I need to “reconstruct” the full image, going either with iterators or TileImageFilter, which left me wondering if I could use a better way, like a direct setting through some kind of views.

SliceBySliceImageFilter is a nice tool, but here I think it can’t be used as the processing on one slice is dependent on other slices.

Oh, that looks very interesting, quite like the Boost.MultiArray without the library burden, thank for the discovery :smiley:. Have you already experiment with it?

It does, indeed, look very cool :haircut_man:. I defer to @phcerdan as the expert.

yes, xtensor provide a clean pythonic/numpy interface, plus handy modules, like xtensor-blas for linear algebra. The performance seems good, they are able to achieve same performance than Eigen when the dimension of the arrays are known (type: xtensor).

I started playing to integrate xtensor in ITK, I will keep you all updated!