Search in sources :

Example 1 with EllipseRoi

use of ij.gui.EllipseRoi in project bioformats by openmicroscopy.

the class ROIHandler method saveROIs.

/**
 * Save ROIs in the ROI manager to the given MetadataStore.
 *
 * @param store Where to store the rois.
 */
public static void saveROIs(MetadataStore store) {
    Roi[] rois = readFromOverlays();
    if (rois == null || rois.length == 0) {
        rois = readFromRoiManager();
    }
    if (rois == null || rois.length == 0)
        return;
    List<String> discardList = new ArrayList<String>();
    String roiID = null;
    OME root = (OME) store.getRoot();
    int roicount = root.sizeOfROIList();
    int cntr = roicount;
    ImagePlus imp = WindowManager.getCurrentImage();
    for (int i = 0; i < rois.length; i++) {
        String polylineID = MetadataTools.createLSID("Shape", cntr, 0);
        roiID = MetadataTools.createLSID("ROI", cntr, 0);
        Roi ijRoi = rois[i];
        int c = ijRoi.getCPosition() - 1;
        int z = ijRoi.getZPosition() - 1;
        int t = ijRoi.getTPosition() - 1;
        ImagePlus image = WindowManager.getImage(ijRoi.getImageID());
        if (image == null) {
            // pick the current image in that case
            image = imp;
        }
        int pos = ijRoi.getPosition();
        if (imp != null) {
            if (imp.getNChannels() == 1 && imp.getNSlices() == 1) {
                t = pos - 1;
            } else if (imp.getNChannels() == 1 && imp.getNFrames() == 1) {
                z = pos - 1;
            } else if (imp.getNSlices() == 1 && imp.getNFrames() == 1) {
                c = pos - 1;
            }
            if (t > imp.getNFrames() - 1 || c > imp.getNChannels() - 1 || z > imp.getNSlices() - 1) {
                // 
                continue;
            }
        }
        if (ijRoi.isDrawingTool()) {
            // Checks if the given roi is a Text box/Arrow/Rounded Rectangle
            if (ijRoi.getTypeAsString().matches("Text")) {
                if (ijRoi instanceof TextRoi) {
                    store.setLabelID(polylineID, cntr, 0);
                    storeText((TextRoi) ijRoi, store, cntr, 0, c, z, t);
                }
            } else if (ijRoi.getTypeAsString().matches("Rectangle")) {
                if (ijRoi instanceof Roi) {
                    store.setRectangleID(polylineID, cntr, 0);
                    storeRectangle(ijRoi, store, cntr, 0, c, z, t);
                }
            } else {
                roiID = null;
                String type = ijRoi.getName();
                IJ.log("ROI ID : " + type + " ROI type : " + "Arrow (Drawing Tool) is not supported");
            }
        } else if (ijRoi instanceof OvalRoi) {
            // Check if its an oval or ellipse ROI
            store.setEllipseID(polylineID, cntr, 0);
            storeOval((OvalRoi) ijRoi, store, cntr, 0, c, z, t);
        } else if (ijRoi instanceof Line) {
            // Check if its a Line or Arrow ROI
            boolean checkpoint = ijRoi.isDrawingTool();
            if (!checkpoint) {
                store.setLineID(polylineID, cntr, 0);
                storeLine((Line) ijRoi, store, cntr, 0, c, z, t);
            } else {
                roiID = null;
                String type = ijRoi.getName();
                IJ.log("ROI ID : " + type + " ROI type : " + "Arrow (Drawing Tool) is not supported");
            }
        } else if (ijRoi instanceof PolygonRoi || ijRoi instanceof EllipseRoi) {
            if (ijRoi.getTypeAsString().matches("Polyline") || ijRoi.getTypeAsString().matches("Freeline") || ijRoi.getTypeAsString().matches("Angle")) {
                store.setPolylineID(polylineID, cntr, 0);
                storePolygon((PolygonRoi) ijRoi, store, cntr, 0, c, z, t);
            } else if (ijRoi.getTypeAsString().matches("Point")) {
                store.setPointID(polylineID, cntr, 0);
                storePoint((PointRoi) ijRoi, store, cntr, 0, c, z, t);
            } else if (ijRoi.getTypeAsString().matches("Polygon") || ijRoi.getTypeAsString().matches("Freehand") || ijRoi.getTypeAsString().matches("Traced") || ijRoi.getTypeAsString().matches("Oval")) {
                store.setPolygonID(polylineID, cntr, 0);
                storePolygon((PolygonRoi) ijRoi, store, cntr, 0, c, z, t);
            }
        } else if (ijRoi instanceof ShapeRoi) {
            Roi[] subRois = ((ShapeRoi) ijRoi).getRois();
            for (int q = 0; q < subRois.length; q++) {
                polylineID = MetadataTools.createLSID("Shape", cntr, q);
                roiID = MetadataTools.createLSID("ROI", cntr, q);
                Roi ijShape = subRois[q];
                if (ijShape.isDrawingTool()) {
                    // Checks if the given roi is a Text box/Arrow/Rounded Rectangle
                    if (ijShape.getTypeAsString().matches("Text")) {
                        if (ijShape instanceof TextRoi) {
                            store.setLabelID(polylineID, cntr, q);
                            storeText((TextRoi) ijShape, store, cntr, q, c, z, t);
                        }
                    } else if (ijShape.getTypeAsString().matches("Rectangle")) {
                        if (ijShape instanceof Roi) {
                            store.setRectangleID(polylineID, cntr, q);
                            storeRectangle(ijShape, store, cntr, q, c, z, t);
                        }
                    } else {
                        roiID = null;
                        String type = ijShape.getName();
                        IJ.log("ROI ID : " + type + " ROI type : " + "Arrow (Drawing Tool) is not supported");
                    }
                } else if (ijShape instanceof Line) {
                    boolean checkpoint = ijShape.isDrawingTool();
                    if (!checkpoint) {
                        store.setLineID(polylineID, cntr, 0);
                        storeLine((Line) ijShape, store, cntr, 0, c, z, t);
                    } else {
                        roiID = null;
                        String type1 = ijShape.getName();
                        discardList.add(type1);
                        IJ.log("ROI ID : " + type1 + " ROI type : " + "Arrow (DrawingTool) is not supported");
                    }
                } else if (ijShape instanceof OvalRoi) {
                    store.setEllipseID(polylineID, cntr, q);
                    storeOval((OvalRoi) ijShape, store, cntr, q, c, z, t);
                } else if (ijShape instanceof PolygonRoi || ijShape instanceof EllipseRoi) {
                    if (ijShape.getTypeAsString().matches("Polyline") || ijShape.getTypeAsString().matches("Freeline") || ijShape.getTypeAsString().matches("Angle")) {
                        store.setPolylineID(polylineID, cntr, q);
                        storePolygon((PolygonRoi) ijShape, store, cntr, q, c, z, t);
                    } else if (ijShape.getTypeAsString().matches("Point")) {
                        store.setPointID(polylineID, cntr, q);
                        storePoint((PointRoi) ijShape, store, cntr, q, c, z, t);
                    } else if (ijShape.getTypeAsString().matches("Polygon") || ijShape.getTypeAsString().matches("Freehand") || ijShape.getTypeAsString().matches("Traced") || ijShape.getTypeAsString().matches("Oval")) {
                        store.setPolygonID(polylineID, cntr, q);
                        storePolygon((PolygonRoi) ijShape, store, cntr, q, c, z, t);
                    }
                } else if (ijShape.getTypeAsString().matches("Rectangle")) {
                    store.setRectangleID(polylineID, cntr, q);
                    storeRectangle(ijShape, store, cntr, q, c, z, t);
                } else {
                    roiID = null;
                    String type = ijShape.getName();
                    IJ.log("ROI ID : " + type + " ROI type : " + ijShape.getTypeAsString() + "is not supported");
                }
            }
        } else if (ijRoi.getTypeAsString().matches("Rectangle")) {
            // Check if its a Rectangle or Rounded Rectangle ROI
            store.setRectangleID(polylineID, cntr, 0);
            storeRectangle(ijRoi, store, cntr, 0, c, z, t);
        } else {
            roiID = null;
            String type = ijRoi.getName();
            IJ.log("ROI ID : " + type + " ROI type : " + rois[cntr].getTypeAsString() + "is not supported");
        }
        // Save Roi's using ROIHandler
        if (roiID != null) {
            store.setROIID(roiID, cntr);
            store.setImageROIRef(roiID, 0, cntr);
            cntr++;
        }
    }
}
Also used : ShapeRoi(ij.gui.ShapeRoi) OME(ome.xml.model.OME) ArrayList(java.util.ArrayList) TextRoi(ij.gui.TextRoi) OvalRoi(ij.gui.OvalRoi) PolygonRoi(ij.gui.PolygonRoi) TextRoi(ij.gui.TextRoi) OvalRoi(ij.gui.OvalRoi) PointRoi(ij.gui.PointRoi) EllipseRoi(ij.gui.EllipseRoi) ShapeRoi(ij.gui.ShapeRoi) Roi(ij.gui.Roi) ImagePlus(ij.ImagePlus) Point(ome.xml.model.Point) Line(ij.gui.Line) PolygonRoi(ij.gui.PolygonRoi) EllipseRoi(ij.gui.EllipseRoi)

Aggregations

ImagePlus (ij.ImagePlus)1 EllipseRoi (ij.gui.EllipseRoi)1 Line (ij.gui.Line)1 OvalRoi (ij.gui.OvalRoi)1 PointRoi (ij.gui.PointRoi)1 PolygonRoi (ij.gui.PolygonRoi)1 Roi (ij.gui.Roi)1 ShapeRoi (ij.gui.ShapeRoi)1 TextRoi (ij.gui.TextRoi)1 ArrayList (java.util.ArrayList)1 OME (ome.xml.model.OME)1 Point (ome.xml.model.Point)1