Imagej batch processing with macro, every second image fails

Sample image and/or code

atitle = getTitle();
title = replace(atitle, “.czi”, “”);
dir = “x”

selectWindow(title + “.czi”);
run(“Split Channels”);
selectWindow(“C2-” + title + “.czi”);
run(“Convert to Mask”);
run(“Analyze Particles…”, “size=0.50-2.00 clear add”);
selectWindow(“C1-” + title + “.czi”);
saveAs(“Results”, dir + title + “.csv”);


Measuring fluorescent intensity.

Analysis goals

We have one image with two channels, fluorescent and phase contrast.
The code splits the channels, uses the phase contrast channel (“C1-”) to create a mask to select the bacteria, then the roiManager is used on the fluorescent channel (“C2-”) to measure the intensity, and save these results in a .csv named accordingly.


Every second .csv file produced by the macro is incorrect and displays values ranging from 0-255, whereas the expected values (if run individually) are ~0-8000. The values are outputted as binary.
I’m new to writing macros, and I am completely lost as to why it only works on every second image.
Any help would be greatly appreciated.

Could it be that at the end of your processing of individual images within your folders, you still have some thresholded images open that are accidently measured instead of the image that you intend to measure?
I would try to explicitly close the opened images at the end of your code to avoid any confusion.
Just a thought.
Hope it helps,