imglib / imglib2-roi

Regions of interest (ROIs) and labelings for ImgLib2
Other
8 stars 8 forks source link

LabelRegions returns an empty LabelRegion if no Labels are present #11

Closed IdealOutage closed 9 years ago

IdealOutage commented 9 years ago

LabelRegions will return a single empty LabelRegion if no Labels are present. I've provided a short example to illustrate the issue below. In my opinion, if there is no data present then LabelRegions shouldn't return a single LabelRegion. Otherwise we would have to check if we have a correct LabelRegion everytime we want to use it.


@Test
public void testLabelRegion() {

    // create a 3d imglabeling (50x50x3)
    ImgLabeling<String, IntType> imgLabeling =
        new ImgLabeling<String, IntType>(ArrayImgs.ints(50, 50, 3));
    Cursor<LabelingType<String>> localizingCursor =
        imgLabeling.localizingCursor();

    // fill the interval between 20,20,1 and 30, 30, 1 with some data.
    while (localizingCursor.hasNext()) {
        localizingCursor.fwd();
        if (localizingCursor.getDoublePosition(0) >= 20 &&
            localizingCursor.getDoublePosition(0) < 30)
        {
            if (localizingCursor.getDoublePosition(1) >= 20 &&
                localizingCursor.getDoublePosition(1) < 30)
            {
                if (localizingCursor.getDoublePosition(2) == 1) {
                    localizingCursor.get().add("1");
                }
            }
        }
    }

    // take a hyperslice from the first plane, there isn't a single label here
    // even if there isn't a label, LabelRegions will at least return one
    // LabelRegion
    for (LabelRegion<String> labelRegion : new LabelRegions<String>(Views
        .hyperSlice(imgLabeling, 2, 0)))
    {
        // and this LabelRegion has some really weird min and max values
        System.err.println("Min: " +
            Arrays.toString(Intervals.minAsIntArray(labelRegion)) + "\tMax:" +
            Arrays.toString(Intervals.maxAsIntArray(labelRegion)));
    }

    // take a hyperslice from the second plane, in this plane we wrote some data
    // and get the LabelRegion as expected
    for (LabelRegion<String> labelRegion : new LabelRegions<String>(Views
        .hyperSlice(imgLabeling, 2, 1)))
    {
        // and now we have a nice looking labelregion with proper min/max values
        System.out.println("Min: " +
            Arrays.toString(Intervals.minAsIntArray(labelRegion)) + "\tMax:" +
            Arrays.toString(Intervals.maxAsIntArray(labelRegion)));
    }
}
tpietzsch commented 9 years ago

@DanielSeebacher yup, this is a bug. A fix is on the way... Thanks for the catch!

tpietzsch commented 9 years ago

@DanielSeebacher it should be fixed by this commit 2ccd92b955b296c47a3b48ee424968d5e5791a4a