From 9fa488b95f4fde1543f1318231a53cad80c55a36 Mon Sep 17 00:00:00 2001 From: jean-marie burel Date: Thu, 6 Jul 2023 06:47:57 +0100 Subject: [PATCH] Set ROI plane coordinates (#65) * set plane coordinates * add null check for test * Only adjust hyperstack coordinates if they're not already set and the corresponding image can be retrieved * Add tests --- .../igred/omero/roi/GenericShapeWrapper.java | 41 +++++++++- .../fr/igred/omero/roi/ROI2ImageJTest.java | 74 +++++++++++++++++++ 2 files changed, 112 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..f66d1d5f 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; @@ -136,9 +137,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 +145,43 @@ protected void copyFromIJRoi(ij.gui.Roi ijRoi) { data.getShapeSettings().setStrokeWidth(size); data.getShapeSettings().setStroke(stroke); data.getShapeSettings().setFill(fill); + + // Set the plane + int c = ijRoi.getCPosition(); + int z = ijRoi.getZPosition(); + int t = ijRoi.getTPosition(); + + // Adjust coordinates if ROI does not have hyperstack positions + if (!ijRoi.hasHyperStackPosition()) { + ImagePlus imp = ijRoi.getImage(); + if (imp == null) { + imp = ij.WindowManager.getImage(ijRoi.getImageID()); + } + 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)); } 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()); + } + }