Help with my loop

Hello fellow Fiji users,

Yesterday I started writing a macro for counting cells from stainings. Now it works and does everything I want it to, but after completing the task its last action is to search for a ROI Manager window, that has already been closed and therefore displaying an error message. I can’t seem to figure my syntax mistake in the loop out. I also installed the Results to excel Plugin.
Here the loop code:

//define the process
function action(input, output, filename) {
	//open image
	open(input + filename);
	//open ROI Manager and the saved ROIs
	run("ROI Manager...");
	roiManager("Open", input + "RoiSet_" + filename + ".zip");
	//select the correct stained channel
	//threshold, watershed and analyze particles for finding the cell somas as Maxima
	setAutoThreshold("Default dark");
	setThreshold(100, 255);
	run("Convert to Mask", "method=Default background=Dark only black");
	run("Watershed", "slice");
	run("Analyze Particles...", "size=90-Infinity circularity=0.25-1.00 show=Masks exclude clear slice");
	saveAs("Tiff", output + "Mask_" + filename);
	//find Maxima=Cell somas in the different layers
	roiManager("Select", 1);
	run("Find Maxima...", "prominence=10 exclude output=Count");
	roiManager("Select", 2);
	run("Find Maxima...", "prominence=10 exclude output=Count");
	roiManager("Select", 3);
	run("Find Maxima...", "prominence=10 exclude output=Count");
	run("Read and Write Excel", "no_count_column stack_results file=[C:/Users/Admin/Desktop/Interneuron_results.xlsx] sheet=result");
	//close all windows for next Image
	run("Close All");

//define parameters
input = "D:/ImageJ_batch_process/input/"
output= "D:/ImageJ_batch_process/output/"

//define batching
list = getFileList(input);
for (i = 0; i < list.length; i++)
        action(input, output, list[i]);

I hope someone will be able to explain to me where I messed up. Thank You guys very much for Your time and effort.

Have a nice day,

I don’t see the error like this. It seems to me that the roi-manager is not closed in the macro.
You can try to use roiManager("reset") instead of the deselect and delete. This will work whether the roiManager is open or not and whether it contains Rois or not.
Best regards,

1 Like

Thank You very much for Your fast reply. I implemented Your suggestion, however the error still occurs.
Here it is. The interesing thing is, that this message is displayed after processing all the data successfully.

Best regards

Ok, I think I understand what happens now.

The getFileList command will give you a list of all files in the input folder, i.e. the images and the roi-sets. The open command will then open the roi-set and the selectWindow command will try to select a window with its name. But the roi-set has been opened in the roi-manager and no window with the name exists, which produces the error.

You should call the action function only with the images from the input folder and not with the roi-sets. You could for example check if the file is an image based on its file extension (something like .tif but not .zip.

Best regards,

1 Like

Thank You very much. Just solved that problem by dividing the list length by 2, because to each Tif image there is a ROI save file. Thank You again, I wouldn’t have realised that in ages! Enjoy your evening, Volker
Best regards,