use of qupath.lib.objects.PathROIObject in project qupath by qupath.
the class RefineAnnotationsPlugin method getTasks.
@Override
protected Collection<Runnable> getTasks(final PluginRunner<T> runner) {
Collection<? extends PathObject> parentObjects = getParentObjects(runner);
if (parentObjects == null || parentObjects.isEmpty())
return Collections.emptyList();
// Add a single task, to avoid multithreading - which may complicate setting parents
List<Runnable> tasks = new ArrayList<>(1);
PathObjectHierarchy hierarchy = getHierarchy(runner);
double minFragmentSize;
double maxHoleSize, maxHoleSizeTemp;
ImageServer<T> server = getServer(runner);
PixelCalibration cal = server.getPixelCalibration();
if (cal.hasPixelSizeMicrons()) {
double pixelAreaMicrons = cal.getPixelWidthMicrons() * cal.getPixelHeightMicrons();
minFragmentSize = params.getDoubleParameterValue("minFragmentSizeMicrons") / pixelAreaMicrons;
maxHoleSizeTemp = params.getDoubleParameterValue("maxHoleSizeMicrons") / pixelAreaMicrons;
} else {
minFragmentSize = params.getDoubleParameterValue("minFragmentSizePixels");
maxHoleSizeTemp = params.getDoubleParameterValue("maxHoleSizePixels");
}
// Handle negative values
if (maxHoleSizeTemp < 0)
maxHoleSize = Double.POSITIVE_INFINITY;
else
maxHoleSize = maxHoleSizeTemp;
// Want to reset selection
PathObject selected = hierarchy.getSelectionModel().getSelectedObject();
Collection<PathObject> previousSelection = new ArrayList<>(hierarchy.getSelectionModel().getSelectedObjects());
tasks.add(() -> {
List<PathObject> toRemove = new ArrayList<>();
Map<PathROIObject, ROI> toUpdate = new HashMap<>();
for (PathObject pathObject : parentObjects) {
ROI roiOrig = pathObject.getROI();
if (roiOrig == null || !roiOrig.isArea())
continue;
ROI roiUpdated = RoiTools.removeSmallPieces(roiOrig, minFragmentSize, maxHoleSize);
if (roiUpdated == null || roiUpdated.isEmpty())
toRemove.add(pathObject);
else if (roiOrig != roiUpdated && pathObject instanceof PathROIObject) {
toUpdate.put((PathROIObject) pathObject, roiUpdated);
}
}
if (toRemove.isEmpty() && toUpdate.isEmpty())
return;
hierarchy.getSelectionModel().clearSelection();
if (!toRemove.isEmpty())
hierarchy.removeObjects(toRemove, true);
if (!toUpdate.isEmpty()) {
hierarchy.removeObjects(toUpdate.keySet(), true);
toUpdate.forEach((p, r) -> p.setROI(r));
hierarchy.addPathObjects(toUpdate.keySet());
}
hierarchy.getSelectionModel().selectObjects(previousSelection);
hierarchy.getSelectionModel().setSelectedObject(selected, true);
});
return tasks;
}
use of qupath.lib.objects.PathROIObject in project qupath by qupath.
the class MoveTool method mouseDragged.
@Override
public void mouseDragged(MouseEvent e) {
if (mover != null)
mover.stopMoving();
super.mouseDragged(e);
if (!e.isPrimaryButtonDown() || e.isConsumed())
return;
// Handle ROIs if the spacebar isn't down
var viewer = getViewer();
if (!viewer.isSpaceDown()) {
RoiEditor editor = viewer.getROIEditor();
Point2D p = mouseLocationToImage(e, true, false);
// Try moving handle
if (editor != null && editor.hasActiveHandle()) {
double x = p.getX();
double y = p.getY();
if (PathPrefs.usePixelSnappingProperty().get() && editor.getROI() != null && editor.getROI().isArea()) {
x = (int) x;
y = (int) y;
}
ROI updatedROI = editor.setActiveHandlePosition(x, y, viewer.getDownsampleFactor() / 2.0, e.isShiftDown());
if (updatedROI == null)
// This shouldn't occur...?
logger.warn("Updated ROI is null! Will be skipped...");
else {
PathObject selectedObject = viewer.getSelectedObject();
if (selectedObject.getROI() != updatedROI && selectedObject instanceof PathROIObject)
((PathROIObject) selectedObject).setROI(updatedROI);
// TODO: Check event firing frequency!
viewer.getHierarchy().fireObjectsChangedEvent(this, Collections.singleton(selectedObject), true);
// viewer.repaint();
e.consume();
return;
}
}
// Try to translate, if that's what is happening
ROI currentROI = viewer.getCurrentROI();
if (editor != null && editor.isTranslating()) {
Rectangle2D boundsBefore = AwtTools.getBounds2D(currentROI);
ROI translatedROI = editor.updateTranslation(p.getX(), p.getY(), viewer.getServerBounds());
if (translatedROI != null) {
Rectangle2D boundsAfter = AwtTools.getBounds2D(currentROI);
Rectangle2D boundsIntersection = new Rectangle2D.Double();
Rectangle2D.union(boundsBefore, boundsAfter, boundsIntersection);
((PathROIObject) viewer.getSelectedObject()).setROI(translatedROI);
viewer.getHierarchy().fireObjectsChangedEvent(this, Collections.singleton(viewer.getSelectedObject()), true);
// System.err.println("Changing... " + viewer.getHierarchy().nObjects());
// viewer.repaintImageRegion(boundsIntersection, false);
}
pDragging = null;
return;
}
// Try to select objects, if alt is down
if (e.isAltDown()) {
tryToSelect(p.getX(), p.getY(), e.getClickCount() - 1, true, false);
e.consume();
return;
}
}
// Don't allow dragging if 'zoom to fit' selected
if (viewer.getZoomToFit())
return;
// If we don't have a previous point, we aren't dragging (e.g. there was an alt-click)
if (pDragging == null)
return;
// Extract previous coordinates so we can reuse the Point2D object
double xPrevious = pDragging.getX();
double yPrevious = pDragging.getY();
// Calculate how much the image was dragged
pDragging = mouseLocationToImage(e, false, false);
dx = pDragging.getX() - xPrevious;
dy = pDragging.getY() - yPrevious;
// Update the viewer
viewer.setDoFasterRepaint(true);
viewer.setCenterPixelLocation(viewer.getCenterPixelX() - dx, viewer.getCenterPixelY() - dy);
// viewer.setDoFasterRepaint(false);
pDragging = mouseLocationToImage(e, false, false);
lastDragTimestamp = System.currentTimeMillis();
}
use of qupath.lib.objects.PathROIObject in project qupath by qupath.
the class AbstractPathDraggingROITool method mouseDragged.
@Override
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
if (!e.isPrimaryButtonDown()) {
return;
}
var viewer = getViewer();
ROI currentROI = viewer.getCurrentROI() instanceof ROI ? (ROI) viewer.getCurrentROI() : null;
RoiEditor editor = viewer.getROIEditor();
if (currentROI != null && editor.getROI() == currentROI && editor.hasActiveHandle()) {
PathObject pathObject = viewer.getSelectedObject();
Point2D p = mouseLocationToImage(e, true, requestPixelSnapping());
ROI roiUpdated = editor.setActiveHandlePosition(p.getX(), p.getY(), 0.25, e.isShiftDown());
if (roiUpdated != currentROI) {
((PathROIObject) pathObject).setROI(roiUpdated);
viewer.repaint();
}
viewer.getHierarchy().fireObjectsChangedEvent(this, Collections.singleton(pathObject), true);
// editor.setActiveHandlePosition(x, y, minDisplacement, shiftDown)
// currentROI.updateAdjustment(p.getX(), p.getY(), e.isShiftDown());
viewer.repaint();
}
}
use of qupath.lib.objects.PathROIObject in project qupath by qupath.
the class PointsTool method handleAltClick.
/**
* Alt-clicks remove the selected point, or selects a new 'family' of points (i.e. a different object) if
* a point from the current object isn't clicked.
*
* @param x
* @param y
* @param currentObject
* @return
*/
private boolean handleAltClick(double x, double y, PathObject currentObject) {
var viewer = getViewer();
var viewerPlane = viewer.getImagePlane();
PathObjectHierarchy hierarchy = viewer.getHierarchy();
double distance = PathPrefs.pointRadiusProperty().get();
// Remove a point if the current selection has one
if (currentObject != null && PathObjectTools.hasPointROI(currentObject)) {
PointsROI points = (PointsROI) currentObject.getROI();
if (points.getImagePlane().equals(viewerPlane)) {
ROI points2 = removeNearbyPoint(points, x, y, distance);
if (points != points2) {
((PathROIObject) currentObject).setROI(points2);
hierarchy.updateObject(currentObject, false);
// hierarchy.fireHierarchyChangedEvent(this, currentObject);
return true;
}
}
}
// Activate a points object if there is one
for (PathObject pathObject : hierarchy.getPointObjects(PathObject.class)) {
// Don't check the current object again
if (pathObject == currentObject || !pathObject.getROI().getImagePlane().equals(viewerPlane))
continue;
// See if we've almost clicked on a point
if (((PointsROI) pathObject.getROI()).getNearest(x, y, distance) != null) {
viewer.setSelectedObject(pathObject);
// hierarchy.getSelectionModel().setSelectedPathObject(pathObject);
return true;
}
}
// Select nothing
viewer.setSelectedObject(null);
return true;
}
use of qupath.lib.objects.PathROIObject in project qupath by qupath.
the class PointsTool method mouseDragged.
@Override
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
if (!e.isPrimaryButtonDown() || e.isConsumed()) {
return;
}
var viewer = getViewer();
RoiEditor editor = viewer.getROIEditor();
if (!(editor.getROI() instanceof PointsROI) || !editor.hasActiveHandle())
return;
PointsROI points = getCurrentPoints();
// Find out the coordinates in the image domain & update the adjustment
Point2D pAdjusting = mouseLocationToImage(e, true, requestPixelSnapping());
// double radius = PointsROI.defaultPointRadiusProperty().get();
PointsROI points2 = (PointsROI) editor.setActiveHandlePosition(pAdjusting.getX(), pAdjusting.getY(), 0.25, e.isShiftDown());
if (points2 == points)
return;
PathROIObject currentObject = (PathROIObject) viewer.getSelectedObject();
currentObject.setROI(points2);
viewer.repaint();
// viewer.getHierarchy().fireHierarchyChangedEvent(this, currentObject);
// // points.updateAdjustment(pAdjusting.getX(), pAdjusting.getY(), e.isShiftDown());
//
// // Point2 p = points.getNearest(pAdjusting.getX(), pAdjusting.getY(), radius);
// if (p == null) {
// } else {
// p.setLocation(pAdjusting.getX(), pAdjusting.getY());
// // points.resetMeasurements();
// viewer.repaint();
// }
}
Aggregations