Error when applying image calculator to a path

Hi everybody,
I am trying to build up a macro to process .C01 files in batch. I have used part of @BiochemNotCoding macro to start with. However, when I tried to modified the macro to aply it to my needs, I encountered some problems. I would like to run a macro to generate separated images from nuclei and cytoplasms. Aditionally, I want to save the ROI for several masks, nuclei (DAPI channel 0), cytoplasms and channel 1 (488nm fluorescence).
At some point I have to run image calculator action and the macro reports an error. Tells me that it cannot find the images. Within image calculator, I included the path to the two pictures needed for calculation, but oviously I did it in the wrong way. In adition, I am afraid that from there on, all the code would be wrong because I have used the same logic when pointing at the pictures used in the following steps.

I have tried inserting “open” and “selectimage” commands and even, defining variables, path1 and path2 corresponding to the paths were the images to be used for calculation would be created, but nothing worked.

Of course, these are my first steps in coding so, I am quite lost.

Does anybody some clue for my? Thanks in advance.

Please find below the macro I have tried to generate.
Best,

Isbaal.

// This part belongs to @BiochemNotCoding 

	inDir = getDirectory("C:/Users/laure/OneDrive/Documents/MSc Molecular Biology & Biotechnology/Masters Project - Dombrowski Lab/Project Data/LD006 CX5/CellHealthProfiling.V4_03-15-20_01;35;58/CEM-133432_200315130001/Test files");
	outDir = getDirectory("C:/Users/laure/OneDrive/Documents/MSc Molecular Biology & Biotechnology/Masters Project - Dombrowski Lab/Project Data/LD006 CX5/CellHealthProfiling.V4_03-15-20_01;35;58/CEM-133432_200315130001/Results"); 

	list = getFileList(inDir);

	C01list = newArray(0);
	for (i = 0; i < list.length; i++) {
	if (endsWith(list[i], ".C01")) {
		C01list = Array.concat(C01list, list[i]);
	}
	} 


	d0_List = newArray(0);
	d1_List = newArray(0);


	for (j = 0; j < C01list.length; j++){
	if (matches(C01list[j], ".*d0.*")) {
		d0_List = Array.concat(d0_List, C01list[j]);
	}

	if (matches(C01list[j], ".*d1.*")) {
		d1_List = Array.concat(d1_List, C01list[j]);
	}


	}


	Array.sort(d0_List);
	Array.sort(d1_List);


	setBatchMode(true);

	for(img = 0; img < d0_List.length; img++){

	run("Bio-Formats Importer", "open=[" + inDir + File.separator + d0_List[img] + "] color_mode=Default open_files rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
	DAPI = getTitle();
	run("Bio-Formats Importer", "open=[" + inDir + File.separator + d1_List[img] + "] color_mode=Default open_files rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
	Nomad = getTitle();


	dIndex = indexOf(DAPI, "d" );
	imgName = substring(DAPI, 0, dIndex);

	print("The image being processed is " + imgName);

//My code starts here.

// Nuclei Masks

	selectWindow(DAPI);
	setOption("ScaleConversions", true);
	run("8-bit");
	run("Enhance Contrast...", "saturated=0.3");
	setAutoThreshold("Huang dark");
	//setThreshold(3, 255);
	setOption("BlackBackground", true);
	run("Convert to Mask");
	run("Fill Holes");
	run("Watershed");
	run("Analyze Particles...", "size=100-Infinity pixel show=Masks exclude");
	run("Invert LUT");
	saveAs("Tiff", outDir + File.separator + imgName + "_Nucleos optim");


// Nuclei segmentation

	run("Find Maxima...", "prominence=75 exclude output=[Segmented Particles]");
	saveAs("Tiff", outDir + File.separator + imgName + "_Nucleos segmentados");
	

// 488nm Masks

	selectWindow(Nomad);
	setOption("ScaleConversions", true);
	run("8-bit");
	run("Enhance Contrast...", "saturated=0.3");
	setAutoThreshold("Default dark");
	run("Smooth");
	//setThreshold(4, 255);
	run("Convert to Mask");
	run("Watershed");
	run("Fill Holes");
	saveAs("Tiff", outDir + File.separator + imgName + "_Nomad");



// 488nm segmentation using nuclei masks

//ERROR STARTS HERE

	imageCalculator("AND", outDir + File.separator + imgName + "_Nucleos segmentados.tif", outDir + File.separator + imgName + "_Nomad.tif");
	run("Analyze Particles...", "size=250-Infinity pixel show=Masks exclude clear");
	run("Invert LUT");
	selectWindow(outDir + File.separator + "Mask of" + imgName + "_Nucleos segmentados";
	rename(outDir + File.separator + imgName + "_Nomad Segmented");

// Citoplasms Masks

	imageCalculator("Subtract create", outDir + File.separator + imgName + "_Nomad Segmented", outDir + File.separator + imgName + "_Nucleos optim.tif");
	selectWindow(outDir + File.separator + "Result of" + imgName + "_Nomad Segmented");
	rename(outDir + File.separator + imgName + "_Citoplasma");
	saveAs("Tiff", outDir + File.separator + imgName + "_Citoplasma");

// Nuclei ROI

	selectWindow(outDir + File.separator + imgName + "_Nucleos optim.tif");
	run("Analyze Particles...", "size=100-Infinity pixel show=Masks exclude clear add");
	selectWindow(outDir + File.separator + "Mask of" + imgName + "_Nucleos optim.tif");
	close();
	roiManager("Deselect");
	roiManager("Save", outDir + File.separator + imgName + "_RoiSet nucleos.zip");
	

// Cytoplasm ROI 

	roiManager("reset");
	selectWindow(outDir + File.separator + imgName + "_Citoplasma.tif");
	run("Analyze Particles...", "size=0-Infinity pixel show=Masks exclude clear add");
	selectWindow(outDir + File.separator + "Mask of" + imgName + "_Citoplasma.tif");
	close();
	roiManager("Deselect");
	roiManager("Save", outDir + File.separator + imgName + "_RoiSet citoplasma.zip");

	
// 488nm ROIs

	roiManager("reset");
	selectWindow(outDir + File.separator + imgName + "_Nomad.tif");
	run("Analyze Particles...", "size=0-Infinity pixel show=Masks exclude clear add");
	selectWindow(outDir + File.separator + "Mask of" + imgName + "_Nomad.tif");
	close();
	roiManager("Deselect");
	roiManager("Save", outDir + File.separator + imgName + "_RoiSet Nomad.zip");
	
// cleaning

		run("Close All");

	}

	setBatchMode(false);

This may or may not be your issue, but is worth ruling out- sometimes I’ve run into issues with ImageJ scripts when there are spaces in the file names. It looks like you have this in several places- Nomad segementados , etc, and that. your “ERROR STARTS HERE” is in the first place where it needs to select or load a file with a space in the name. Can you use a dash or an underscore or just no space at all in all of your file names, and see if that helps?

Hi Beth,
Thanks for your suggestion. I have just tried with underscores but it has not worked either.
Good suggestion though.
Best,
Isbaal.

Hi Isbaal,
You may not have space in the file name but do you have any in the path? If you do, you need to modify your code and include [ ] around the path.

So

imageCalculator("AND", outDir + File.separator + imgName + "_Nucleos segmentados.tif", outDir + File.separator + imgName + "_Nomad.tif");

need to be changed to this:

imageCalculator("AND", [outDir + File.separator + imgName + "_Nucleos segmentados.tif"], [outDir + File.separator + imgName + "_Nomad.tif"]);

Hi
imageCalculator(operator, img1, img2)
takes image titles or image IDs of open images as arguments, not file paths.
Jérôme.

1 Like

Hi Matthieu,
Thanks. It did not work either. Keep going.
Best,
Isbaal.

Hi Jérôme,
Thanks for your help. I gues that that is the problem. Do you have any idea about how to recall the images from the output file to apply the imagecalulator in batch? I have tried to run the “open” comand (with the path as argument) previous to the image calculator operation, but it did not work either.
Best,
Isbaal.

Hi Isbaal,

instead or on top of saving your images, get their imageID and use these in the imageCalculator call.

...
saveAs("Tiff", outDir + File.separator + imgName + "_Nucleos segmentados.tif");
img1=getImageID;
...
saveAs("Tiff", outDir + File.separator + imgName + "_Nomad.tif");
img2=getImageID;
imageCalculator("AND", img1, img2);

Hi Jerome,
Thanks for your help. I get it. Actually I am adding a lot of other actions to the macro.
Best,
Isbaal.

1 Like