> For the complete documentation index, see [llms.txt](https://disc4all-qupath.gitbook.io/qupath-project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://disc4all-qupath.gitbook.io/qupath-project/qupath-h-dab-docs/qupath-h-dab-tutorial/cell-detection.md).

# Cell Detection

QuPath has already a built in function for cell detection that you can use.

1. Under *<mark style="color:green;">Automate > Show script editor</mark>* open your "EstimateStainVecotrs\_TissueComponentx" Script and run it on the training image.

{% hint style="info" %}
If your training image is pixelated, rerun the creating a training image step.&#x20;
{% endhint %}

1. For performing the cell detection *<mark style="color:green;">draw a square</mark>* in the newly created training image including all the regions.&#x20;
2. Under *<mark style="color:green;">Analyze</mark>* you can find *<mark style="color:green;">**cell detection**</mark>* or you can use the *<mark style="color:green;">shortcut ctrl+L</mark>* and then *<mark style="color:green;">type in cell detection</mark>* to open the window.&#x20;

{% hint style="warning" %}
Run the **cell detection PlugIn** not the positive cell detection. &#x20;
{% endhint %}

{% hint style="info" %}
If you do not see any detection the overlay opacity might be too low or the viewer is off. &#x20;

To change that click on the icons in the list (top middle) <img src="https://2829430504-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSleK316zl0BYwa7DfK2J%2Fuploads%2FWEAyzsUUrncbHMfU7COb%2Fimage.png?alt=media&amp;token=f6ded607-26ee-412c-b3d1-fdbe409bf6b5" alt="" data-size="line">

<img src="https://2829430504-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSleK316zl0BYwa7DfK2J%2Fuploads%2F8s6rnd6Yy1ZMTsZRMcub%2Fimage.png?alt=media&amp;token=df62849c-af5a-4514-872b-a70c7371b3da" alt="" data-size="line"> Toggle showing all detections in the viewer

<img src="https://2829430504-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSleK316zl0BYwa7DfK2J%2Fuploads%2FeKBSOHZ4JquhXVDtwIwZ%2Fimage.png?alt=media&amp;token=fed061d9-d047-43f2-834c-1012671143d7" alt="" data-size="line"> Toggle showing detection ROIs as filled shapes in the viewer

<img src="https://2829430504-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSleK316zl0BYwa7DfK2J%2Fuploads%2F59vWISrPCHllgFGx6URu%2Fimage.png?alt=media&amp;token=3a8a69f0-89f5-4e88-b56f-8ed844069f60" alt="" data-size="line"> adjusting the overlay opacity
{% endhint %}

<figure><img src="https://2829430504-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSleK316zl0BYwa7DfK2J%2Fuploads%2FTfpkyL5UAncjm3HB2n14%2Fimage.png?alt=media&amp;token=1009019b-2753-4493-9761-cedab568ef8b" alt=""><figcaption><p>Cell detection PlugIn without adjaustments</p></figcaption></figure>

<figure><img src="https://2829430504-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSleK316zl0BYwa7DfK2J%2Fuploads%2FCqT20JPgiGfaozpeWqo2%2Fimage.png?alt=media&amp;token=ba6b01a5-b942-403b-a4d9-56a50276bfdf" alt=""><figcaption><p>Cell detection after optimisation</p></figcaption></figure>

{% hint style="info" %}
It might take a while to find the correct settings for your slides but as a first step decide whether you should choose Haematoxylin OD for the detection or Optical Density Sum.&#x20;

Choose Haematoxylin OD if your nuclei are blue and the DAB staining is not masking the blue. Choose ODS if you have blue as well as brown nuclei.&#x20;

Then adjust the different settings you can find some examples of parameters set in the table below.&#x20;
{% endhint %}

<figure><img src="https://2829430504-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSleK316zl0BYwa7DfK2J%2Fuploads%2FkBTpmbdQEYe35qJIPXFD%2Fimage.png?alt=media&amp;token=6d9575dc-6472-4603-b647-aa31fe062269" alt=""><figcaption><p>Dont change all the parameters at the same time. In the image above only the threshold and the max. background intensity were changed. </p></figcaption></figure>

{% hint style="warning" %}
Those parameters are suggestions we used on our stainings, you must adjust them for your own project, but they might help as a starting point.&#x20;
{% endhint %}

<table><thead><tr><th width="131">See below</th><th width="158">Cartilage</th><th width="136">Disc Tissue </th><th>Disc Tissue</th><th data-hidden>Bone</th></tr></thead><tbody><tr><td>1</td><td>ODS</td><td>ODS</td><td>HOD</td><td></td></tr><tr><td>2</td><td>1</td><td>1</td><td>1</td><td></td></tr><tr><td>3</td><td>5</td><td>8</td><td>8</td><td></td></tr><tr><td>4</td><td>0.5</td><td>0</td><td>0</td><td></td></tr><tr><td>5</td><td>0.8</td><td>1.5</td><td>1.5</td><td></td></tr><tr><td>6</td><td>5-100</td><td>20-100</td><td>10-400</td><td></td></tr><tr><td>7</td><td>0.12</td><td>0.2</td><td>0.1</td><td></td></tr><tr><td>8</td><td>1.5</td><td>0.25</td><td>2</td><td></td></tr></tbody></table>

<figure><img src="https://2829430504-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSleK316zl0BYwa7DfK2J%2Fuploads%2F0LH96E37IqDwQzV5kKcd%2Fimage.png?alt=media&amp;token=8bedbfd5-bfb8-4b98-b864-0f1aec47ea40" alt=""><figcaption><p>Examples for parameters for the cell detection</p></figcaption></figure>

Adjustable parameters are:&#x20;

<table data-view="cards"><thead><tr><th></th><th></th><th></th><th data-type="files"></th></tr></thead><tbody><tr><td><p><mark style="color:green;"><strong>1 Detection images</strong></mark></p><p></p><p>Haematoxylin OD, </p><p>if the nuclei are blue, if they are brown the </p><p>DAB might mask haematoxylin and they do not get recognised.</p><p></p><p>Optical density sum (ODS), </p><p>if you have a lot of brown and blue nuclei</p></td><td></td><td></td><td></td></tr><tr><td><p><mark style="color:green;"><strong>2 Requested pixel size</strong></mark></p><p></p><p>Check pixel size in Image und Image</p><p></p><p>The bigger the chosen value in pixel size the faster, find the max. size that is still accurate</p><p></p></td><td></td><td></td><td></td></tr><tr><td><p><mark style="color:green;"><strong>3 Background radius</strong></mark></p><p></p><p>QuPath will try to subtract background value from each pixel</p><p></p><p>Correlates with Threshold</p><p></p><p>Should be greater than the largest nuclei or set to 0 if it is turned off, then threshold needs to be increased</p></td><td></td><td></td><td></td></tr><tr><td><p><mark style="color:green;"><strong>4 Median filter radius</strong></mark></p><p></p><p>If Nuclei are segmented increase</p><p></p><p>Way to smooth image</p></td><td></td><td></td><td></td></tr><tr><td><p><mark style="color:green;"><strong>5 Sigma</strong></mark></p><p></p><p>Segments nuclei but could also merge them together</p><p></p><p>Way to smooth image</p></td><td></td><td></td><td></td></tr><tr><td><p><mark style="color:green;"><strong>6 Area</strong></mark></p><p></p><p>Minimum area/size of a nuclei</p><p></p><p>Maximum area/size of a nuclei</p></td><td></td><td></td><td></td></tr><tr><td><p><mark style="color:green;"><strong>7 Threshold</strong></mark></p><p></p><p>Can help to remove detection of false nuclei within tissue</p></td><td></td><td>If a high amount of cells is not detected try lowering the threshold. </td><td></td></tr><tr><td><p><mark style="color:green;"><strong>8 Max background intensity</strong></mark></p><p></p><p>Can remove tissue fold as background is darker as usual</p><p></p><p>The lower the value the more the folds will be ignored</p><p></p><p>Default doesn’t really show an effect</p></td><td></td><td></td><td></td></tr></tbody></table>

Even when the cell detection is optimised **non-cell regions will get recognised as cells this is due to the tissue properties.** In the next step, we will train the system to recognise those as non-cell regions.&#x20;

{% hint style="warning" %} <mark style="color:blue;">Under</mark> <mark style="color:blue;"></mark><mark style="color:blue;">**Workflow > create**</mark> <mark style="color:blue;"></mark><mark style="color:blue;">script save a new script "</mark><mark style="color:blue;">**CellDetection\_componentx**</mark><mark style="color:blue;">". This script will be needed to run the cell detection on other training slides and to substitute the cell detection in the final script.</mark>&#x20;

<mark style="color:red;">**Delete all the previous lines except the last one starting with runPlugin('quPath...)**</mark>

<mark style="color:blue;">Then click on File > save as > File name: Cell Detection, Save as type:  Groovy file</mark>

`runPlugin('qupath.imagej.detect.cells.WatershedCellDetection', '{"detectionImageBrightfield":"Optical density sum","requestedPixelSizeMicrons":1.0,"backgroundRadiusMicrons":8.0,"backgroundByReconstruction":true,"medianRadiusMicrons":0.0,"sigmaMicrons":1.5,"minAreaMicrons":20.0,"maxAreaMicrons":400.0,"threshold":0.2,"maxBackground":0.25,"watershedPostProcess":true,"excludeDAB":false,"cellExpansionMicrons":5.0,"includeNuclei":true,"smoothBoundaries":true,"makeMeasurements":true}')`

{% endhint %}

<figure><img src="https://2829430504-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSleK316zl0BYwa7DfK2J%2Fuploads%2FFnxDIk3glNwmofKfxz8y%2Fimage.png?alt=media&amp;token=30bd067a-387c-41e7-b99f-2aee858867ef" alt=""><figcaption><p>In the subfolder scripts of your project, the newly created scripts for the cell detection can be found</p></figcaption></figure>
