Skip to content

Instantly share code, notes, and snippets.

@caner-ercan
Last active February 16, 2024 16:53
Show Gist options
  • Select an option

  • Save caner-ercan/fac4b8d82c3f3a07ef83d915bad2a8ff to your computer and use it in GitHub Desktop.

Select an option

Save caner-ercan/fac4b8d82c3f3a07ef83d915bad2a8ff to your computer and use it in GitHub Desktop.
#StarDist Cell Detection #QuPath
//don't forget to change the stardist model location
// this script will detect the cells and calculate shape, intensity features
// iff you have a cell classifier model you can add it too at line 53
// the output measurements will be in a folder named 'cellMeasurements' in the project directory
//caner 15.02.23
import qupath.ext.stardist.StarDist2D
import qupath.lib.images.servers.*
// Specify the model directory (you will need to change this!)
def modelDir = "CHANGE_ME"
def pathModel = buildFilePath(modelDir)
double pixelSize = getCurrentServer().getPixelCalibration().getAveragedPixelSize()
def stardist = StarDist2D.builder(pathModel)
.threshold(0.1) // Prediction threshold
.normalizePercentiles(1, 99) // Percentile normalization
.pixelSize(pixelSize) // Resolution for detection
.cellExpansion(5.0) // Approximate cells based upon nucleus expansion
.cellConstrainScale(1.5) // Constrain cell expansion using nucleus size
.measureShape() // Add shape measurements
.measureIntensity() // Add cell measurements (in all compartments)
.constrainToParent(false)
.includeProbability(false)
.build()
// Run detection for the selected objects
def imageData = getCurrentImageData()
selectAnnotations();
def pathObjects = getSelectedObjects()
if (pathObjects.isEmpty()) {
Dialogs.showErrorMessage("StarDist", "Please select a parent object!")
return
}
stardist.detectObjects(imageData, pathObjects)
println getProjectEntry().getImageName()+' Done!'
selectDetections();
runPlugin('qupath.lib.algorithms.IntensityFeaturesPlugin', '{"pixelSizeMicrons": pixelSize, "region": "ROI", "tileSizeMicrons": 25.0, "colorOD": true, "colorStain1": true, "colorStain2": true, "colorStain3": false, "colorRed": false, "colorGreen": false, "colorBlue": false, "colorHue": false, "colorSaturation": false, "colorBrightness": false, "doMean": true, "doStdDev": true, "doMinMax": true, "doMedian": true, "doHaralick": true, "haralickDistance": 1, "haralickBins": 32}');
selectDetections();
runPlugin('qupath.lib.algorithms.IntensityFeaturesPlugin', '{"pixelSizeMicrons": pixelSize, "region": "Cell nucleus", "tileSizeMicrons": 25.0, "colorOD": true, "colorStain1": true, "colorStain2": true, "colorStain3": false, "colorRed": false, "colorGreen": false, "colorBlue": false, "colorHue": false, "colorSaturation": false, "colorBrightness": false, "doMean": true, "doStdDev": true, "doMinMax": true, "doMedian": true, "doHaralick": true, "haralickDistance": 1, "haralickBins": 32}');
addShapeMeasurements("AREA", "LENGTH", "CIRCULARITY", "SOLIDITY", "MAX_DIAMETER", "MIN_DIAMETER", "NUCLEUS_CELL_RATIO")
//Activate the line below after you have a QuPath cell classifier in your model
//runObjectClassifier("cellClassifier")
//Export data
// the output measurements will be in a folder named 'cellMeasurements' in the project directory
def exportDir = buildFilePath(PROJECT_BASE_DIR, 'cellMeasurements')
def checkDir = new File(exportDir)
if (!checkDir.exists()){
checkDir.mkdir()
};
saveDetectionMeasurements(exportDir, getProjectEntry().getImageName())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment