tissue detection within an annotation

971 views
Skip to first unread message

enmunari

unread,
Aug 13, 2018, 12:36:30 PM8/13/18
to QuPath users
Hi Pete,
Is it possible to run the simple tissue detection command within an annotated area of a whole section? I have been trying but qu path does not seem to allow me...
Thank you!

Pete

unread,
Aug 13, 2018, 12:43:20 PM8/13/18
to QuPath users
I'm afraid not, only for the entire image or for TMA cores.

Anything less simple would currently require an ImageJ macro or script... or if you have an H-DAB image (or are willing to tell QuPath you do), you might find Analyze -> Region identification -> Create cytokeratin annotations useful.  For this to work, you'll need to make sure the 'Downsample factor' is sufficiently high and possibly set the 'DAB threshold' high as well... then reset the classification of the annotation is creates (a bit awkward, since tissue detection isn't really the intended use of the command...).

enmunari

unread,
Aug 13, 2018, 1:04:01 PM8/13/18
to QuPath users
Is there another way to get rid of white space (non-tissue)? i want to exclude white space from the calculated area in order to get a precise number of positive cells /mm^2 (i am counting CD8 positive cells)...Thanks

Pete

unread,
Aug 13, 2018, 1:06:11 PM8/13/18
to QuPath users
Within your original annotation, you can use the brush or wand too with the 'Alt' key pressed to remove parts from a region (including creating holes).  Is that what you mean?

It's better to do this before running the detection; trying to modify the annotation latter will be slow.

enmunari

unread,
Aug 13, 2018, 1:19:27 PM8/13/18
to QuPath users
ok i'll try with the wand tool... One more thing, please: in my images (lung cancer) there's always some black pigment (see attached image, right hand side); is there a way to avoid qu path considering these as dab staining? thank you!!

micros...@gmail.com

unread,
Aug 13, 2018, 1:42:32 PM8/13/18
to QuPath users
I wasn't able to see an image, but for black background you might consider a later step that removes all nuclei with an OD over a certain threshold, or use a color vector set which isolates black from brown.  Depending on what you are measuring and how, though, you can do this different ways.


If you ran Simple tissue detection to get your annotation in the first place, you might want to try a lower Max fill area as well.  If you have to manually annotate, disregard :)  When I did not want to take an extra coding step to remove the whitespace, I would often run something like the following (use your own Threshold of course!):

Simple tissue detection.JPG



enmunari

unread,
Aug 13, 2018, 1:58:15 PM8/13/18
to QuPath users
Sorry! Here is the image!
thanks!
dab and pigment.png

micros...@gmail.com

unread,
Aug 13, 2018, 2:23:28 PM8/13/18
to QuPath users
Are you using "Hematoxylin" or "OD sum" to generate your cells?  I often have trouble using the Hematoxylin setting when the DAB overlaps the nucleus so much.  Hard to get accurate nuclei.  So if you are using the "OD Sum" for nuclear detection you can probably use a method that is roughly...

1. Detect cells
2. Analyze->Calculate features->Add intensity features: Use OD sum, Mean or Median (median could take much longer), or some color vector that you find defines your areas you want to exclude.  This will only work well if your cell expansion is small enough to not dilute the value with whitespace.
3. Use the one line script setCellIntensityClassifications("Your OD sum variable name here", 1.5)
4. Remove all positive cells.  Those will be the ones that are darker than the 1.5 value in the previous script.
5.  Reset detection classifications
6. Classify your cells again, this time using something more like setCellIntensityClassifications("DAB mean of something", 0.2)

That should all be workable as a single script... but the specifics depend on how you are going about your cell detection, and how you are picking thresholds currently.

enmunari

unread,
Aug 13, 2018, 3:56:55 PM8/13/18
to QuPath users
Sorry but i cannot get the script going...maybe i am typing it wrong? i did points 1 and 2, using Color Transforms: Optical density sum and Basic features: Mean. Could you please write it as you had to put it into the editor so i can copy and paste it? Thank you!! 

enmunari

unread,
Aug 13, 2018, 4:06:40 PM8/13/18
to QuPath users
Also, i did not understand if this is going to give me back the number of cells/mm^2...

micros...@gmail.com

unread,
Aug 13, 2018, 8:12:45 PM8/13/18
to QuPath users
The number of cells/mm^2 show up as long as your final classification works.  It should always be obtainable through the Annotation Measurements.  That part is up to you, this part is just removing anything that is too "dark" and you will have to figure out what the threshold is for that.  Play with the setCellIntensityClassifications until you are happy with the cutoff.

script1.JPG

I used the above to calculate my features, but preferred pixel size you choose may be different.  Adapt the rest accordingly, but in these it will be 0.5.

script2.JPG

That shows the measurement in the detection key:values listing in the lower left for a single cell.  That value can sometimes be hard to copy, so I use Pete's printing script so that I can copy and paste it. https://gist.github.com/Svidro/5e4c29630e8d2ef36988184987d1028f#file-print-a-list-of-detection-object-measurements-groovy

I copied the last entry from the text window below the script into
setCellIntensityClassifications("ROI: 0.50 µm per pixel: OD Sum:  Mean", 1.5)


Then I want to remove all of the Positive cells (if I am happy with the 1.5 threshold) and use a variant of :https://gist.github.com/Svidro/33558ac3bd9f68a5ec2428f74550831f#file-removing-specific-annotations-groovy
 

class1
= getPathClass('Positive')
toRemove = getDetectionObjects().findAll { it.getPathClass() == class1}
// Remove annotations meaning that criteria
removeObjects
(toRemove, true)

Finally I would use something like:
setCellIntensityClassifications("Cytoplasm: DAB OD mean", 0.2)

And look at my annotation measurements table which has per mm^2

script3.JPG

The most difficult part of this might be the name of the setCellIntensityClassification string.  Things like the mu symbol did not work as well in version 1.2, so if you are still on that you may need to replace the mu symbol or add it in each time you load QuPath and open the script.  1.3 makes it much easier to work with the micrometer symbol.

enmunari

unread,
Aug 14, 2018, 2:37:26 AM8/14/18
to QuPath users
I tried and it worked! Thank you so much!

enmunari

unread,
Aug 16, 2018, 2:18:37 AM8/16/18
to QuPath users
Hi, i tried to put all the steps as a script through the workflow from cell detection onward, but the software seems not working...
Here is the script i am using:

runPlugin('qupath.imagej.detect.nuclei.WatershedCellDetection', '{"detectionImageBrightfield": "Optical density sum",  "requestedPixelSizeMicrons": 0.5,  "backgroundRadiusMicrons": 8.0,  "medianRadiusMicrons": 0.0,  "sigmaMicrons": 1.5,  "minAreaMicrons": 10.0,  "maxAreaMicrons": 400.0,  "threshold": 0.1,  "maxBackground": 2.0,  "watershedPostProcess": true,  "excludeDAB": false,  "cellExpansionMicrons": 5.0,  "includeNuclei": true,  "smoothBoundaries": true,  "makeMeasurements": true}');
runPlugin('qupath.lib.algorithms.IntensityFeaturesPlugin', '{"pixelSizeMicrons": 0.5,  "region": "ROI",  "tileSizeMicrons": 25.0,  "colorOD": true,  "colorStain1": false,  "colorStain2": false,  "colorStain3": false,  "colorRed": false,  "colorGreen": false,  "colorBlue": false,  "colorHue": false,  "colorSaturation": false,  "colorBrightness": false,  "doMean": true,  "doStdDev": false,  "doMinMax": false,  "doMedian": false,  "doHaralick": false,  "haralickDistance": 1,  "haralickBins": 32}');
setCellIntensityClassifications("ROI: 0.50 µm per pixel: OD Sum:  Mean", 1.1)
class1 = getPathClass('Positive') 
toRemove = getDetectionObjects().findAll { it.getPathClass() == class1}
// Remove annotations meaning that criteria
removeObjects(toRemove, true)
setCellIntensityClassifications("Cytoplasm: DAB OD mean", 0.2)

Could you give me a suggestion?
Thank you!

Pete

unread,
Aug 16, 2018, 2:31:52 AM8/16/18
to QuPath users
The intensity features aren't being calculated because the cells aren't selected when that part is run.

I've updated your script to select the cells before and deselect them after calculating the features.

runPlugin('qupath.imagej.detect.nuclei.WatershedCellDetection', '{"detectionImageBrightfield": "Optical density sum",  "requestedPixelSizeMicrons": 0.5,  "backgroundRadiusMicrons": 8.0,  "medianRadiusMicrons": 0.0,  "sigmaMicrons": 1.5,  "minAreaMicrons": 10.0,  "maxAreaMicrons": 400.0,  "threshold": 0.1,  "maxBackground": 2.0,  "watershedPostProcess": true,  "excludeDAB": false,  "cellExpansionMicrons": 5.0,  "includeNuclei": true,  "smoothBoundaries": true,  "makeMeasurements": true}');

// Select the cells before calculating intensity features
selectDetections
()


runPlugin
('qupath.lib.algorithms.IntensityFeaturesPlugin', '{"pixelSizeMicrons": 0.5,  "region": "ROI",  "tileSizeMicrons": 25.0,  "colorOD": true,  "colorStain1": false,  "colorStain2": false,  "colorStain3": false,  "colorRed": false,  "colorGreen": false,  "colorBlue": false,  "colorHue": false,  "colorSaturation": false,  "colorBrightness": false,  "doMean": true,  "doStdDev": false,  "doMinMax": false,  "doMedian": false,  "doHaralick": false,  "haralickDistance": 1,  "haralickBins": 32}');

// Reset the selection again
resetSelection
()

enmunari

unread,
Aug 16, 2018, 4:40:09 AM8/16/18
to QuPath users
Great!! Thank you so much!!

enmunari

unread,
Aug 16, 2018, 12:36:54 PM8/16/18
to QuPath users
Guys, is there a way (a script) to create 4 circles with 1 mm diameter in an image? i have to go through a bunch of images and select 4 hotspot areas and is taking me a considerable amount of time drawing manually 4 circles every time....i am using the grid to get the right dimension....Thanks!!!

Pete

unread,
Aug 16, 2018, 12:42:39 PM8/16/18
to QuPath users
This script shows the idea: https://petebankhead.github.io/qupath/scripting/2018/03/09/script-create-fixed-size-region.html

But you'll need to change 'RectangleROI' to 'EllipseROI', and also adjust the 'sizeMicrons'.  Hopefully that can do what you need.

enmunari

unread,
Aug 16, 2018, 12:59:00 PM8/16/18
to QuPath users
Wonderful! It does the job quite nicely!!
Reply all
Reply to author
Forward
0 new messages