imglib / imglib2-roi

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

Min/Max of LabelRegion are identical #10

Closed IdealOutage closed 9 years ago

IdealOutage commented 9 years ago

Hi @tpietzsch,

The min() and max() values of a LabelRegion are identical. Shouldn't they be the min of the max of the bounding box for the complete Label, like it is stated in the JavaDoc:

The interval bounds represent the bounding box of all pixels having the label.

If they should be equal, is there a possibility to get the size of the bounding box without iterating through the whole LabelRegion?

I have included a small example to illustrate my point.

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.imageio.ImageIO;

import net.imglib2.RandomAccess;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.roi.labeling.ImgLabeling;
import net.imglib2.roi.labeling.LabelRegion;
import net.imglib2.roi.labeling.LabelRegions;
import net.imglib2.roi.labeling.LabelingType;
import net.imglib2.type.numeric.integer.IntType;

public class Test {
    public static void main(String[] args) throws Exception {
        // read simple polygon image
        BufferedImage read = ImageIO.read(new URL(
                "http://i.imgur.com/cZgkFsK.png").openStream());

        ImgLabeling<String, IntType> img = new ImgLabeling<String, IntType>(
                ArrayImgs.ints(read.getWidth(), read.getHeight()));

        // at each black pixel of the polygon add a "1" label.
        RandomAccess<LabelingType<String>> randomAccess = img.randomAccess();
        for (int y = 0; y < read.getHeight(); y++) {
            for (int x = 0; x < read.getWidth(); x++) {
                randomAccess.setPosition(new int[] { x, y });
                Color c = new Color(read.getRGB(x, y));
                if (c.getRed() == Color.black.getRed()) {
                    randomAccess.get().add("1");
                }
            }
        }

        LabelRegions<String> labelRegions = new LabelRegions<String>(img);
        LabelRegion<String> labelRegion = labelRegions.getLabelRegion("1");

        // Shouldn't this be 11,94 - ~787,985
        System.out.println("Bounds: " + labelRegion.min(0) + ","
                + labelRegion.min(1) + "\t-\t" + labelRegion.max(0) + ","
                + labelRegion.max(1));
    }
}
tpietzsch commented 9 years ago

Thank you for noticing this bug, @DanielSeebacher. And thank you so much for the minimal example! I fixed it in 0153fc8d2a566f74c65bc5f22d85fa8669f6f60b. I'll release a new version of imglib2-roi

tpietzsch commented 9 years ago

I released imglib-roi 0.3.1 containing the bug-fix