How to create an efficient sparse ImgLib2 volume populated by a list of 3D spheres



Hi all,

Using the KDTree, perhaps there is a way to convert data like this:

from net.imglib2 import RealPoint

spheres = [
  RealPoint([100.2, 10.3, 23.5]),
  RealPoint([340.2, 55.0, 47.8]),

… into a sparse RealRandomAccessible. How would one go about it, @tpietzsch ?

The radius can be the same for all, or specified differently–doesn’t matter. The RealRandomAccessible's cursor should return a value when inside and zero when outside the radius of any of the spheres.



I believe the answer is in imglib2-roi. You can make sphere ROIs and merge multiple ROIs by and operations.

@awalter17 has a demo branch in the imglib2-roi repository that she and @tpietzsch have worked on where you can find some examples of this.


Thanks very much! Will explore imglib2-roi.


So I did explore imglib2-roi, and it looks like an impenetrable jungle. Are there any annotated real-world examples of usage of imglib2-roi? Perhaps @tinevez has some?


Hi Albert,
do you have something like this in mind:

Coordinates and associated colors put in KDTree, then converted to nearest neighbor RealRandomAccessible.


Thanks. With a radius for every point, though.


Ok, I should have read the topic title…
You would make a KDTree of those centroids.
Then make a RealRandomAccessible with the KDTree and a custom RealRandomAccess.
The RealRandomAccess has a NearestNeighborSearchOnKDTree search on tree, in get() it does, then compares search.getDistance() to radius.
Returns one T value inside, another outside.
If you derive the custom RealRandomAccess from RealPoint, it’s not a lot of boilderplate


If each point has a different radius, you need more than the nearest neighbor. You would use RadiusNeighborSearch with the maximum radius of any of your spheres.
In mastodon-collection we have IncrementalNEarestNeighborSearch which would be preferable but I haven’t got around to backporting that to imglib2 yet.


Maybe as reference for boilerplate for RRAI and RRA:

This is similar (in terms of boilerplate): one color inside some distance to an ellipsoid surface, another color outside.


Thank you very much @tpietzsch, that seems to be exactly what I am after.


By the way this:

… reads like it would make a useful util class.


I’ve posted a full example that uses the KDTree and NearestNeighborSearchOnKDTree classes to draw simulated segmentations of detected embryos in the sample image embryos.jpg.