# Cell Detection

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="/files/AGBk4cTVRNxOEM35oJIg" alt="" data-size="line">

<img src="/files/Y5pijFERjSwm4sKsf4Ir" alt="" data-size="line"> Toggle showing all detections in the viewer

<img src="/files/vhpHvffLkQfgg6BKJEoW" alt="" data-size="line"> Toggle showing detection ROIs as filled shapes in the viewer

<img src="/files/36AUU1NNcYmXZm9LZFFw" alt="" data-size="line"> adjusting the overlay opacity
{% endhint %}

<figure><img src="/files/XZiaNrlAgZxwwc786dlR" alt=""><figcaption><p>Cell detection PlugIn without adjaustments</p></figcaption></figure>

<figure><img src="/files/kwnZfqzJYKKeRfQBlIvO" 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="/files/AScUVLK3DAJjcfY4NaYz" 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="/files/qmYZspAiJODoLYKcA2o5" 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="/files/KUqMBuKZ5HyZuGFuMdyU" alt=""><figcaption><p>In the subfolder scripts of your project, the newly created scripts for the cell detection can be found</p></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://disc4all-qupath.gitbook.io/qupath-project/qupath-h-dab-docs/qupath-h-dab-tutorial/cell-detection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
