Created
June 30, 2018 14:30
-
-
Save petebankhead/8d541effc8898d6a07edd4ed95b6929c to your computer and use it in GitHub Desktop.
Export ROIs made in QuPath as .zip files that can be imported into ImageJ
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 ROIs made in QuPath as .zip files that can be imported into ImageJ. | |
| * | |
| * This makes several assumptions: | |
| * - the rois apply to the whole image, without offsets or downsampling | |
| * - only the outer ROI for cells is export (rather than the outer ROI + nucleus) | |
| * - the image is 2D (i.e. it doesn't attempt to set z-slice properties) | |
| * | |
| * The ROIs will be put into a 'rois' subdirectory of the QuPath project directory. | |
| * | |
| * It is possible to use 'Run -> Run for project (without save)' for batch export. | |
| * | |
| * @author Pete Bankhead | |
| */ | |
| import ij.plugin.frame.RoiManager | |
| import qupath.imagej.objects.ROIConverterIJ | |
| import qupath.lib.awt.color.ColorToolsAwt | |
| import qupath.lib.objects.helpers.PathObjectColorToolsAwt | |
| import qupath.lib.scripting.QPEx | |
| // Create a suitable export path | |
| def imageName = getProjectEntry().getImageName() | |
| def pathExport = buildFilePath(PROJECT_BASE_DIR, 'rois') | |
| mkdirs(pathExport) | |
| pathExport = buildFilePath(pathExport, imageName + '.zip') | |
| // Get objects | |
| def hierarchy = QPEx.getCurrentHierarchy() | |
| def pathObjects = hierarchy.getObjects(null, null) | |
| // Filter out objects without a ROI (i.e. the root object of the hierarchy) | |
| pathObjects = pathObjects.findAll {it.hasROI()} | |
| if (pathObjects.isEmpty()) | |
| return | |
| // Convert to ImageJ ROIs | |
| def roiMan = new RoiManager(false) | |
| pathObjects.each { pathObject -> | |
| // Get ImageJ ROI - assume no offset, no downsampleling | |
| def roi = ROIConverterIJ.convertToIJRoi(pathObject.getROI(), 0, 0, 1) | |
| // Set properties according to the object | |
| def color = ColorToolsAwt.getCachedColor(PathObjectColorToolsAwt.getDisplayedColor(pathObject)) | |
| roi.setStrokeColor(color) | |
| roi.setName(pathObject.getDisplayedName()) | |
| roiMan.addRoi(roi) | |
| } | |
| // Save using the default methods of the ImageJ ROI Manager | |
| roiMan.runCommand("save", pathExport) | |
| roiMan.runCommand("reset") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment