Saving csv in batch mode

Hi,

I’m trying to detect and count jellyfish in a very large number of images (extracted as jpeg from video). Therefore, I would like to use the batch processing function. The process I follow for one image is to invert it, I apply colour threshold, do a selection, transfer the selection to the ROI manager, split the selection and measure, I then save Results as a csv file. I’ve managed to get the macro working for one image but I haven’t managed to apply the batch function through a macro. I’ve followed similar posts but I guess I’m still very unfamiliar with ImageJ and its language, I would appreciate any help greatly.

Say my input file is here: “Users/input” and my output file where I would like all the csv to go to is here “Users/output”. How do I set it?What do I need to add at the beginning and end of the code? I’m confused about how to set the directories, the files and the name files. In other posts I’ve seen code such as:

setBatchMode(true); 
list = getFileList(input); 
for (i = 0; i < list.length; i++) 
        action(input, output, list[i]); 
setBatchMode(false); 

BUT, do I need to replace “input”, “output” with the actual path of my files or do I need to define input and output previously (i.e. input=/Users/input)

Below is the code I’m running. What do I need to add at the beginning and end of the code?:

This is the code that I run:

run("Invert"); 
run("Color Threshold..."); 
// Color Thresholder 2.0.0-rc-61/1.51n 
// Autogenerated macro, single images only! 
min=newArray(3); 
max=newArray(3); 
filter=newArray(3); 
a=getTitle(); 
run("HSB Stack"); 
run("Convert Stack to Images"); 
selectWindow("Hue"); 
rename("0"); 
selectWindow("Saturation"); 
rename("1"); 
selectWindow("Brightness"); 
rename("2"); 
min[0]=140; 
max[0]=243; 
filter[0]="pass"; 
min[1]=0; 
max[1]=51; 
filter[1]="pass"; 
min[2]=43; 
max[2]=186; 
filter[2]="pass"; 
for (i=0;i<3;i++){ 
  selectWindow(""+i); 
  setThreshold(min[i], max[i]); 
  run("Convert to Mask"); 
  if (filter[i]=="stop")  run("Invert"); 
} 
imageCalculator("AND create", "0","1"); 
imageCalculator("AND create", "Result of 0","2"); 
for (i=0;i<3;i++){ 
  selectWindow(""+i); 
  close(); 
} 
selectWindow("Result of 0"); 
close(); 
selectWindow("Result of Result of 0"); 
rename(a); 
// Colour Thresholding------------- 

run("Create Selection"); 
roiManager("Add"); 
roiManager("Split"); 
roiManager("Measure"); 

I’d be extremely grateful for any help or tips given,

Ana

Something like this to save the results as *.csv:

Below snippet from: https://imagej.net/How_to_apply_a_common_operation_to_a_complete_directory

input = "/Users/input/";
output = "/Users/input/results/";

setBatchMode(true); 
list = getFileList(input);
for (i = 0; i < list.length; i++)
        action(input, output, list[i]);
setBatchMode(false);

Then something like:

selectWindow("Results");
saveAs("Results",  output + yourFileName + ".csv"); 

Here is another snippet for a recursive folder operation (recognize all subfolders):

https://imagej.nih.gov/ij/macros/BatchProcessFolders.txt

Here a more complete example (change for your needs):

input = "/Users/input/";
output = "Users/input/"; // I would create an extra folder for the output!

//setBatchMode(true);
list = getFileList(input);
for (i = 0; i < list.length; i++) {
    action(input, output, list[i]);
}
//setBatchMode(false);

function action(input, output, image) {
    setBatchMode(true);
    nameOfFile = File.getName(image);
    /*Remove the file extension!*/
    dotIndex = indexOf(nameOfFile, ".");
    nameOfFile = substring(nameOfFile, 0, dotIndex);
    print(nameOfFile);
    open(input + image);
    run("Invert");
    run("Color Threshold...");
    // Color Thresholder 2.0.0-rc-61/1.51n 
    // Autogenerated macro, single images only! 
    min = newArray(3);
    max = newArray(3);
    filter = newArray(3);
    a = getTitle();
    run("HSB Stack");
    run("Convert Stack to Images");
    selectWindow("Hue");
    rename("0");
    selectWindow("Saturation");
    rename("1");
    selectWindow("Brightness");
    rename("2");
    min[0] = 140;
    max[0] = 243;
    filter[0] = "pass";
    min[1] = 0;
    max[1] = 51;
    filter[1] = "pass";
    min[2] = 43;
    max[2] = 186;
    filter[2] = "pass";
    for (i = 0; i < 3; i++) {
        selectWindow("" + i);
        setThreshold(min[i], max[i]);
        run("Convert to Mask");
        if (filter[i] == "stop") run("Invert");
    }
    imageCalculator("AND create", "0", "1");
    imageCalculator("AND create", "Result of 0", "2");
    for (i = 0; i < 3; i++) {
        selectWindow("" + i);
        close();
    }
    selectWindow("Result of 0");
    close();
    selectWindow("Result of Result of 0");
    rename(a);
    // Colour Thresholding------------- 

    run("Create Selection");
    /*Close dialog!*/
    selectWindow("Threshold Color");
    run("Close");

    setBatchMode(false);

    roiManager("Add");
    roiManager("Split");
    roiManager("Measure");
    wait(100);
    /*Save the results!*/
    selectWindow("Results");
    saveAs("Results", output + nameOfFile + ".csv");
    /*Close all other dialogs!*/
    selectWindow("ROI Manager");
    run("Close");
    selectWindow("Results");
    run("Close");
}

To control the sequences you can remove the setBatchMode() command which speeds up the macro!
Also you can close some dialogs at the end and not in the loop if you want to optimize the macro a little bit!

In addition you could create an extra function for the color threshold function part to replace it more easily if you have to change settings.

1 Like

Hi Ana,

@Bio7’s advice is great, but as an alternative I also wrote a plugin to export the results table to a single .xlsx file that is saved to the user’s desktop and that works very well with batch processing. Hopefully this suggestion is not seen as spam, and by-all-means feel free to totally ignore it.
You can install the plugin from the imagej update manager:


Just tick ‘ResultsToExcel’ as in the above picture.

Then to implement the plugin use the following command in a macro:

run ("Read and Write Excel", "This is the title I want to use instead of the image title");

If you leave out the “This is the title…” portion, then the plugin will use the image title (if you have multiple images open, then it’ll be the first you opened that is still open).

Further info and a quick tutorial can be found here: