Skip to content

Instantly share code, notes, and snippets.

@lacan
Created April 1, 2022 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lacan/4bbcc4e57e26c109e76fb327c91300d9 to your computer and use it in GitHub Desktop.
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
// 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