Efficient Sparse Labeling of Large Image with ImgLib2


I have a large image and I want to label a small number of the pixels to belong to foreground or background. The labeling is sparse so most pixels won’t be labeled at all.

I search for a efficient way to only iterate over the labeled pixels.
Additionally I want to store and load such labeling, efficiently.

Currently I wrap a ImgLabeling around a cached Img. To iterate only over the pixels labeled (for example) foreground I use the LabelRegions and LabelRegion classes. But that’s not efficient, because the algorithm that sets up the LabelRegions iterates over the whole cached Img, including the pixels that aren’t labeled.

Is there already a solution for such sparse labeling, using imglib2 and ops?


Hey @dietzc and @tpietzsch, if you have any input on this, I would really appreciate it. Thank’s in advance!

Hi @maarzt,

Not sure if this helps, but NtreeImg’s are efficient at storing sparse data. It will iterate over all pixels in the image though (that’s what the spec for an IterableInterval has to do) but if you get the underlying data structure, then it will be easy to iterate over only the pixels with data.

Maybe others have other ideas, but hope this helps anyway,

Hi @maarzt,

What would be cool in the long run would be a Labeling (or so) which bases on a set of (named) ROIs directly, rather than using a RAI with indices to Sets of labels as a backend. In some situations (as yours) this is more efficient (of course also depends on the use-case).



@bogovicj thanks for pointing out NtreeImg that will be useful.

@dietzc I guess I will implement a labeling that will be a mapping from label to ROI. Is there currently a sparse representation for an arbitrary shaped ROI?

@maarzt this is a question for @ctrueden and @awalter17.



Related to that, its probably worth checking this thread on ‘imglib2-roi’ development.

1 Like

Hello @maarzt,

Unfortunately, no.

I do have methods for combining multiple ROIs into a single ROI on my shape-rois branch (see Masks), but those are for real space ROIs and there isn’t currently a way to efficiently iterate the contained pixels.

I believe what you’d want is something similar to that but for discrete space ROIs which know how to iterate themselves efficiently. But sadly this currently doesn’t exist :slight_frown:




@awalter17 Thank you for the input.
What would be the appropriate interface for a discrete space ROI with efficient iteration? (IterableRegion?)

1 Like

@maarzt IterableRegion would be the correct interface. You would also want to implement Mask<Localizable>, which should serve as the base interface for discrete space ROIs.

There’s a question of if you should first make a discrete space ROI which implements only Mask<Localizable> and then wrap this Mask<Localizable> as an IterableRegion, or if both of those interfaces should be implemented by one class. @ctrueden and I think it would be OK to just have one class which implements both Mask<Localizable> and IterableRegion. @tpietzsch do you see any reason to keep them separate?

You may also want to look at the imglib2 neighborhood implementations, as some of those could essentially be made into discrete space ROIs and they have efficient iteration. There was already some discussion about moving parts of the neighborhood components to imglib2-roi, see this post.