Search code examples
c++arraysboostiteratorboost-iterators

Boost: iterating over an image region


I have images that I don't own, with adjacent monochrome pixels, which I currently iterate with dumb pointer-increment.

I now have the need to iterate over 2D regions, so I'm thinking about the best way to provide iterators that are efficient and easy to write.

I need only forward_iterator. If I have a 10x10 image and if I want to iterate on 4x3 upper left region it will iterate on these elements:

0 1 2 3 10 11 12 13 20 21 22 23

I have two options:

  • write it by hand with boost::iterator_facade
  • use boost::range_iterator, which gives me an iterator over the array, and modify its behavior with boost::iterator_adaptor

What do you think is the best one?


Solution

  • You have two concepts: (i) subimage, and (ii) 1-d iterator over image.

    I would start by taking a look at vil_image_view in the VXL library.

    This separates the concept of image data, from the representation (view) of the data. The view provides istep, jstep amd planestep increments, and pixel indexing. Different vil_image_views can be created to represent cropped images, from the same underlying data. This library is only partially compatible with STL, and a good STL implementation would be interesting, but I am not sure of a clean way to implement a 2d iterator in STL.