ImgLabeling contains no labels when converted from slice view

I’m trying to get a labeling image from just a single slice of a stack, using ImageJ2 (i.e. imglib2-roi and imagej-ops) functionality.

With the following (Groovy) script (and the new Img => ImgLabeling conversion in imagej-common-0.33.0), when trying e.g. on the Bat Cochlea Volume sample image:

#@ Dataset inputStack
#@ ConvertService cs
#@ OpService ops
#@ UIService ui

import net.imglib2.roi.labeling.ImgLabeling

slice = ops.transform().hyperSliceView(inputStack, 2, 10)

labeling = cs.convert(slice, ImgLabeling.class)

println labeling.getMapping().getLabels()

I get an empty set ([]) when trying to convert just a slice, whereas everything works as expected ([255]) when I convert the entire stack:

labeling = cs.convert(inputStack, ImgLabeling.class)

When showing the index image, I do see a correct one (with continuous labels starting at 1) for the entire stack, but just a copy of the slice (with index at 255) for the “converted” single slice.

My questions to the ImgLib2 experts:

  • Is there something wrong in my implementation of the conversion? (See imagej-common changes)
  • Is there something wrong in how I use ImageJ Ops to get the slice (view)? Should I make a copy first?
  • Is there something wrong in my script?

Thanks all for your help.


Is it possible that this fails

because the slice is not an Img (I assume…?), so it will not have a ImageFactory?
But strange that you apparently get a non-null labeling then.

1 Like

Ah, yes, thanks for that hint!

That was exactly what was confusing me as well. But your reply reminded me that there is org.scijava.convert.DefaultConverter that supports conversions of:

  • Objects where the destination Class has a constructor which takes that Object

So in this case, the SciJava framework wasn’t choosing the dedicated implementation (because the input object didn’t match Img), but instead returned a:

new ImgLabeling(slice)

… which is an invalid ImgLabeling that has no LabelMapping attached, but still has a non-empty index image. That explains exactly what I am seeing. :slight_smile:

So, now: @tpietzsch, if I want to change the converter to accept any RandomAccessibleInterval, what is the recommended way to create a new image with a suitable ImgFactory and the same type and dimensions as my input RAI?

I would use net.imglib2.util.Util::getTypeFromInterval to get the type of the input RAI and net.imglib2.util.Util::getSuitableImgFactory to get a suitable image factory.

1 Like