Search in sources :

Example 46 with PathObject

use of qupath.lib.objects.PathObject in project qupath by qupath.

the class IJTools method convertToPathObject.

/**
 * Create a {@link PathObject} for a specific ImageJ Roi, using an {@link ImagePlus} to help set properties.
 *
 * @param roi the ImageJ ROI
 * @param downsampleFactor the downsample factor used for rescaling (or 1.0 for no rescaling)
 * @param creator a function
 * @param imp the {@link ImagePlus} associated with this Roi; it is used to determine the xOrigin, yOrigin and image plane
 * @return a {@link PathObject} or null if no object could be created (e.g. the ImageJ roi is null or incompatible)
 *
 * @see #convertToPathObject(Roi, double, double, double, Function, ImagePlane)
 * @since v0.4.0
 */
public static PathObject convertToPathObject(Roi roi, double downsampleFactor, Function<ROI, PathObject> creator, ImagePlus imp) {
    Calibration cal = imp == null ? null : imp.getCalibration();
    var plane = getImagePlane(roi, imp);
    ROI pathROI = IJTools.convertToROI(roi, cal, downsampleFactor, plane);
    if (pathROI == null)
        return null;
    PathObject pathObject = creator.apply(pathROI);
    calibrateObject(pathObject, roi);
    return pathObject;
}
Also used : PathObject(qupath.lib.objects.PathObject) Calibration(ij.measure.Calibration) PixelCalibration(qupath.lib.images.servers.PixelCalibration) EllipseROI(qupath.lib.roi.EllipseROI) PointsROI(qupath.lib.roi.PointsROI) PolygonROI(qupath.lib.roi.PolygonROI) RectangleROI(qupath.lib.roi.RectangleROI) LineROI(qupath.lib.roi.LineROI) PolylineROI(qupath.lib.roi.PolylineROI) ROI(qupath.lib.roi.interfaces.ROI)

Example 47 with PathObject

use of qupath.lib.objects.PathObject in project qupath by qupath.

the class SubcellularDetection method getParentObjects.

@Override
protected Collection<PathObject> getParentObjects(final PluginRunner<BufferedImage> runner) {
    Collection<Class<? extends PathObject>> parentClasses = getSupportedParentObjectClasses();
    List<PathObject> parents = new ArrayList<>();
    for (PathObject parent : getHierarchy(runner).getSelectionModel().getSelectedObjects()) {
        for (Class<? extends PathObject> cls : parentClasses) {
            if (cls.isAssignableFrom(parent.getClass())) {
                parents.add(parent);
                break;
            }
        }
    }
    return parents;
}
Also used : PathObject(qupath.lib.objects.PathObject) ArrayList(java.util.ArrayList) PathClass(qupath.lib.objects.classes.PathClass)

Example 48 with PathObject

use of qupath.lib.objects.PathObject in project qupath by qupath.

the class IJExtension method extractOverlay.

/**
 * Extract an ImageJ overlay for the specified region.
 * @param hierarchy
 * @param request
 * @param options options to control which objects are being displayed
 * @param filter optional additional filter used to determine which objects will be included (may be used in combination with options)
 * @return
 */
public static Overlay extractOverlay(PathObjectHierarchy hierarchy, RegionRequest request, OverlayOptions options, Predicate<PathObject> filter) {
    Overlay overlay = new Overlay();
    double downsample = request.getDownsample();
    double xOrigin = -request.getX() / downsample;
    double yOrigin = -request.getY() / downsample;
    // TODO: Permit filling/unfilling ROIs
    for (PathObject child : hierarchy.getObjectsForRegion(PathObject.class, request, null)) {
        if (filter != null && !filter.test(child))
            continue;
        if (child.hasROI()) {
            // Check if this is displayed - skip it not
            if (options != null && ((child instanceof PathDetectionObject && !options.getShowDetections()) || (child instanceof PathAnnotationObject && !options.getShowAnnotations()) || (child instanceof TMACoreObject && !options.getShowTMAGrid())))
                continue;
            boolean isCell = child instanceof PathCellObject;
            Color color = ColorToolsAwt.getCachedColor(ColorToolsFX.getDisplayedColorARGB(child));
            if (!(isCell && (options == null || !options.getShowCellBoundaries()))) {
                Roi roi = IJTools.convertToIJRoi(child.getROI(), xOrigin, yOrigin, downsample);
                roi.setStrokeColor(color);
                roi.setName(child.getDisplayedName());
                // roi.setStrokeWidth(2);
                overlay.add(roi);
            }
            if (isCell && (options == null || options.getShowCellNuclei())) {
                ROI nucleus = ((PathCellObject) child).getNucleusROI();
                if (nucleus == null)
                    continue;
                Roi roi = IJTools.convertToIJRoi(((PathCellObject) child).getNucleusROI(), xOrigin, yOrigin, downsample);
                roi.setStrokeColor(color);
                roi.setName(child.getDisplayedName() + " - nucleus");
                overlay.add(roi);
            }
        }
    }
    return overlay;
}
Also used : PathDetectionObject(qupath.lib.objects.PathDetectionObject) PathAnnotationObject(qupath.lib.objects.PathAnnotationObject) PathObject(qupath.lib.objects.PathObject) TMACoreObject(qupath.lib.objects.TMACoreObject) Color(java.awt.Color) Overlay(ij.gui.Overlay) Roi(ij.gui.Roi) ROI(qupath.lib.roi.interfaces.ROI) PathCellObject(qupath.lib.objects.PathCellObject)

Example 49 with PathObject

use of qupath.lib.objects.PathObject in project qupath by qupath.

the class PathObjectIOTest method test_IOObjectsGeoJSONImpl.

private void test_IOObjectsGeoJSONImpl(boolean keepMeasurements, GeoJsonExportOptions... options) throws IOException {
    ROI roiDetection = ROIs.createRectangleROI(0, 0, 10, 10, ImagePlane.getDefaultPlane());
    ROI roiAnnotation = ROIs.createRectangleROI(100, 100, 10, 10, ImagePlane.getDefaultPlane());
    ROI roiCell1 = ROIs.createRectangleROI(25, 25, 25, 25, ImagePlane.getDefaultPlane());
    ROI roiCell2 = ROIs.createRectangleROI(12, 12, 5, 5, ImagePlane.getDefaultPlane());
    ROI roiTile = ROIs.createRectangleROI(100, 100, 10, 10, ImagePlane.getDefaultPlane());
    MeasurementList mlDetection = MeasurementListFactory.createMeasurementList(16, MeasurementList.MeasurementListType.GENERAL);
    MeasurementList mlCell = MeasurementListFactory.createMeasurementList(16, MeasurementList.MeasurementListType.GENERAL);
    PathObject myPDO = PathObjects.createDetectionObject(roiDetection, PathClassFactory.getPathClass("PathClassTest1", ColorTools.BLACK), mlDetection);
    PathObject myPAO = PathObjects.createAnnotationObject(roiAnnotation, PathClassFactory.getPathClass("PathClassTest1", ColorTools.BLACK));
    PathObject myPCO = PathObjects.createCellObject(roiCell1, roiCell2, PathClassFactory.getPathClass("PathClassTest2", ColorTools.GREEN), mlCell);
    PathObject myPTO = PathObjects.createTileObject(roiTile, PathClassFactory.getPathClass("PathClassTest2", ColorTools.GREEN), null);
    PathObject myTMA = PathObjects.createTMACoreObject(25, 25, 25, false);
    Collection<PathObject> objs = Arrays.asList(myPDO, myPCO, myPAO, myPTO, myTMA);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    // Add measurements
    mlDetection.addMeasurement("TestMeasurement1", 5.0);
    mlDetection.addMeasurement("TestMeasurement2", 10.0);
    mlCell.addMeasurement("TestMeasurement3", 15.0);
    mlCell.addMeasurement("TestMeasurement4", 20.0);
    // Export to GeoJSON
    PathIO.exportObjectsAsGeoJSON(bos, objs, options);
    // Import from GeoJSON
    List<PathObject> objsBack = new ArrayList<>(PathIO.readObjectsFromGeoJSON(new ByteArrayInputStream(bos.toByteArray())));
    assertEquals(objs.size(), objsBack.size());
    // Array to count number of each PathObject type
    int[] countCheck = new int[] { 0, 0, 0, 0, 0 };
    for (PathObject po : objsBack) {
        if (po == null)
            continue;
        // Test whether po has a ROI
        assertTrue(po.hasROI());
        if (po.isTile()) {
            assertEquals(po.getPathClass(), PathClassFactory.getPathClass("PathClassTest2", ColorTools.GREEN));
            assertSameROIs(po.getROI(), roiTile);
            assertFalse(po.hasMeasurements());
            countCheck[0]++;
        } else if (po.isCell()) {
            assertEquals(po.getPathClass(), PathClassFactory.getPathClass("PathClassTest2", ColorTools.GREEN));
            assertSameROIs(po.getROI(), roiCell1);
            assertSameROIs(((PathCellObject) po).getNucleusROI(), roiCell2);
            if (keepMeasurements) {
                assertTrue(po.hasMeasurements());
                assertSameMeasurements(po.getMeasurementList(), myPCO.getMeasurementList());
            } else
                assertFalse(po.hasMeasurements());
            countCheck[1]++;
        } else if (po.isDetection()) {
            assertEquals(po.getPathClass(), PathClassFactory.getPathClass("PathClassTest1", ColorTools.BLACK));
            assertSameROIs(po.getROI(), roiDetection);
            if (keepMeasurements) {
                assertTrue(po.hasMeasurements());
                assertSameMeasurements(po.getMeasurementList(), myPDO.getMeasurementList());
            } else
                assertFalse(po.hasMeasurements());
            countCheck[2]++;
        } else if (po.isAnnotation()) {
            assertEquals(po.getPathClass(), PathClassFactory.getPathClass("PathClassTest1", ColorTools.BLACK));
            assertSameROIs(po.getROI(), roiAnnotation);
            assertFalse(po.hasMeasurements());
            countCheck[3]++;
        } else if (po.isTMACore()) {
            assertFalse(po.hasMeasurements());
            assertSameROIs(po.getROI(), myTMA.getROI());
            countCheck[4]++;
        }
    }
    assertArrayEquals(countCheck, new int[] { 1, 1, 1, 1, 1 });
}
Also used : PathObject(qupath.lib.objects.PathObject) ByteArrayInputStream(java.io.ByteArrayInputStream) MeasurementList(qupath.lib.measurements.MeasurementList) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ROI(qupath.lib.roi.interfaces.ROI) PathCellObject(qupath.lib.objects.PathCellObject)

Example 50 with PathObject

use of qupath.lib.objects.PathObject in project qupath by qupath.

the class AbstractPathROITool method createNewAnnotation.

/**
 * Create a new annotation & set it in the current viewer.
 * @param e
 * @param x
 * @param y
 * @return
 */
PathObject createNewAnnotation(MouseEvent e, double x, double y) {
    var viewer = getViewer();
    var currentObject = viewer.getSelectedObject();
    var editor = viewer.getROIEditor();
    if (currentObject != null && currentObject.getParent() == null && currentObject.getROI() == editor.getROI() && (editor.isTranslating() || editor.hasActiveHandle())) {
        logger.warn("Creating a new annotation before a previous one was complete - {} will be discarded!", currentObject);
    }
    logger.trace("Creating new annotation at ({}, {}", x, y);
    PathObjectHierarchy hierarchy = viewer.getHierarchy();
    if (hierarchy == null) {
        logger.warn("Cannot create new annotation - no hierarchy available!");
        return null;
    }
    ROI roi = createNewROI(e, x, y, viewer.getImagePlane());
    if (roi == null)
        return null;
    PathObject pathObject = PathObjects.createAnnotationObject(roi, PathPrefs.autoSetAnnotationClassProperty().get());
    var selectionModel = hierarchy.getSelectionModel();
    if (PathPrefs.selectionModeProperty().get() && !selectionModel.noSelection())
        viewer.setSelectedObject(pathObject, true);
    else
        viewer.setSelectedObject(pathObject);
    return pathObject;
}
Also used : PathObjectHierarchy(qupath.lib.objects.hierarchy.PathObjectHierarchy) PathObject(qupath.lib.objects.PathObject) PolylineROI(qupath.lib.roi.PolylineROI) ROI(qupath.lib.roi.interfaces.ROI) PolygonROI(qupath.lib.roi.PolygonROI)

Aggregations

PathObject (qupath.lib.objects.PathObject)182 ArrayList (java.util.ArrayList)84 ROI (qupath.lib.roi.interfaces.ROI)74 PathObjectHierarchy (qupath.lib.objects.hierarchy.PathObjectHierarchy)61 List (java.util.List)48 BufferedImage (java.awt.image.BufferedImage)37 IOException (java.io.IOException)37 PathClass (qupath.lib.objects.classes.PathClass)37 Collectors (java.util.stream.Collectors)35 PathAnnotationObject (qupath.lib.objects.PathAnnotationObject)34 Map (java.util.Map)33 Logger (org.slf4j.Logger)33 LoggerFactory (org.slf4j.LoggerFactory)33 ImageData (qupath.lib.images.ImageData)31 TMACoreObject (qupath.lib.objects.TMACoreObject)31 Collection (java.util.Collection)29 Collections (java.util.Collections)29 HashMap (java.util.HashMap)28 PathObjectTools (qupath.lib.objects.PathObjectTools)26 Arrays (java.util.Arrays)25