Need help with batch processing macros

Hi all,

What I want to do is to open up a folder and process all the images in that folder, but it kept saying IJ.openImage() returned null and I don’t know which part of the macros is wrong. I’m really new to ImageJ and the macros so any help would be appreciated. Thank you!

 * Macro template to process multiple images in a folder
 */

#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix

// See also Process_Folder.py for a version of this code
// in the Python scripting language.

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + list[i]))
			processFolder(input + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {
	run("8-bit");
	setAutoThreshold("Default dark");
//run("Threshold...");
	setThreshold(200, 255);
//setThreshold(200, 255);
	setOption("BlackBackground", true);
	run("Convert to Mask");
	run("Make Binary");
	run("Watershed");
	run("Analyze Particles...", "size=5-250 circularity=0.01-1.00 display exclude summarize");

	print("Processing: " + input + file);
	print("Saving to: " + output);
} 
1 Like

Hey @charz0,

before running the line run("8-bit"); you should call open(input + file);. Otherwise the macro doesn’t open any image :slightly_smiling_face:

Cheers,
Robert

Thank you Robert! While it’s opening files now, it’s not running the functions that I put in, do you know why is that?

What is it doing then? Does a table with numbers show up?

It opened one of the images from the folder and that was it, there wasn’t any table with results or anything like that.

Would you mind sharing the full code you run again?

 * Macro template to process multiple images in a folder
 */

#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix

// See also Process_Folder.py for a version of this code
// in the Python scripting language.

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + list[i]))
			processFolder(input + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {
	open(input + file);
	run("8-bit");
	setAutoThreshold("Default dark");
//run("Threshold...");
	setThreshold(200, 255);
//setThreshold(200, 255);
	setOption("BlackBackground", true);
	run("Convert to Mask");
	run("Make Binary");
	run("Watershed");
	run("Analyze Particles...", "size=5-250 circularity=0.01-1.00 display exclude summarize");

	print("Processing: " + input + file);
	print("Saving to: " + output);
} 

That looks good. Does it print out the “Processing: …” thingy?

And how does the image look like?

There’s no print out from the processing and there was no change to the image opened.

Could you please try if this one works?

#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix

// See also Process_Folder.py for a version of this code
// in the Python scripting language.

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + list[i]))
			processFolder(input + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {
	open(input + File.separator + file);
	run("8-bit");
	setAutoThreshold("Default dark");
//run("Threshold...");
	setThreshold(200, 255);
//setThreshold(200, 255);
	setOption("BlackBackground", true);
	run("Convert to Mask");
	run("Make Binary");
	run("Watershed");
	run("Analyze Particles...", "size=5-250 circularity=0.01-1.00 display exclude summarize");

	print("Processing: " + input + file);
	print("Saving to: " + output);
}

An output table showed up but the images didn’t get process for some reasons. I did get another macros that process opened images to work tho. Thank you so much for you help!

1 Like