Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set plane coordinates #65

Merged
merged 4 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}

}