Created
May 16, 2022 12:03
-
-
Save petebankhead/79fdd34efe373baf5c615bfe24a341d2 to your computer and use it in GitHub Desktop.
QuPath script to export annotation to GeoJSON using calibrated pixel sizes
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
| /** | |
| * Export QuPath annotations in GeoJSON using calibrated pixel sizes | |
| * (rather than pixel units). | |
| * | |
| * Written for QuPath v0.3.2 | |
| * | |
| * @author Pete Bankhead | |
| */ | |
| // Define export directory (or null if we don't want to export, but just print the GeoJSON) | |
| def basePath = buildFilePath(PROJECT_BASE_DIR, 'geojson') | |
| // Get all annotations | |
| def annotations = getAnnotationObjects() | |
| // Optionally filter to keep only annotations with a specified class | |
| // annotations = annotations.findAll(a -> a.getPathClass() == getPathClass('Tumor')) | |
| // Get the pixel size | |
| def server = getCurrentServer() | |
| def cal = server.getPixelCalibration() | |
| // Scale all the annotations by the pixel size | |
| // Here, we don't check the units | |
| def transform = java.awt.geom.AffineTransform.getScaleInstance(cal.pixelWidth, cal.pixelHeight) | |
| def annotationsScaled = annotations.collect(a -> PathObjectTools.transformObject(a, transform, true)) | |
| // Convert to GeoJSON | |
| def json = GsonTools.getInstance(true).toJson(annotationsScaled) | |
| // Export to a file, or print if no path is specified | |
| if (basePath) { | |
| mkdirs(basePath) // Ensure directory exists | |
| def name = GeneralTools.getNameWithoutExtension(getProjectEntry().getImageName()) + ".geojson" | |
| def file = new File(basePath, name) | |
| file.text = json | |
| println "${annotationsScaled.size()} annotation(s) export to ${file.getAbsolutePath()}" | |
| } else | |
| println json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment