Search in sources :

Example 1 with OvalRoi

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

the class ROIHandler method openROIs.

/**
 * Opens the rois and converts them into ImageJ Rois.
 *
 * @param retrieve The OMEXML store.
 * @param images The imageJ object.
 * @param isOMERO <code>true</code> if data stored in OMERO,
 *        <code>false</code> otherwise.
 * @param roisMode Determines whether to import Rois to overlay or RoiManager
 */
public static void openROIs(IMetadata retrieve, ImagePlus[] images, boolean isOMERO, String roisMode) {
    if (!(retrieve instanceof OMEXMLMetadata))
        return;
    int nextRoi = 0;
    RoiManager manager = RoiManager.getInstance();
    OME root = (OME) retrieve.getRoot();
    Roi roi;
    Float sw;
    Color sc;
    Color fc;
    int imageCount = images.length;
    for (int imageNum = 0; imageNum < imageCount; imageNum++) {
        int roiCount = root.sizeOfROIList();
        if (roiCount > 0 && manager == null && roisMode.equals(ImporterOptions.ROIS_MODE_MANAGER)) {
            manager = new RoiManager();
        }
        for (int roiNum = 0; roiNum < roiCount; roiNum++) {
            Union shapeSet = root.getROI(roiNum).getUnion();
            int shapeCount = shapeSet.sizeOfShapeList();
            for (int shape = 0; shape < shapeCount; shape++) {
                Shape shapeObject = shapeSet.getShape(shape);
                roi = null;
                sw = null;
                sc = null;
                fc = null;
                int c = 0;
                int z = 0;
                int t = 0;
                if (shapeObject instanceof Ellipse) {
                    Ellipse ellipse = (Ellipse) shapeObject;
                    int cx = ellipse.getX().intValue();
                    int cy = ellipse.getY().intValue();
                    int rx = ellipse.getRadiusX().intValue();
                    int ry = ellipse.getRadiusY().intValue();
                    roi = new OvalRoi(cx - rx, cy - ry, rx * 2, ry * 2);
                    if (ellipse.getStrokeColor() != null) {
                        ome.xml.model.primitives.Color StrokeColor = ellipse.getStrokeColor();
                        sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
                        if (isOMERO) {
                            sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
                        }
                    }
                    if (ellipse.getFillColor() != null) {
                        ome.xml.model.primitives.Color FillColor = ellipse.getFillColor();
                        fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
                        if (isOMERO) {
                            fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
                        }
                    }
                    if (ellipse.getStrokeWidth() != null) {
                        sw = ellipse.getStrokeWidth().value().floatValue();
                    }
                } else if (shapeObject instanceof ome.xml.model.Line) {
                    ome.xml.model.Line line = (ome.xml.model.Line) shapeObject;
                    int x1 = line.getX1().intValue();
                    int x2 = line.getX2().intValue();
                    int y1 = line.getY1().intValue();
                    int y2 = line.getY2().intValue();
                    roi = new Line(x1, y1, x2, y2);
                    if (line.getStrokeColor() != null) {
                        ome.xml.model.primitives.Color StrokeColor = line.getStrokeColor();
                        sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
                        if (isOMERO) {
                            sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
                        }
                    }
                    if (line.getFillColor() != null) {
                        ome.xml.model.primitives.Color FillColor = line.getFillColor();
                        fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
                        if (isOMERO) {
                            fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
                        }
                    }
                    if (line.getStrokeWidth() != null) {
                        sw = line.getStrokeWidth().value().floatValue();
                    }
                } else if (shapeObject instanceof Point) {
                    Point point = (Point) shapeObject;
                    int x = point.getX().intValue();
                    int y = point.getY().intValue();
                    roi = new PointRoi(x, y);
                    if (point.getStrokeColor() != null) {
                        ome.xml.model.primitives.Color StrokeColor = point.getStrokeColor();
                        sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
                        if (isOMERO) {
                            sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
                        }
                    }
                    if (point.getFillColor() != null) {
                        ome.xml.model.primitives.Color FillColor = point.getFillColor();
                        fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
                        if (isOMERO) {
                            fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
                        }
                    }
                    if (point.getStrokeWidth() != null) {
                        sw = point.getStrokeWidth().value().floatValue();
                    }
                } else if (shapeObject instanceof Polyline) {
                    Polyline polyline = (Polyline) shapeObject;
                    String points = polyline.getPoints();
                    int[][] coordinates = parsePoints(points);
                    roi = new PolygonRoi(coordinates[0], coordinates[1], coordinates[0].length, Roi.POLYLINE);
                    if (polyline.getStrokeColor() != null) {
                        ome.xml.model.primitives.Color StrokeColor = polyline.getStrokeColor();
                        sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
                        if (isOMERO) {
                            sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
                        }
                    }
                    if (polyline.getFillColor() != null) {
                        ome.xml.model.primitives.Color FillColor = polyline.getFillColor();
                        fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
                        if (isOMERO) {
                            fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
                        }
                    }
                    if (polyline.getStrokeWidth() != null) {
                        sw = polyline.getStrokeWidth().value().floatValue();
                    }
                } else if (shapeObject instanceof Polygon) {
                    Polygon polygon = (Polygon) shapeObject;
                    String points = polygon.getPoints();
                    int[][] coordinates = parsePoints(points);
                    roi = new PolygonRoi(coordinates[0], coordinates[1], coordinates[0].length, Roi.POLYGON);
                    if (polygon.getStrokeColor() != null) {
                        ome.xml.model.primitives.Color StrokeColor = polygon.getStrokeColor();
                        sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
                        if (isOMERO) {
                            sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
                        }
                    }
                    if (polygon.getFillColor() != null) {
                        ome.xml.model.primitives.Color FillColor = polygon.getFillColor();
                        fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
                        if (isOMERO) {
                            fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
                        }
                    }
                    if (polygon.getStrokeWidth() != null) {
                        sw = polygon.getStrokeWidth().value().floatValue();
                    }
                } else if (shapeObject instanceof ome.xml.model.Label) {
                    // add support for TextROI's
                    ome.xml.model.Label label = (ome.xml.model.Label) shapeObject;
                    double x = label.getX().doubleValue();
                    double y = label.getY().doubleValue();
                    String labelText = label.getText();
                    int size = label.getFontSize().value().intValue();
                    Font font = new Font(labelText, Font.PLAIN, size);
                    roi = new TextRoi((int) x, (int) y, labelText, font);
                    if (label.getStrokeColor() != null) {
                        ome.xml.model.primitives.Color StrokeColor = label.getStrokeColor();
                        sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
                        if (isOMERO) {
                            sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
                        }
                    }
                    if (label.getFillColor() != null) {
                        ome.xml.model.primitives.Color FillColor = label.getFillColor();
                        fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
                        if (isOMERO) {
                            fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
                        }
                    }
                    if (label.getStrokeWidth() != null) {
                        sw = label.getStrokeWidth().value().floatValue();
                    }
                } else if (shapeObject instanceof ome.xml.model.Rectangle) {
                    ome.xml.model.Rectangle rectangle = (ome.xml.model.Rectangle) shapeObject;
                    int x = rectangle.getX().intValue();
                    int y = rectangle.getY().intValue();
                    int w = rectangle.getWidth().intValue();
                    int h = rectangle.getHeight().intValue();
                    roi = new Roi(x, y, w, h);
                    if (rectangle.getStrokeColor() != null) {
                        ome.xml.model.primitives.Color StrokeColor = rectangle.getStrokeColor();
                        sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
                        if (isOMERO) {
                            sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
                        }
                    }
                    if (rectangle.getFillColor() != null) {
                        ome.xml.model.primitives.Color FillColor = rectangle.getFillColor();
                        fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
                        if (isOMERO) {
                            fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
                        }
                    }
                    if (rectangle.getStrokeWidth() != null) {
                        sw = rectangle.getStrokeWidth().value().floatValue();
                    }
                }
                if (roi != null) {
                    String roiLabel = shapeObject.getText();
                    if (roiLabel == null) {
                        roiLabel = shapeObject.getID();
                    }
                    roi.setName(roiLabel);
                    if (Prefs.showAllSliceOnly) {
                        if (shapeObject.getTheC() != null) {
                            c = shapeObject.getTheC().getValue();
                        }
                        if (shapeObject.getTheZ() != null) {
                            z = shapeObject.getTheZ().getValue();
                        }
                        if (shapeObject.getTheT() != null) {
                            t = shapeObject.getTheT().getValue();
                        }
                        // ImageJ expects 1-based indexing, opposed to
                        // 0-based indexing in OME
                        // Roi positions differ between hyperstacks and normal stacks
                        ImagePlus imp = images[imageNum];
                        if (imp.getNChannels() > 1) {
                            c++;
                        }
                        if (imp.getNSlices() > 1) {
                            z++;
                        }
                        if (imp.getNFrames() > 1) {
                            t++;
                        }
                        if (c == 0)
                            c = 1;
                        if (t == 0)
                            t = 1;
                        if (z == 0)
                            z = 1;
                        if (imp.getNChannels() == 1 && imp.getNSlices() == 1) {
                            roi.setPosition(t);
                        } else if (imp.getNChannels() == 1 && imp.getNFrames() == 1) {
                            roi.setPosition(z);
                        } else if (imp.getNSlices() == 1 && imp.getNFrames() == 1) {
                            roi.setPosition(c);
                        } else if (imp.isHyperStack()) {
                            roi.setPosition(c, z, t);
                        }
                    }
                    if (sw == null) {
                        roi.setStrokeWidth((float) 1);
                    }
                    if (sw != null) {
                        if (sw == 0) {
                            sw = (float) 1;
                        }
                        roi.setStrokeWidth(sw);
                    }
                    if (sc != null) {
                        roi.setStrokeColor(sc);
                    }
                    if (roisMode.equals(ImporterOptions.ROIS_MODE_MANAGER)) {
                        manager.add(images[imageNum], roi, nextRoi++);
                    } else if (roisMode.equals(ImporterOptions.ROIS_MODE_OVERLAY)) {
                        Overlay overlay = images[imageNum].getOverlay();
                        if (overlay == null) {
                            overlay = new Overlay(roi);
                            images[imageNum].setOverlay(overlay);
                        } else {
                            overlay.add(roi);
                        }
                    }
                }
            }
        }
        if (roiCount > 0 && manager != null) {
            manager.setAlwaysOnTop(true);
            manager.runCommand("show all with labels");
        }
    }
}
Also used : Ellipse(ome.xml.model.Ellipse) Shape(ome.xml.model.Shape) Rectangle(java.awt.Rectangle) TextRoi(ij.gui.TextRoi) OvalRoi(ij.gui.OvalRoi) Union(ome.xml.model.Union) Font(java.awt.Font) PolygonRoi(ij.gui.PolygonRoi) Polygon(ome.xml.model.Polygon) Overlay(ij.gui.Overlay) PointRoi(ij.gui.PointRoi) OME(ome.xml.model.OME) Color(java.awt.Color) Point(ome.xml.model.Point) 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) RoiManager(ij.plugin.frame.RoiManager) Line(ij.gui.Line) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) Polyline(ome.xml.model.Polyline)

Example 2 with OvalRoi

use of ij.gui.OvalRoi 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)

Example 3 with OvalRoi

use of ij.gui.OvalRoi in project TrakEM2 by trakem2.

the class AreaWrapper method makeBrush.

/**
 * This method could get tones of improvement, which should be pumped upstream into ImageJ's RoiBrush class which is creating it at every while(true) {} iteration!!!
 * The returned area has its coordinates centered around 0,0
 */
public static Area makeBrush(int diameter, double mag) {
    if (diameter < 1)
        return null;
    if (mag >= 1)
        return new Area(new OvalRoi(-diameter / 2, -diameter / 2, diameter, diameter).getPolygon());
    // else, create a smaller brush and transform it up, i.e. less precise, less points to store -but precision matches what the eye sees, and allows for much better storage -less points.
    int screen_diameter = (int) (diameter * mag);
    // can't paint at this mag with this diameter
    if (0 == screen_diameter)
        return null;
    Area brush = new Area(new OvalRoi(-screen_diameter / 2, -screen_diameter / 2, screen_diameter, screen_diameter).getPolygon());
    // scale to world coordinates
    AffineTransform at = new AffineTransform();
    at.scale(1 / mag, 1 / mag);
    return brush.createTransformedArea(at);
// smooth out edges
/*
		Polygon pol = new OvalRoi(-diameter/2, -diameter/2, diameter, diameter).getPolygon();
		Polygon pol2 = new Polygon();
		// cheap and fast: skip every other point, since all will be square angles
		for (int i=0; i<pol.npoints; i+=2) {
			pol2.addPoint(pol.xpoints[i], pol.ypoints[i]);
		}
		return new Area(pol2);
		// the above works nice, but then the fill and fill-remove don't work properly (there are traces in the edges)
		// Needs a workround: before adding/substracting, enlarge the polygon to have square edges
		*/
}
Also used : Area(java.awt.geom.Area) AffineTransform(java.awt.geom.AffineTransform) OvalRoi(ij.gui.OvalRoi) Point(java.awt.Point)

Example 4 with OvalRoi

use of ij.gui.OvalRoi in project TrakEM2 by trakem2.

the class DownsamplerTest method main.

/**
 * @param args
 */
public static void main(final String[] args) {
    new ImageJ();
    final Timer timer = new Timer();
    final ImagePlus imp = new ImagePlus("/home/saalfeld/tmp/fetter-example.tif");
    // final ImagePlus imp = new ImagePlus( "/home/albert/Desktop/t2/fetter-example.tif" );
    // final ImagePlus imp = new ImagePlus( "/home/saalfeld/Desktop/norway.jpg" );
    imp.show();
    final ImageProcessor ip = imp.getProcessor().duplicate();
    System.out.println("short");
    final ShortProcessor ipShort = (ShortProcessor) ip.convertToShort(false);
    for (int i = 0; i < n; ++i) {
        timer.start();
        testShort(ipShort);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("downsampleShort() + downsampleByteProcessor() (independent short with byte mapping + alpha)");
    for (int i = 0; i < n; ++i) {
        final Pair<ShortProcessor, byte[]> ba = new Pair<ShortProcessor, byte[]>(ipShort, (byte[]) ipShort.convertToByte(true).getPixels());
        final ByteProcessor alpha = new ByteProcessor(ipShort.getWidth(), ipShort.getHeight(), (byte[]) ipShort.convertToByte(true).getPixels(), null);
        timer.start();
        testShortAlphaIndependently(ba, alpha);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("downsampleShortProcessor() + convertToByte() + downsampleByteProcessor() (independent short + byte mapping + alpha)");
    for (int i = 0; i < n; ++i) {
        final ByteProcessor alpha = new ByteProcessor(ipShort.getWidth(), ipShort.getHeight(), (byte[]) ipShort.convertToByte(true).getPixels(), null);
        timer.start();
        testShortAlphaByteMappingIndependently(ipShort, alpha);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("float");
    final FloatProcessor ipFloat = (FloatProcessor) ip.convertToFloat();
    for (int i = 0; i < n; ++i) {
        timer.start();
        testFloat(ipFloat);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("independent float with byte mapping + alpha");
    for (int i = 0; i < n; ++i) {
        final Pair<FloatProcessor, byte[]> ba = new Pair<FloatProcessor, byte[]>(ipFloat, (byte[]) ipShort.convertToByte(true).getPixels());
        final ByteProcessor alpha = new ByteProcessor(ipShort.getWidth(), ipShort.getHeight(), (byte[]) ipShort.convertToByte(true).getPixels(), null);
        timer.start();
        testFloatAlphaIndependently(ba, alpha);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("byte");
    final ByteProcessor ipByte = (ByteProcessor) ip.convertToByte(true);
    for (int i = 0; i < n; ++i) {
        timer.start();
        testByte(ipByte);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("2 x byte");
    final ByteProcessor ipByte2 = (ByteProcessor) ipByte.duplicate();
    for (int i = 0; i < n; ++i) {
        timer.start();
        testByte(ipByte);
        testByte(ipByte2);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("color");
    final ColorProcessor ipColor = (ColorProcessor) ip.convertToRGB();
    for (int i = 0; i < n; ++i) {
        timer.start();
        testColor(ipColor);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("independent color with byte mapping + alpha");
    for (int i = 0; i < n; ++i) {
        final byte[][] rgb = new byte[4][ipColor.getWidth() * ipColor.getHeight()];
        ipColor.getRGB(rgb[0], rgb[1], rgb[2]);
        final Pair<ColorProcessor, byte[][]> ba = new Pair<ColorProcessor, byte[][]>(ipColor, rgb);
        final ByteProcessor alpha = new ByteProcessor(ipShort.getWidth(), ipShort.getHeight(), (byte[]) ipShort.convertToByte(true).getPixels(), null);
        timer.start();
        testColorAlphaIndependently(ba, alpha);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("alpha + outside");
    for (int i = 0; i < n; ++i) {
        ByteProcessor outside = new ByteProcessor(ipByte.getWidth(), ipByte.getHeight());
        outside.setRoi(new OvalRoi(100, 100, ipByte.getWidth() - 200, ipByte.getHeight() - 200));
        outside.setValue(255);
        outside.fill(outside.getMask());
        final Pair<ByteProcessor, ByteProcessor> ba = new Pair<ByteProcessor, ByteProcessor>(ipByte, outside);
        timer.start();
        testAlphaOutside(ba);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("outside");
    for (int i = 0; i < n; ++i) {
        ByteProcessor outside = new ByteProcessor(ipByte.getWidth(), ipByte.getHeight());
        outside.setRoi(new OvalRoi(100, 100, ipByte.getWidth() - 200, ipByte.getHeight() - 200));
        outside.setValue(255);
        outside.fill(outside.getMask());
        timer.start();
        testOutside(outside);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
// System.out.println( "byte integral" );
// final ByteProcessor ipByteI = ( ByteProcessor )ipShort.convertToByte( true );
// 
// for ( int i = 0; i < 10; ++i )
// {
// timer.start();
// testByteIntegral( ipByteI );
// final long t = timer.stop();
// System.out.println( i + ": " + t  + "ms" );
// }
}
Also used : ByteProcessor(ij.process.ByteProcessor) FloatProcessor(ij.process.FloatProcessor) OvalRoi(ij.gui.OvalRoi) ImagePlus(ij.ImagePlus) ShortProcessor(ij.process.ShortProcessor) ImageJ(ij.ImageJ) ImageProcessor(ij.process.ImageProcessor) ColorProcessor(ij.process.ColorProcessor) Timer(mpicbg.util.Timer) Pair(mpicbg.trakem2.util.Downsampler.Pair)

Example 5 with OvalRoi

use of ij.gui.OvalRoi in project TrakEM2 by trakem2.

the class Utils method paint.

/**
 * Paints an approximation of the pipe into the set of slices.
 */
public static void paint(final Pipe pipe, final Map<Layer, ImageProcessor> slices, final int value, final float scale) {
    final VectorString3D vs = pipe.asVectorString3D();
    // one pixel
    vs.resample(1);
    final double[] px = vs.getPoints(0);
    final double[] py = vs.getPoints(1);
    final double[] pz = vs.getPoints(2);
    final double[] pr = vs.getDependent(0);
    // For each point
    for (int i = 0; i < px.length - 1; i++) {
        final ImageProcessor ip = slices.get(pipe.getLayerSet().getNearestLayer(pz[i]));
        if (null == ip)
            continue;
        final OvalRoi ov = new OvalRoi((int) ((px[i] - pr[i]) * scale), (int) ((py[i] - pr[i]) * scale), (int) (pr[i] * 2 * scale), (int) (pr[i] * 2 * scale));
        ip.setRoi(ov);
        ip.setValue(value);
        ip.fill(ip.getMask());
    }
}
Also used : ImageProcessor(ij.process.ImageProcessor) VectorString3D(ini.trakem2.vector.VectorString3D) OvalRoi(ij.gui.OvalRoi)

Aggregations

OvalRoi (ij.gui.OvalRoi)5 ImagePlus (ij.ImagePlus)3 EllipseRoi (ij.gui.EllipseRoi)2 Line (ij.gui.Line)2 PointRoi (ij.gui.PointRoi)2 PolygonRoi (ij.gui.PolygonRoi)2 Roi (ij.gui.Roi)2 ShapeRoi (ij.gui.ShapeRoi)2 TextRoi (ij.gui.TextRoi)2 ImageProcessor (ij.process.ImageProcessor)2 OME (ome.xml.model.OME)2 Point (ome.xml.model.Point)2 ImageJ (ij.ImageJ)1 Overlay (ij.gui.Overlay)1 RoiManager (ij.plugin.frame.RoiManager)1 ByteProcessor (ij.process.ByteProcessor)1 ColorProcessor (ij.process.ColorProcessor)1 FloatProcessor (ij.process.FloatProcessor)1 ShortProcessor (ij.process.ShortProcessor)1 VectorString3D (ini.trakem2.vector.VectorString3D)1