How to best check that an IterableInterval image is binary?

Many of the Ops in BoneJ2 are only meant to work with binary images, so I often need to check the open image before passing it down. A binary image is any image that contains only two distinct values, e.g. black & white. I was suggested that a fast and general way to check this would be:

final Class<?> elementClass = interval.firstElement().getClass();
final boolean isBinary = BooleanType.class.isAssignableFrom(elementClass);

Now this works fine in unit tests where I create a Dataset whose elements are BitType etc. The thing is, it doesn’t work that well in the real world. For example, the sample image “Bat Cochlea Volume” fails this test (element type is UnsignedByteType). Furthermore, even if I binarize an image (Process > Binary > Make Binary) the result won’t pass the test.

Any ideas how to test IterableInterval / Dataset for this property? You could always brute force and go through all the elements in the interval, but that’s not a very good solution…

Best Regards,
Richard Domander

I think you Unit-test is absolutely correct and the way to go. I’d rather ask myself why you have an UnsignedByteType IterableInterval which is actually binary? Why isn’t a proper type used?



I agree with @dietzc, although I also think having a statistic that detects “binary” images (i.e., with only two distinct values) would be useful. Like similar statistics (e.g., histograms), it fits in with the “features” framework in that we would want to cache it for reuse.

we could easily add such a statistic. Simply check if only two values occur in an image and then consider it to be binary. But I still hope we won’t need this too often :slight_smile:

1 Like