Automated cell counting advice

imagej
cellcounting

#1

Hello!

I’m new to ImageJ/Fiji and am having some trouble developing a macro to count fluorescent stained cells from tissue samples. The quality of our images and staining intensity varies quite a bit and there are also areas of very concentrated and intense staining in certain images. For simplicity and consistency, I’ve been trying to use a single macro which subtracts background, converts to 8-bit greyscale, auto-thresholds, analyzes particles, and outputs an excel file for each image. This works fine for most of the ~250 images, but bright spots with intense fluorescence throw off the auto-thresholding in some images and prevent the program from making a cell count.

Below is the current version of my macro:

dir = getDirectory("Choose a Directory");

list = getFileList(dir);

count = 1;
for (i=0; i<list.length; i++) 
{
	if(endsWith(list[i], ".tif")) 
	{
		open(list[i]);
		CellCounter();
		close();
	}
}

function CellCounter() {
	xlsFileName = replace(getInfo("image.filename"),".tif",".xls");
	fullFilePath = getDirectory("image") + xlsFileName;
	run("Subtract Background...", "rolling=100");
	run("8-bit");
	run("Auto Threshold", "method=Li white");
	run("Convert to Mask");
	run("Analyze Particles...", "size=500-5000 circularity=0.20-1.00 display");
	currDialog = getInfo("window.type");
	if(currDialog != "ResultsTable") {
		close("Particle*");
	} else {
		saveAs("Results", fullFilePath);
		run("Close");
	}
}

Dialog.create("Status");
Dialog.addMessage("Counts complete.");
Dialog.show();

I’ve also attached one of the images that’s giving me trouble (Note: I had to compress it to upload). Any advice is greatly appreciated!

Original tif file (4116x3072): download


#2

Good day,

not sure where you got this line from

run("Auto Threshold", "method=Li white");

but you may try:

setAutoThreshold("Li");

In any case, if you use thresholding or binarization, then always include

setOption("BlackBackground", true);

or

setOption("BlackBackground", false);

at the beginning of your macro code.

Instead of

Dialog.create("Status");
Dialog.addMessage("Counts complete.");
Dialog.show();

use

showMessage("title", "message");

Displays “message” in a dialog box using “title” as the the dialog box title. Can display HTML formatted text (example).

count = 1;

doesn’t appear to be used.

Finally, it is a good idea to have the code of macro functions at the very end of the main code.

Please study the ImageJ docs dealing with macro coding:
https://imagej.nih.gov/ij/developer/macro/functions.html
https://imagej.nih.gov/ij/developer/macro/macros.html

Regards

Herbie