Search in sources :

Example 1 with PolylineROI

use of qupath.lib.roi.PolylineROI in project qupath by qupath.

the class IJTools method convertToIJRoi.

/**
 * Convert a QuPath ROI to an ImageJ Roi.
 * @param <T>
 * @param pathROI
 * @param xOrigin x-origin indicating relationship of ImagePlus to the original image, as stored in ImageJ Calibration object
 * @param yOrigin y-origin indicating relationship of ImagePlus to the original image, as stored in ImageJ Calibration object
 * @param downsampleFactor downsample factor at which the ImagePlus was extracted from the full-resolution image
 * @return
 */
public static <T extends PathImage<ImagePlus>> Roi convertToIJRoi(ROI pathROI, double xOrigin, double yOrigin, double downsampleFactor) {
    if (pathROI instanceof PolygonROI)
        return ROIConverterIJ.convertToPolygonROI((PolygonROI) pathROI, xOrigin, yOrigin, downsampleFactor);
    if (pathROI instanceof RectangleROI)
        return ROIConverterIJ.getRectangleROI((RectangleROI) pathROI, xOrigin, yOrigin, downsampleFactor);
    if (pathROI instanceof EllipseROI)
        return ROIConverterIJ.convertToOvalROI((EllipseROI) pathROI, xOrigin, yOrigin, downsampleFactor);
    if (pathROI instanceof LineROI)
        return ROIConverterIJ.convertToLineROI((LineROI) pathROI, xOrigin, yOrigin, downsampleFactor);
    if (pathROI instanceof PolylineROI)
        return ROIConverterIJ.convertToPolygonROI((PolylineROI) pathROI, xOrigin, yOrigin, downsampleFactor);
    if (pathROI instanceof PointsROI)
        return ROIConverterIJ.convertToPointROI((PointsROI) pathROI, xOrigin, yOrigin, downsampleFactor);
    // If we have any other kind of shape, create a general shape roi
    if (pathROI != null && pathROI.isArea()) {
        // TODO: Deal with non-AWT area ROIs!
        Shape shape = RoiTools.getArea(pathROI);
        // "scaleX", "shearY", "shearX", "scaleY", "translateX", "translateY"
        shape = new AffineTransform(1.0 / downsampleFactor, 0, 0, 1.0 / downsampleFactor, xOrigin, yOrigin).createTransformedShape(shape);
        return ROIConverterIJ.setIJRoiProperties(new ShapeRoi(shape), pathROI);
    }
    // TODO: Integrate ROI not supported exception...?
    return null;
}
Also used : PolygonROI(qupath.lib.roi.PolygonROI) ShapeRoi(ij.gui.ShapeRoi) RectangleROI(qupath.lib.roi.RectangleROI) Shape(java.awt.Shape) EllipseROI(qupath.lib.roi.EllipseROI) PolylineROI(qupath.lib.roi.PolylineROI) PointsROI(qupath.lib.roi.PointsROI) AffineTransform(java.awt.geom.AffineTransform) LineROI(qupath.lib.roi.LineROI)

Example 2 with PolylineROI

use of qupath.lib.roi.PolylineROI in project qupath by qupath.

the class AbstractPathROITool method mousePressed.

@Override
public void mousePressed(MouseEvent e) {
    super.mousePressed(e);
    if (!e.isPrimaryButtonDown() || e.isConsumed()) {
        return;
    }
    var viewer = getViewer();
    PathObjectHierarchy hierarchy = viewer.getHierarchy();
    if (hierarchy == null)
        return;
    PathObject currentObject = viewer.getSelectedObject();
    ROI currentROI = currentObject == null ? null : currentObject.getROI();
    RoiEditor editor = viewer.getROIEditor();
    boolean adjustingPolygon = (currentROI instanceof PolygonROI || currentROI instanceof PolylineROI) && editor.getROI() == currentROI && (editor.isTranslating() || editor.hasActiveHandle());
    // If we're adjusting a polygon/polyline with an appropriate tool, return at leave it up to the tool to handle the custom things
    if (adjustingPolygon) {
        if (viewer.getActiveTool() == PathTools.POLYGON || viewer.getActiveTool() == PathTools.POLYLINE)
            return;
        else {
            viewer.getHierarchy().getSelectionModel().clearSelection();
            viewer.getHierarchy().fireHierarchyChangedEvent(currentObject);
        }
    }
    // Find out the coordinates in the image domain
    Point2D p2 = mouseLocationToImage(e, false, requestPixelSnapping());
    double xx = p2.getX();
    double yy = p2.getY();
    if (xx < 0 || yy < 0 || xx >= viewer.getServerWidth() || yy >= viewer.getServerHeight())
        return;
    // If we are double-clicking & we don't have a polygon, see if we can access a ROI
    if (!PathPrefs.selectionModeProperty().get() && e.getClickCount() > 1) {
        // Reset parent... for now
        resetConstrainedAreaParent();
        tryToSelect(xx, yy, e.getClickCount() - 2, false);
        e.consume();
        return;
    }
    // Set the current parent object based on the first click
    setConstrainedAreaParent(hierarchy, xx, yy, Collections.emptyList());
    // Create a new annotation
    PathObject pathObject = createNewAnnotation(e, xx, yy);
    if (pathObject == null)
        return;
    // Start editing the ROI immediately
    editor.setROI(pathObject.getROI());
    editor.grabHandle(xx, yy, viewer.getMaxROIHandleSize() * 1.5, e.isShiftDown());
}
Also used : PathObjectHierarchy(qupath.lib.objects.hierarchy.PathObjectHierarchy) PolygonROI(qupath.lib.roi.PolygonROI) PathObject(qupath.lib.objects.PathObject) RoiEditor(qupath.lib.roi.RoiEditor) PolylineROI(qupath.lib.roi.PolylineROI) Point2D(java.awt.geom.Point2D) PolylineROI(qupath.lib.roi.PolylineROI) ROI(qupath.lib.roi.interfaces.ROI) PolygonROI(qupath.lib.roi.PolygonROI)

Aggregations

PolygonROI (qupath.lib.roi.PolygonROI)2 PolylineROI (qupath.lib.roi.PolylineROI)2 ShapeRoi (ij.gui.ShapeRoi)1 Shape (java.awt.Shape)1 AffineTransform (java.awt.geom.AffineTransform)1 Point2D (java.awt.geom.Point2D)1 PathObject (qupath.lib.objects.PathObject)1 PathObjectHierarchy (qupath.lib.objects.hierarchy.PathObjectHierarchy)1 EllipseROI (qupath.lib.roi.EllipseROI)1 LineROI (qupath.lib.roi.LineROI)1 PointsROI (qupath.lib.roi.PointsROI)1 RectangleROI (qupath.lib.roi.RectangleROI)1 RoiEditor (qupath.lib.roi.RoiEditor)1 ROI (qupath.lib.roi.interfaces.ROI)1