@@ -0,0 +1,1988 @@
+%0Apackage net.restlesscoder.sandbox.imagej;%0A%0Aimport java.util.Set;%0A%0Aimport net.imagej.ImageJ;%0Aimport net.imglib2.FinalDimensions;%0Aimport net.imglib2.algorithm.labeling.ConnectedComponents.StructuringElement;%0Aimport net.imglib2.img.Img;%0Aimport net.imglib2.roi.labeling.ImgLabeling;%0Aimport net.imglib2.roi.labeling.LabelingType;%0Aimport net.imglib2.type.numeric.IntegerType;%0Aimport net.imglib2.type.numeric.integer.UnsignedByteType;%0A%0A/**%0A * The generics of %7B@link ImgLabeling%7D always strike me as potentially unsafe.%0A * But in practice they are OK. This code explores it.%0A */%0Apublic class WhyImgLabelingIsOK %7B%0A%0A%09private static %3CT, I extends IntegerType%3CI%3E%3E void checkIt(String name,%0A%09%09ImgLabeling%3CT, I%3E imgLabeling, T valueToAdd)%0A%09%7B%0A%09%09imgLabeling.firstElement().add(valueToAdd);%0A%0A%09%09boolean first = true;%0A%09%09int count = 0;%0A%09%09for (final LabelingType%3CT%3E item : imgLabeling) %7B%0A%09%09%09count++;%0A%09%09%09final Set%3CT%3E labels = item.getMapping().getLabels(); // A COPY; mutating it will not persist%0A%09%09%09//System.out.println(name + %22: got item labels: %22 + labels + %22 of size %22 + labels.size());%0A%09%09%09for (final T value : labels) %7B%0A%09%09%09%09if (first) first = false;%0A%09%09%09%09else break;%0A%09%09%09%09System.out.println(name + %22: first value = %22 + value.getClass());%0A%09%09%09%7D%0A%09%09%7D%0A%09%09System.out.println(name + %22: item count = %22 + count + %22 compared to size = %22 + imgLabeling.size());%0A%09%7D%0A%0A%09public static %3CT%3E void main(final String... args) throws Exception %7B%0A%09%09final ImageJ ij = new ImageJ();%0A%0A%09%09@SuppressWarnings(%7B %22unchecked%22, %22rawtypes%22 %7D)%0A%09%09final Img%3CUnsignedByteType%3E img = (Img) ij.scifio().datasetIO().open(%22/Users/curtis/data/blobs.tif%22);%0A%09%09final ImgLabeling%3CInteger, UnsignedByteType%3E imgLabelingCCA = ij.op().labeling().cca(img, StructuringElement.EIGHT_CONNECTED);%0A%0A%09%09final ImgLabeling%3CString, UnsignedByteType%3E imgLabelingEmpty = ij.op().create().imgLabeling(new FinalDimensions(3, 4), new UnsignedByteType());%0A%0A%09%09checkIt(%22CCA%22, imgLabelingCCA, 37);%0A%09%09checkIt(%22Empty%22, imgLabelingEmpty, %22hello%22);%0A%0A%09%09ij.context().dispose();%0A%09%7D%0A%0A%7D%0A

Add an exploration of ImgLabeling usage