<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">Hi Robert,<div><br></div><div>the problem is the following:</div><div>In the ImgLabeling constructor</div><div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: rgb(147, 26, 104);">public</span> ImgLabeling( <span style="color: rgb(147, 26, 104);">final</span> RandomAccessibleInterval< I > <span style="color: rgb(126, 80, 79);">img</span> )</div></div><div>the img you pass is used to store the labels at each pixel.</div><div>(The way that works is that integers are mapped to sets of labels that occur. If you have labels of type String, i.e., a ImgLabeling<String,IntType>,</div><div>then depending on which labels and combinations of labels actually occur in your labeling, the mapping might be something like</div><div><div style="margin: 0px; font-size: 11px; font-family: Monaco; color: rgb(78, 144, 114);"> 0 -> {}</div><div style="margin: 0px; font-size: 11px; font-family: Monaco; color: rgb(78, 144, 114);"> 1 -> {"A"}</div><div style="margin: 0px; font-size: 11px; font-family: Monaco; color: rgb(78, 144, 114);"> 2 -> {"B"}</div><div style="margin: 0px; font-size: 11px; font-family: Monaco; color: rgb(78, 144, 114);"> 3 -> {"B","C"}</div><div style="margin: 0px; font-size: 11px; font-family: Monaco; color: rgb(78, 144, 114);"><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;">etc...</div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;">The mapping is build, as you add labels to pixels.)</div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;"><br></div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;">The assumption is that initially the labeling is empty, that is all zeros in the backing image, and the mapping just comprising 0 -> {}.</div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;">In your example, you passed the labelMap as the backing image. This is not zeroed.</div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;"><br></div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;">One way to make your example work is to simply use a new, empty backing image, and then set the labels from the labelMap like you did.</div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;">Replace this:</div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;"><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: rgb(147, 26, 104);">final</span> RandomAccessibleInterval< IntType > <span style="color: rgb(126, 80, 79);">img</span> = ImageJFunctions.wrapNumeric( <span style="color: rgb(126, 80, 79);">labelMap</span> );</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Img< FloatType > <span style="color: rgb(126, 80, 79);">img2</span> = ImageJFunctions.convertFloat( <span style="color: rgb(126, 80, 79);">labelMap</span> );</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: rgb(147, 26, 104);">final</span> <span style="color: rgb(147, 26, 104);">long</span>[] <span style="text-decoration: underline; color: rgb(126, 80, 79);">dims</span> = <span style="color: rgb(147, 26, 104);">new</span> <span style="color: rgb(147, 26, 104);">long</span>[ <span style="color: rgb(126, 80, 79);">img</span>.numDimensions() ];</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span>ImgLabeling< Integer, IntType > <span style="color: rgb(126, 80, 79);">labeling</span> = <span style="color: rgb(147, 26, 104);">new</span> ImgLabeling< Integer, IntType >( <span style="color: rgb(126, 80, 79);">img</span> );</div></div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;">by this:</div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;"><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: rgb(147, 26, 104);">final</span> Img< FloatType > <span style="color: rgb(126, 80, 79);">img2</span> = ImageJFunctions.convertFloat( <span style="color: rgb(126, 80, 79);">labelMap</span> );</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: rgb(147, 26, 104);">final</span> Dimensions <span style="color: rgb(126, 80, 79);">dims</span> = <span style="color: rgb(126, 80, 79);">img2</span>;</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: rgb(147, 26, 104);">final</span> IntType <span style="color: rgb(126, 80, 79);">t</span> = <span style="color: rgb(147, 26, 104);">new</span> IntType();</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: rgb(147, 26, 104);">final</span> RandomAccessibleInterval< IntType > <span style="color: rgb(126, 80, 79);">img</span> = Util.getArrayOrCellImgFactory( <span style="color: rgb(126, 80, 79);">dims</span>, <span style="color: rgb(126, 80, 79);">t</span> ).create( <span style="color: rgb(126, 80, 79);">dims</span>, <span style="color: rgb(126, 80, 79);">t</span> );</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: rgb(147, 26, 104);">final</span> ImgLabeling< Integer, IntType > <span style="color: rgb(126, 80, 79);">labeling</span> = <span style="color: rgb(147, 26, 104);">new</span> ImgLabeling< Integer, IntType >( <span style="color: rgb(126, 80, 79);">img</span> );</div><div>Then it works.</div><div><br></div><div><br></div><div>For transfering the labeling you do not have to loop over all the labels. you could just do this:</div><div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: rgb(147, 26, 104);">final</span> Cursor< LabelingType< Integer > > <span style="color: rgb(126, 80, 79);">labelCursor</span> = Views.flatIterable( <span style="color: rgb(126, 80, 79);">labeling</span> ).cursor();</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: rgb(147, 26, 104);">for</span> ( <span style="color: rgb(147, 26, 104);">final</span> UnsignedByteType <span style="color: rgb(126, 80, 79);">input</span> : Views.flatIterable( ImageJFunctions.wrapByte( <span style="color: rgb(126, 80, 79);">labelMap</span> ) ) )</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: rgb(147, 26, 104);">final</span> LabelingType< Integer > <span style="color: rgb(126, 80, 79);">element</span> = <span style="color: rgb(126, 80, 79);">labelCursor</span>.next();</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: rgb(147, 26, 104);">if</span> ( <span style="color: rgb(126, 80, 79);">input</span>.get() != 0 )</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: rgb(126, 80, 79);">element</span>.add( <span style="color: rgb(126, 80, 79);">input</span>.get() );</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</div></div><div>(This assumes that in labelMap the value 0 means “no label”).</div><div><br></div><div><br></div><div>In principle, if your labelMap has values 0, 1, 2, ... without big gaps, and 0 means “no label” you could just use it directly as backing image and produce a fitting LabelingMapping, like</div><div><div style="margin: 0px; font-size: 11px; font-family: Monaco; color: rgb(78, 144, 114);"> 0 -> {}</div><div style="margin: 0px; font-size: 11px; font-family: Monaco; color: rgb(78, 144, 114);"> 1 -> {1}</div><div style="margin: 0px; font-size: 11px; font-family: Monaco; color: rgb(78, 144, 114);"> 2 -> {2}</div><div style="margin: 0px; font-size: 11px; font-family: Monaco; color: rgb(78, 144, 114);"> 3 -> {3}</div><div style="margin: 0px; font-size: 11px; font-family: Monaco; color: rgb(78, 144, 114);"></div></div></div></div></div><div>etc yourself. But that involves diving much deeper into the details, basically reading through the code of ImgLabeling and LabelingMapping.</div><div>I would recommend above solution for now.</div><div><br></div><div>best regards,</div><div>Tobias</div><div><br></div><div><div>On 08 Sep 2015, at 09:11, Robert Haase <<a href="mailto:rhaase@mpi-cbg.de">rhaase@mpi-cbg.de</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Hi All,<br><br>I'm quite new in developing imagej-plugins using imglib2. I ran in some issues working with Labeling and Regions in imglib2. Hopefully, somebody of you can help me. This is what I would like to do:<br><br>I have an image(Plus) where pixel values correspond to the desired labeling. Thus, all pixels with value 1 belong to label 1, all pixels with value 2 belong to label 2 and so on. Labelled regions cannot overlap. Now, I would like to create an ImgLabeling from the given ImagePlus to manage the corresponding regions of interest in imglib2 and to do the analysis using this library.<br>I was able to create some functions which appear to do the transformation, however, the results are not fully understandable for me. On bottom of this email you find "minimal" example code (which runs fine in eclipse set up for ImageJ-development) which creates a labelmap-ImagePlus, transforms it into ImgLabeling and displays binary images corresponding to the regions. However, when I input a label with 100 pixels, the corresponding output region may or may not have the same number of pixels. So the output of the program is:<br><br>Number of input pixels in label 0: 9400<br>Number of input pixels in label 1: 100<br>Number of input pixels in label 2: 200<br>Number of input pixels in label 3: 300<br>Number of input pixels in label 4: 0<br>Number of out pixels in region: 10000<br>Number of out pixels in region: 600<br>Number of out pixels in region: 500<br>Number of out pixels in region: 300<br><br>As you can see, the number of read-in pixels is not equal to the number of pixels which are positive afterwards in the corresponding region. The program visualises the images as well. The mistake in the result is obvious. My mistake in the code is not that obvious to me.<br><br>I guess, I interpret the meaning of regions and/or labels wrong. Hopefully somebody of you may point me in the right direction to do the above mentioned transformation properly.<br><br>Thanks a lot in advance!<br>Robert<br><br>LabelingExample.java ----------------------------<br><br><br>import ij.ImagePlus;<br>import ij.gui.NewImage;<br>import ij.process.ImageProcessor;<br><br>import java.awt.Rectangle;<br>import java.io.IOException;<br>import java.util.Set;<br><br>import net.imglib2.Cursor;<br>import net.imglib2.RandomAccess;<br>import net.imglib2.RandomAccessibleInterval;<br>import net.imglib2.img.Img;<br>import net.imglib2.img.array.ArrayImgs;<br>import net.imglib2.img.display.imagej.ImageJFunctions;<br>import net.imglib2.roi.labeling.ImgLabeling;<br>import net.imglib2.roi.labeling.LabelRegion;<br>import net.imglib2.roi.labeling.LabelRegionCursor;<br>import net.imglib2.roi.labeling.LabelRegions;<br>import net.imglib2.roi.labeling.LabelingType;<br>import net.imglib2.type.logic.BitType;<br>import net.imglib2.type.logic.BoolType;<br>import net.imglib2.type.numeric.integer.IntType;<br>import net.imglib2.type.numeric.real.FloatType;<br><br>public class LabelingExample {<br> LabelingExample()<br> {<br> new ij.ImageJ();<br><br> //Create and show a simple test image, 100x100<br> ImagePlus imp = createTestImage();<br> imp.show();<br><br> //Create an ImgLabeling from the ImagePlus<br> ImgLabeling<Integer, IntType> labeling = getIntIntImgLabellingFromLabelMapImagePlus(imp);<br><br> //Read out regions and labelNames<br> LabelRegions<Integer> regions = new LabelRegions<Integer>(labeling);<br> Set<Integer> labelNames = labeling.getMapping().getLabels();<br><br> //Visualise the labels as binary images.<br> for (Integer labelName : labelNames )<br> {<br> LabelRegion<Integer> lr = regions.getLabelRegion(labelName);<br> ImageJFunctions.show(labelRegionToBinaryImage(lr, (Img<IntType>)labeling.getIndexImg()));<br> }<br> }<br><br> //Create an image with three labels (+background)<br> //The three labels have an area of 100, 200 and 300.<br> private ImagePlus createTestImage()<br> {<br> ImagePlus imp = NewImage.createByteImage("Test iamge", 100, 100, 1, NewImage.FILL_BLACK);<br> ImageProcessor ip = imp.getProcessor();<br> ip.setRoi(new Rectangle(10, 10, 10, 10));<br> ip.setColor(1);<br> ip.fill();<br> ip.setRoi(new Rectangle(20, 20, 10, 20));<br> ip.setColor(2);<br> ip.fill();<br> ip.setRoi(new Rectangle(40, 40, 10, 30));<br> ip.setColor(3);<br> ip.fill();<br><br> return imp;<br> }<br><br> //Transform an ImagePlus to a Labeling Imp. The original pixel value should be the label afterwards.<br> public static ImgLabeling<Integer, IntType> getIntIntImgLabellingFromLabelMapImagePlus(ImagePlus labelMap)<br> {<br> final RandomAccessibleInterval< IntType > img = ImageJFunctions.wrapNumeric(labelMap);<br> Img< FloatType > img2 = ImageJFunctions.convertFloat(labelMap);<br> final long[] dims = new long[ img.numDimensions() ];<br><br> ImgLabeling< Integer, IntType > labeling = new ImgLabeling< Integer, IntType >( img );<br><br> Cursor<LabelingType<Integer>> labelCursor = labeling.cursor();<br> RandomAccess<FloatType> imageRA = img2.randomAccess();<br><br> // Go through the whole image again and again for every single label, until no more label is found.<br> // If you know a more efficient way to do this, tell me...<br> int currentLabel = 0;<br> boolean anythingFound = true;<br> while (anythingFound)<br> {<br> anythingFound = false;<br> labelCursor.reset();<br><br> //Go through the whole image and add every pixel, that belongs to the currently processed label<br> int count = 0;<br> while (labelCursor.hasNext())<br> {<br> LabelingType<Integer> element = labelCursor.next();<br> imageRA.setPosition(labelCursor);<br><br> int i = (int)(imageRA.get().get());<br> if (i == currentLabel)<br> {<br> element.add(i);<br> anythingFound = true;<br> count ++;<br> }<br> }<br> System.out.println("Number of input pixels in label " + currentLabel + ": " + count);<br> currentLabel++;<br> }<br> ImageJFunctions.show(labeling.getIndexImg());<br> return labeling;<br> }<br><br> //Transform a Region to a binary image, which can be displayed<br> public static < T, F > Img<BitType> labelRegionToBinaryImage(LabelRegion<T> region, Img<F> img)<br> {<br> final long[] pos = new long[ img.numDimensions() ];<br> final long[] dims = new long[ img.numDimensions() ];<br> img.dimensions( dims );<br><br> final Img< BitType > newImage = ArrayImgs.bits( dims );<br><br> RandomAccess<BitType> imageRA = newImage.randomAccess();<br> LabelRegionCursor regionCursor = region.cursor();<br><br> int count = 0;<br> while (regionCursor.hasNext())<br> {<br> //Read yes/no from region<br> BoolType regionElement = regionCursor.next();<br> regionCursor.localize(pos);<br> imageRA.setPosition(pos);<br> BitType imageElement = imageRA.get();<br><br> //Draw binary image pixel by pixel<br> if (regionElement.get())<br> {<br> count++;<br> imageElement.set(true);<br> }<br> else<br> {<br> imageElement.set(false);<br> }<br> }<br> System.out.println("Number of output pixels in region: " + count);<br> return newImage;<br> }<br><br><br><br> //Just to run it<br> public static void main(final String... args) throws IOException<br> {<br> new LabelingExample();<br> }<br>}<br><br><br><br><br>-- <br>Robert Haase<br>Bio-Image informatics, Scientific Computing Facility<br><br>Max Planck Institute CBG<br>Pfotenhauer Str. 108, 01307 Dresden<br>room: 106 (s)<br>phone: +49 351 210 2972<br>fax: +49 351 210 1689<br><br><br>_______________________________________________<br>ImageJ-devel mailing list<br><a href="mailto:ImageJ-devel@imagej.net">ImageJ-devel@imagej.net</a><br>http://imagej.net/mailman/listinfo/imagej-devel<br></blockquote></div><br></body></html>