Created
April 1, 2022 13:20
-
-
Save lacan/4bbcc4e57e26c109e76fb327c91300d9 to your computer and use it in GitHub Desktop.
[Create ImageJ Rois from QuPath Cells] Generates a RoiSet.zip file for the current image for all cell objects #QuPath #ImageJ
This file contains 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
// Create cytoplasm ROI before adding it to the Roi Manager? | |
def getcytoplasm = true | |
// START OF SCRIPT | |
// Use ImageJ's RoiManager | |
def rm = RoiManager.getInstance() == null ? new RoiManager() : RoiManager.getInstance() | |
rm,.reset() | |
// Pick up active image name | |
def name = getProjectEntry().getImageName() | |
// Prepare a folder inside the QuPath Project called 'ROIs' | |
def saveFolder = new File( getProject().getPath().toFile().getParent(), "ROIs" ) | |
saveFolder.mkdirs() | |
// Get the cells and iterate through them | |
def cells = getCellObjects() | |
cells.eachWithIndex{ cell, idx -> | |
// Get the ROIs, convert them to ImageJ at full resolution, assuming that the | |
// top left corner has coordinates 0,0 ( will not work for crops) | |
def nuc = cell.getNucleusROI() | |
def ce = cell.getROI() | |
def ijnuc = IJTools.convertToIJRoi( nuc, 0,0, 1 ) | |
def ijcell = IJTools.convertToIJRoi( ce, 0,0, 1 ) | |
// Name and add to the RoiManager | |
ijnuc.setName( "Nucleus " + idx ) | |
ijcell.setName( "Cell " + idx ) | |
rm.addRoi(ijnuc) | |
rm.addRoi(ijcell) | |
// Create a Cytoplasm ROI if requested | |
if( getcytoplasm ) { | |
def cytoGeom = ce.getGeometry().difference( nuc.getGeometry() ) | |
def cyto = GeometryTools.geometryToROI( cytoGeom, cell.getROI().getImagePlane() ) | |
def ijcyto = IJTools.convertToIJRoi( cyto, 0,0, 1 ) | |
ijcyto.setName( "Cytoplasm " + idx ) | |
rm.addRoi(ijcyto) | |
} | |
} | |
// Save everything in the RoiManager to a zip file | |
rm.runCommand( "Save", new File( saveFolder, name + "_Rois.zip" ).getAbsolutePath() ); | |
// Imports | |
import ij.plugin.frame.RoiManager |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment