Imagej Macro processing using bioformat

I am trying to create a macro that will run a loop through a file containing images. To open the images I am using Bio-Format importer, and the code is working, however, it prompts me to choose the image in the file every time. Is there a way to make it automatically run through the file?
this is my code below… any help would be appreciated…

setBatchMode(true); 

inputDirectory = getDirectory("Choose a Directory of Image")

fileList = getFileList(inputDirectory);

for (i = 0; i < fileList.length; i++)
{
    processImage(fileList[i]);
    // Show a dialog to allow user to save the results file
outputDirectory = "S:/Research/MLW/OUTPUT/";
outputFile = outputDirectory+fileList[i]+".csv";
// Save the results data 
saveAs("results",outputFile);
}

setBatchMode(false); 
updateResults();  

function processImage(imageFile)
{

    prevNumResults = nResults;  

	run("Bio-Formats Macro Extensions"); 

	run("Bio-Formats Importer", "open= + inputDirectory + fileList[i] color_mode=Default rois_import=[ROI manager] split_channels view=Hyperstack stack_order=XYCZT");	
	
       filename = getTitle();

	close();
	close();
	run("Auto Threshold", "method=Yen white");
	run("Skeletonize (2D/3D)");
	run("Analyze Skeleton (2D/3D)", "prune=none");
	run("Summarize");
  
    close("*");  // Closes all images

@mwilson

I would recommend restructuring your code based on the example macro code Process Folder (IJ1 Macro):

/*
 * 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

processFolder(input);

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

function processFile(input, output, file) {
	// Do the processing here by adding your own code.
	// Leave the print statements until things work, then remove them.
	print("Processing: " + input + File.separator + file);
	print("Saving to: " + output);
}

There is a nice example of this code - modified - for the Scripting with Fiji workshop. Here is the slide with the code. That example has all images being opened via Bio-Formats.

From a quick glance at your code though… I think the issue might be in your run() call for Bio-Formats… make sure you are concatenating your Strings with your variables (ie. inputDirectory) properly… such as:

run("Bio-Formats Importer", "open=" + inputDirectory + fileList[i] +" color_mode=Default rois_import=[ROI manager] split_channels view=Hyperstack stack_order=XYCZT");	

You might need a File.separator in there too between the two variables listed there…

Hi @mwilson,
I believe what you mean is that you still get the Bio-Formats window in which you have to select one or more images to open, right?
Perhaps your issue is already solved with the help of @etarena, but if not:
You can open a single image (series) in a file by letting Bio-Formats know which series you want to open: add an argument series_x (with x the number of the image you want to open) in the line
run("Bio-Formats Importer", "open= + inputDirectory + fileList[i] color_mode=Default rois_import=[ROI manager] split_channels view=Hyperstack stack_order=XYCZT");

If you want to open all images in each file one by one you could e.g. make another loop over all series in a file inside the processImage function (which then becomes more like processFile). Get the number of series using Ext.getSeriesCount(seriesCount).

Good luck!
Bram

2 Likes

Hi,
as @etarena already pointed out, the problem might be the way you call Bio-Formats.
Maybe try opening your images with the Bio-Formats windowless importer:

run("Bio-Formats Windowless Importer", "open=" + image);

Cheers,
Mario

1 Like

Hi @marioK,

That may solve the problem, but only when the file contains a single series, since the windowless importer opens the first one.
Plus, after running the macro, you can’t drag&drop multi-series files any more, because the Bio-Formats importer continues to be the windowless, until you actually run the plugin from the menu. (At least, it was like that a few years ago.)

Best regards,
Bram

2 Likes

Hi Bram,

thank you for the details. Didn’t know about that.
Since @mwilson is running in batch mode, I guess that the loss of drag and drop should not be an issue.

Cheers,
Mario

Hi @etarena, thank you for your suggestion! I restructured my code to match that template but now I am getting an error for line 1. the processFolder(input); line is giving me an undefined variable error. I thought maybe I should change input to the path to my file but it is also not recognizing it then. Also I am confused on what the #@ File notes are before the code… are those things I should be defining?

@mwilson

Those are Script Parameters. I think the reason you are getting the error is that you deleted them or so? Those are what the user defines upon running the script…