Count Nuclei (or other spots) in an image

Counting Nuclei (or other features) in an image is a common analysis problem.

A very common biological sample for microscopy is DAPI stained DNA in cell nuclei. The staining delineates the nuclei pretty well, because in a metaphase cell there is DNA all over the nucleus. however, the staining is not homogenous, as there are areas of more or less condensation of the chromosomes. This makes the nuclei appear granular. Worse still, in many tissues that are interesting for developmental biology, the cells are tightly packed, and are composed mostly of nucleus with very little cytoplasm separating them. The nuclei often seem to touch each other, which in reality, of course, they can not. There is a membrane or two at least between them, but an optical microscope cannot resolve that, or cannot discern that they are separated in Z. The PSF is much bigger than the width of a membrane! Just to make matters even worse, images are oftern quite noisy, because they are made with a fast scan on a confocal laser scanning microscope, or with a low exposure time/high gain setting on a CCD camera. So the image has an inherently low signal/noise ratio. That gives objects fuzzy edges and adds uncertainty to the intensity values of each pixel, making it harder to segment properly.Here is one way to accomplish this task.I use FIJI (Fiji is just ImageJ) a packaging of imageJ that includes all the plugins you need for most image analysis. You can find it at
1 – Open your image.
File -> Open…

Here’s one I will use:

fluorescent image of mammalian cell nuclei stained with DAPI

It’s an image I took of DAPI stained nuclei on the SP5 confocal microscope. Notice it’s not the best image you’ve ever seen of DAPI stained nuclei. It was purposely taken with low laser power and high-ish gain on the PMT to produce an image with higher “noise” than would typically be taken.

2 – Filter to remove the noise.
Process -> Filters -> Gaussian Blur…
Pay with the Sigma(Radius), maybe start with 2 or 3. The idea is to blur out the background “salt and pepper” noise which is actually Poisson distributed, statistical “photon shot noise”, and also to smooth out the inhomogeneity of the nuclear staining. Mouse cells will require higher sigma values as the DNA within the nucleus is much less homogeneous than other mammalian cell types.

Here’s our image after Gaussian blur:

processed image after applying gaussian blur to nuclei image

3 – Subtract the background. (Optional, may or may not be required)
Process -> Subtract Background…
Make sure the box marked “Light Background” is unticked. You may have to play around with the “rolling ball” size depending on the size of the features in your image. Essentially you are trying to subtract the background and make it near zero everywhere. The rolling ball method takes into account uneven background due to illumination, detector problems, etc.

Once again, here’s the result of subtracting the background from the previous image:

processed image after applying gaussian blur to nuclei image and subtracting background

4 – Set the measurements we will use later for filtering the nuclei.
Analyse -> Set Measurements …
Tick the boxes marked “Area” and “Shape Descriptors”. Then click on the “OK” button
In this step, you’re telling FIJI what measurements to make when, later on in the workflow you choose to “filter” which features to include/exclude in the counting.

FIJI set measurements window

5 – Threshold your image.
Image -> Adjust -> Threshold…
In this case, make sure that the box labelled “Dark Background” is ticked. Adjust the sliders so that your features are red coloured, but the rest of the image is not. This is often not possible to achieve perfectly. Don’t worry, we can change this if things go wrong, or adjust other parameters later in the analysis to take this into account. Click on the “Apply” button. This will replace your grayscale image with an “8-bit binary image”. All the “red” pixels will be converted to a value of “255” while all the non red pixels will be given a value of “0”.

Processed image - Binary image after processing as in steps 1, 2, 3

6 – Fill in any holes in the nuclei.
Proess -> Binary -> Fill Holes
The next step will not work properly unless your nuclei are without any holes. A few of mine had areas which were below the threshold and so were not counted as part of the “object”, i.e. the nucleus. We can easily get rid of these holes by performing a binary operation to fill the holes in.

Binary Image - morphological processed to fill holes

6 – Separate “Touching” nuclei.
Proess -> Binary -> Watershed
Here is where things get interesting. Your image should contain segmented nuclei. There may be small ones and large ones (some of the small ones may not be nuclei). One common challenge is that the nuclei are touching. How can we separate them? Use the “watershed” algorithm.This method finds the centre of each object (using a morphological erode operation), then calculates a distance map from the object centre points to the edges of the objects, then fills that “topological map” with imaginary water. Where 2 “Watersheds” meet, it builds a dam to separate them! One could do all these steps manually, but the watershed function automates that for you, which is nice. Your nuclei should now be separated by lines that are one pixel wide.

Binary Image after applying the watershed algorithm to separate touching objects

7- Now perform the analysis. Here’s where we actually count the nuclei,
Analyze -> Analyze Particles…
In this dialogue box we start to include and exclude our nuclei or features based on their attributes. For example if we set the “Size” to be larger than 1 square pixel (or microns if you’re using a calibrated image), then no particles that are less than 1 square pixel will be counted in the particle analysis. As we’re interested in nuclei, set this parameter to discard the junk particles that are small. Similarly, as nuclei are quite round, we can filter the results to only include those particles which are nearly round (calculated as 4∏*area/perimeter^2 – a perfect circle = 1). To get an idea of what size and how circular the nuclei in your image are, perform the analysis a couple of times and look at the results table. It will show you the measurements you set in step 4. Also – the first column in the results table is the metric that you want, i.e. the number of each particle and the total number of particles in your analysis.

Final image now with nuclei highlighted and counted.

It may not work the first time, but as you change the parameters of each step in this image processing workflow you can tweak the parameters in each step to optimise the results for your particular problem.