Last active
August 26, 2024 19:41
-
-
Save ajay1685/034fcb27d82198e0a7870567bd82f9d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Rotate annotations or detections in QuPath. | |
* | |
* This creates new transformed objects that are then added to the current image hierarchy. | |
* | |
* Note: Tested and working with version 0.5.1. | |
* Note: Watch for objects extending beyond image bounds | |
* Note: Inspired from: https://gist.github.com/petebankhead/2cab4873e001ab42e03c864666284c5f | |
* TODO: Use RoiTools.transformROI(roi, transform.getRotateInstance(-Math.PI/2)) | |
* | |
* @author Ajay Zalavadia | |
*/ | |
import java.awt.geom.AffineTransform | |
// Parameters to adjust | |
// Use positive value for clockwise and negative values for anti-clockwise rotation | |
// set to zero to disable rotation | |
var rotate = -25 | |
boolean keepExisting = false | |
// Get selected objects & its ROI | |
def selected = getSelectedObjects().findAll {it.hasROI()} | |
if (!selected) { | |
print 'No objects selected!' | |
return | |
} | |
def server = getCurrentServer() | |
def transformedObjects = selected.collect{ | |
def transform = new AffineTransform() | |
if (rotate != 0){ | |
double theta = Math.toRadians(rotate) | |
def roi = it.getROI() | |
double centerX = roi.getCentroidX() | |
double centerY = roi.getCentroidY() | |
transform.rotate(theta, centerX, centerY) | |
} | |
PathObjectTools.transformObject(it, transform, false) | |
} | |
if (!keepExisting) | |
removeObjects(selected, true) | |
addObjects(transformedObjects) | |
selectObjects(transformedObjects) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment