Skip to content

Commit

Permalink
Set ROI plane coordinates (#65)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jburel authored Jul 6, 2023
1 parent 5b143ff commit 9fa488b
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/main/java/fr/igred/omero/roi/GenericShapeWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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));
}


Expand Down
74 changes: 74 additions & 0 deletions src/test/java/fr/igred/omero/roi/ROI2ImageJTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Roi> 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<Roi> 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<Roi> 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());
}

}

0 comments on commit 9fa488b

Please sign in to comment.