Total area of the cells detection (cytoplasm+nuclear)

42 views
Skip to first unread message

Malcolm Lim

unread,
Aug 5, 2018, 2:14:04 AM8/5/18
to QuPath users
Hello Qupath users,

I have been using Qupath to count cells and I use this script to batch generate a list of filenames and cell numbers (which i copy paste from the console into excel). 
The individual cell area can be seen via Measurement tabs, but how do i go about including the total area of the cells detection (cytoplasm+nuclear) in my script?
This is so I can batch run the script on 3000+ files.

best wishes
Malcolm   

name = getProjectEntry().getImageName()
annotations = getAnnotationObjects()
detections = getDetectionObjects()
cells = getCellObjects()

print name + "_" + (annotations.size() + "_annotations")
print name + "_" + (detections.size() + "_detections")
println name + "_" + (cells.size() + "_cells")

micros...@gmail.com

unread,
Aug 5, 2018, 3:11:08 AM8/5/18
to QuPath users
Note that cell area counted this way is usually not a great idea, due to the strong interaction between cell density and the Cell Expansion value when generating cells.  QuPath does not truly trace any outline of the cell (with the occasion exception of the Cell+membrane detection).  Hopefully it fits your application well though.

Pete

unread,
Aug 5, 2018, 3:29:58 AM8/5/18
to QuPath users
It's the same idea as above (with the same limitations), but you can also do it with a 'stream'... should give the same answer, but looks a bit shorter.

The idea is that it takes the list of cells, maps each cell to a number (the area measurement),  filters out the missing ones (there probably aren't any), and adds all the remaining numbers.

// Calculate total area of cells with a stream
double totalCellAreas = cells.stream()
 
.mapToDouble({c -> c.getMeasurementList().getMeasurementValue('Cell: Area')})
 
.filter({d -> !Double.isNaN(d)})
 
.sum()
print name + '_' + totalCellAreas + ' total cell area microns^2'

The above script uses the area measurement that is created when the cells are detected, which is likely the one I would use.  It's also possible to calculate the area from the ROIs of the cells themselves.  However this is not guaranteed to give the same result... although it should be close.  The stored measurements are based on counting pixels, while the calculated ones are based on ROI vertices; also, there's a Smooth boundaries option during cell detection that tends to give nicer-looking cell boundaries, as well as requiring less memory to store them, but this smoothing can also have some impact on the exact area measurements.

// Calculate areas using ROIs (may differ slightly from stored measurements!)
cells
= getCellObjects()
def pxWidthMicrons = getCurrentImageData().getServer().getPixelWidthMicrons()
def pxHeightMicrons = getCurrentImageData().getServer().getPixelHeightMicrons()
int skippedROIs = 0
double totalROIArea = 0
for (c in cells) {
   
double area = c.getROI().getScaledArea(pxWidthMicrons, pxHeightMicrons)
   
if (!Double.isNaN(area)) {
        totalROIArea
+= area
   
} else
        skippedROIs
++
}
print 'Total area of cells by ROI: ' + totalROIArea + ' microns^2'
print 'Number skipped: ' + skippedROIs

Reply all
Reply to author
Forward
0 new messages