From 489242a6b29d66ce0babfef99e01e5bc4435074c Mon Sep 17 00:00:00 2001 From: Jean-Marie Burel Date: Wed, 5 Jul 2023 00:21:31 +0100 Subject: [PATCH 1/4] set plane coordinates --- .../igred/omero/roi/GenericShapeWrapper.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java b/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java index 5c9a4e97..2b82a4cc 100644 --- a/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java +++ b/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java @@ -136,9 +136,6 @@ static ShapeList fromImageJ(ij.gui.Roi ijRoi) { * @param ijRoi An ImageJ Roi. */ protected void copyFromIJRoi(ij.gui.Roi ijRoi) { - data.setC(Math.max(-1, ijRoi.getCPosition() - 1)); - data.setZ(Math.max(-1, ijRoi.getZPosition() - 1)); - data.setT(Math.max(-1, ijRoi.getTPosition() - 1)); LengthI size = new LengthI(ijRoi.getStrokeWidth(), UnitsLength.POINT); Color defaultStroke = Optional.ofNullable(Roi.getColor()).orElse(Color.YELLOW); Color defaultFill = Optional.ofNullable(Roi.getDefaultFillColor()).orElse(TRANSPARENT); @@ -147,6 +144,34 @@ protected void copyFromIJRoi(ij.gui.Roi ijRoi) { data.getShapeSettings().setStrokeWidth(size); data.getShapeSettings().setStroke(stroke); data.getShapeSettings().setFill(fill); + // Set the plane + int pos = ijRoi.getPosition(); + int c = ijRoi.getCPosition(); + int z = ijRoi.getZPosition(); + int t = ijRoi.getTPosition(); + ij.ImagePlus ip = ij.WindowManager.getImage(ijRoi.getImageID()); //ijRoi.getImage() returns null + int imageC = ip.getNChannels(); + int imageT = ip.getNFrames(); + int imageZ = ip.getNSlices(); + if (imageC == 1 && imageZ == 1) { + t = pos; + //reset values + c = 1; + z = 1; + } else if (imageZ == 1 && imageT == 1) { + c = pos; + //reset values + z = 1; + t = 1; + } else if (imageC == 1 && imageT == 1) { + z = pos; + //reset values + c = 1; + t = 1; + } + data.setC(Math.max(-1, c - 1)); + data.setZ(Math.max(-1, z - 1)); + data.setT(Math.max(-1, t - 1)); } From 1d054690a5fea0b1236fa745dd0050f8e500d66d Mon Sep 17 00:00:00 2001 From: Jean-Marie Burel Date: Wed, 5 Jul 2023 23:20:17 +0100 Subject: [PATCH 2/4] add null check for test --- .../java/fr/igred/omero/roi/GenericShapeWrapper.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java b/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java index 2b82a4cc..981725ab 100644 --- a/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java +++ b/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java @@ -150,9 +150,14 @@ protected void copyFromIJRoi(ij.gui.Roi ijRoi) { int z = ijRoi.getZPosition(); int t = ijRoi.getTPosition(); ij.ImagePlus ip = ij.WindowManager.getImage(ijRoi.getImageID()); //ijRoi.getImage() returns null - int imageC = ip.getNChannels(); - int imageT = ip.getNFrames(); - int imageZ = ip.getNSlices(); + int imageC = 1; + int imageT = 1; + int imageZ = 1; + if (ip != null) { + imageC = ip.getNChannels(); + imageT = ip.getNFrames(); + imageZ = ip.getNSlices(); + } if (imageC == 1 && imageZ == 1) { t = pos; //reset values From 12ccc40860996509ddcaaa973c42bf798a8d5273 Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Thu, 6 Jul 2023 01:11:04 +0200 Subject: [PATCH 3/4] Only adjust hyperstack coordinates if they're not already set and the corresponding image can be retrieved --- .../igred/omero/roi/GenericShapeWrapper.java | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java b/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java index 981725ab..a9d7881f 100644 --- a/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java +++ b/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java @@ -22,6 +22,7 @@ import fr.igred.omero.Client; import fr.igred.omero.exception.AccessException; import fr.igred.omero.exception.ServiceException; +import ij.ImagePlus; import ij.gui.Line; import ij.gui.Roi; import ij.gui.ShapeRoi; @@ -144,36 +145,37 @@ protected void copyFromIJRoi(ij.gui.Roi ijRoi) { data.getShapeSettings().setStrokeWidth(size); data.getShapeSettings().setStroke(stroke); data.getShapeSettings().setFill(fill); - // Set the plane - int pos = ijRoi.getPosition(); + + // Set the plane int c = ijRoi.getCPosition(); int z = ijRoi.getZPosition(); int t = ijRoi.getTPosition(); - ij.ImagePlus ip = ij.WindowManager.getImage(ijRoi.getImageID()); //ijRoi.getImage() returns null - int imageC = 1; - int imageT = 1; - int imageZ = 1; - if (ip != null) { - imageC = ip.getNChannels(); - imageT = ip.getNFrames(); - imageZ = ip.getNSlices(); - } - if (imageC == 1 && imageZ == 1) { - t = pos; - //reset values - c = 1; - z = 1; - } else if (imageZ == 1 && imageT == 1) { - c = pos; - //reset values - z = 1; - t = 1; - } else if (imageC == 1 && imageT == 1) { - z = pos; - //reset values - c = 1; - t = 1; + + // Adjust coordinates if ROI does not have hyperstack positions + if (!ijRoi.hasHyperStackPosition()) { + ImagePlus imp = ij.WindowManager.getImage(ijRoi.getImageID()); //ijRoi.getImage() returns null + if (imp != null) { + int stackSize = imp.getStackSize(); + int imageC = imp.getNChannels(); + int imageZ = imp.getNSlices(); + int imageT = imp.getNFrames(); + int pos = ijRoi.getPosition(); + + // Reset values + c = 1; + z = 1; + t = 1; + + if (stackSize == imageZ) { + z = pos; + } else if (stackSize == imageC) { + c = pos; + } else if (stackSize == imageT) { + t = pos; + } + } } + data.setC(Math.max(-1, c - 1)); data.setZ(Math.max(-1, z - 1)); data.setT(Math.max(-1, t - 1)); From 7ce4bfec39b34734bb71a713ee59b8d7a3fb042d Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Thu, 6 Jul 2023 02:07:15 +0200 Subject: [PATCH 4/4] Add tests --- .../igred/omero/roi/GenericShapeWrapper.java | 5 +- .../fr/igred/omero/roi/ROI2ImageJTest.java | 74 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java b/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java index a9d7881f..f66d1d5f 100644 --- a/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java +++ b/src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java @@ -153,7 +153,10 @@ protected void copyFromIJRoi(ij.gui.Roi ijRoi) { // Adjust coordinates if ROI does not have hyperstack positions if (!ijRoi.hasHyperStackPosition()) { - ImagePlus imp = ij.WindowManager.getImage(ijRoi.getImageID()); //ijRoi.getImage() returns null + ImagePlus imp = ijRoi.getImage(); + if (imp == null) { + imp = ij.WindowManager.getImage(ijRoi.getImageID()); + } if (imp != null) { int stackSize = imp.getStackSize(); int imageC = imp.getNChannels(); diff --git a/src/test/java/fr/igred/omero/roi/ROI2ImageJTest.java b/src/test/java/fr/igred/omero/roi/ROI2ImageJTest.java index a8b453a1..36c148e7 100644 --- a/src/test/java/fr/igred/omero/roi/ROI2ImageJTest.java +++ b/src/test/java/fr/igred/omero/roi/ROI2ImageJTest.java @@ -19,6 +19,8 @@ import fr.igred.omero.BasicTest; +import ij.IJ; +import ij.ImagePlus; import ij.gui.Arrow; import ij.gui.EllipseRoi; import ij.gui.Line; @@ -457,4 +459,76 @@ void convertPolyline() { assertEquals(polyline.getT(), newPolyline.getT()); } + + @Test + void convertRectangleWithCStack() { + ImagePlus img = IJ.createImage("test", "grayscale", 1000, 1000, 10, 1, 1); + int pos = 4; + img.setPosition(pos); + + Roi ijRoi = new Roi(10, 10, 10, 10); + ijRoi.setPosition(img); + ijRoi.setImage(img); + + assertEquals(pos, ijRoi.getPosition()); + + List roiList = new ArrayList<>(1); + roiList.add(ijRoi); + ROIWrapper roi = ROIWrapper.fromImageJ(roiList, null).get(0); + + RectangleWrapper newRectangle = roi.getShapes().getElementsOf(RectangleWrapper.class).get(0); + + assertEquals(pos - 1, newRectangle.getC()); + assertEquals(0, newRectangle.getZ()); + assertEquals(0, newRectangle.getT()); + } + + + @Test + void convertRectangleWithZStack() { + ImagePlus img = IJ.createImage("test", "grayscale", 1000, 1000, 1, 10, 1); + int pos = 5; + img.setPosition(pos); + + Roi ijRoi = new Roi(10, 10, 10, 10); + ijRoi.setPosition(img); + ijRoi.setImage(img); + + assertEquals(pos, ijRoi.getPosition()); + + List roiList = new ArrayList<>(1); + roiList.add(ijRoi); + ROIWrapper roi = ROIWrapper.fromImageJ(roiList, null).get(0); + + RectangleWrapper newRectangle = roi.getShapes().getElementsOf(RectangleWrapper.class).get(0); + + assertEquals(0, newRectangle.getC()); + assertEquals(pos - 1, newRectangle.getZ()); + assertEquals(0, newRectangle.getT()); + } + + + @Test + void convertRectangleWithTStack() { + ImagePlus img = IJ.createImage("test", "grayscale", 1000, 1000, 1, 1, 10); + int pos = 6; + img.setPosition(pos); + + Roi ijRoi = new Roi(10, 10, 10, 10); + ijRoi.setPosition(img); + ijRoi.setImage(img); + + assertEquals(pos, ijRoi.getPosition()); + + List roiList = new ArrayList<>(1); + roiList.add(ijRoi); + ROIWrapper roi = ROIWrapper.fromImageJ(roiList, null).get(0); + + RectangleWrapper newRectangle = roi.getShapes().getElementsOf(RectangleWrapper.class).get(0); + + assertEquals(0, newRectangle.getC()); + assertEquals(0, newRectangle.getZ()); + assertEquals(pos - 1, newRectangle.getT()); + } + }