Printing mean results from multiple analysis

Hello ImageJ experts!
I really enjoy your helpful comments in this forum, and I hope you can enlighten me once more. I have a macro that works well on my images, it meassures the mean intensity of the nuclear staining in the red channel, and prints this to the summary. I would like to be able to run it in batch mode, so that I can analyse 100+ images, and then just have the summarized output I am interested in, in the log (Title, nuclei count and most importantly: mean intensity of nuclei in red channel). However, Even though it works when I open one image at a time, when I try to make it run a batch, it keeps adding the mean intensity from one image to the next, resulting in a pretty linear increase down the list of images…
What am I doing wrong here?

Original macro, works pr image:
originalName = getTitle();
run(“Split Channels”);

selectImage(originalName + " (blue)");

run(“Gaussian Blur…”, “sigma=1”);
setAutoThreshold(“Huang dark”);
setOption(“BlackBackground”, false);
run(“Convert to Mask”);
run(“Fill Holes”);
run(“Watershed”);
run(“Set Measurements…”, “mean decimal = 3”);
run(“Analyze Particles…”, “size=5-Infinity pixel show=[Overlay Masks] exclude add in_situ”);
selectImage(originalName + " (red)");
run(“From ROI Manager”);
roiManager(“Measure”);

for (k = 0; k < nResults; k + +)
{
MI + = getResult(‘Mean’, k);
}
{
AMI = (MI/k);//calculates mean intensity

print(getTitle());
print(“Count:” +k);
print (“Avg. Intensity:” + AMI);
}

roiManager(“deselect”);
roiManager(“Delete”);
run(“Clear Results”);
while(nImages > 0) close();

Then I tried the following to make it run on a whole folder og images:

path1=getDirectory(“P:\Microscopy\E5\A side 10x\overlay”);
list=getFileList(path1);

for(i=0;i<list.length;i++){
open(path1+list[i]);

originalName = getTitle();
run(“Split Channels”);

//Identifies the pIRF (red chanel) signal:
selectImage(originalName + " (blue)");

run(“Gaussian Blur…”, “sigma=1”);
setAutoThreshold(“Huang dark”);
//does thresholding, sets to binary 0 or 255;
setOption(“BlackBackground”, false);
run(“Convert to Mask”);
run(“Fill Holes”);
run(“Watershed”);
//Segmentation of 1st pic, DAPI
run(“Set Measurements…”, “mean decimal = 3”);
run(“Analyze Particles…”, “size=5-Infinity pixel show=[Overlay Masks] exclude add in_situ”);
selectImage(originalName + " (red)");
run(“From ROI Manager”);
Table.deleteRows(0, 10000);
//deletes meassurements from Dapi ch which are unnecessary
roiManager(“Measure”);

for (k = 0; k < nResults; k + +)
{
MI + = getResult(‘Mean’, k);
}
{
AMI = (MI/k);//calculates mean intensity

print(getTitle());
print(“Count:” +k);
print (“Avg. Intensity:” + AMI);
}

roiManager(“deselect”);
roiManager(“Delete”);
run(“Clear Results”);

// closes all images before the next iteration
while(nImages > 0) close();
}

Hi @Helenaen ,

I think the key point you are missing is to reset the variable (MI) you are using to keep track of the intensity in each iteration. To be more specific, you should include something like MI=0 after opening each image.

Other than that, it would be super helpful to post code as preformatted text in the text editor with proper indendation as it makes code much easier to understand for others :slight_smile: And secondly - it seems like you are measuring fluorescence intensity of some staining inside DAPI-positive nuclei? If so, you may want to check out the StarDist 2D plugin for nucleus segmentation.

Hope this helps!
Best, Johannes

1 Like

Hi Johannes,
thank you SO much, I cannot believe it was that easy… I have really tried to search for a solution here. I believe I missed this because I am still new to coding - this will be a helpful lesson learned in future macros :smiley:

I apologize for the messy setup of my post, of course I should have put the macro correctly in the text editor.
Thanks again for your time, the macro works now!

1 Like