Authomatic image processing

Hello I’m new with ImageJ macros and I was trying to write a new macro to process all the pictures from one folder. My idea was to process all the pictures (by analyse particles) obtain the summarize option of each analysis and save all the results in the same text file. I writted this macro but no error dialog appears, but also no results file:

 input = getDirectory("xxx");

    suffix = ".tif";

    processFolder(input);

    function processFolder(input) {
        list = getFileList(input);
        print(list.length);     
        for (i = 0; i < list.length; i++) {
            if(File.isDirectory(input + list[i]))
                processFolder("" + input + list[i]);
    	}
    }

    function processFile(input, file) {
run("8-bit");
"Auto Threshold Yen Dark" {
setBatchMode(true);
setAutoThreshold("Yen dark");
setOption("BlackBackground", true);
setBatchMode("exit and display");
run("Convert to Mask");
setBatchMode(false);
    }
run("Close");
run("Watershed");
run("Analyze Particles...", "size=4-Infinity pixel circularity=0.30-1.00 summarize");
    }
    
    function savesummarydata(input, file) {
selectWindow("Summary");
saveAs("Results", "folder of election);
    }

@nacherso

I would use the Process Folder template that is available with Fiji. If you open the Script Editor, just go to Templates > ImageJ1.x > Examples > Process Folder (IJ1 Macro) and just be sure to select IJ1 Macro in the Languages menu.

You can copy/paste your code then into the processFile() function. You can also go through this helpful workshop on Scripting with Fiji - the corresponding slides are here (more recently updates slides are here). Here are some other helpful links for scripting in ImageJ:

1 Like

Hi,

Is the code you provided your macro or only some sample code ?
Because inside, you make no call to functions processFile or savesummarydata (a " is missing at the end of SaveAs)

Inside processFile, you don’t open any image.
Is your setBatchMode necessary ?

You could also use the batch mode (process>Batch>Macro) that allow to process a whole directory.

Nico

Hello thanks for the replies, I tried to apply the processFile macro, and simplify the macro, but I still have problems. I attach the macro code:

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

processFolder(input);

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) {
open(input, file);
  	originalTitle = getTitle();

run("8-bit");
"Auto Threshold Yen Dark" {
setBatchMode(true);
setAutoThreshold("Yen dark");
setOption("BlackBackground", true);
setBatchMode("exit and display");
run("Convert to Mask");
setBatchMode(false);
    }

run("Close");
run("Watershed");
run("Analyze Particles...", "size=4-Infinity pixel circularity=0.30-1.00 summarize");
}

The problem is that all the images are opened as one single image with multiple stacks, and then it opens again the same multistack picture (I have 14 pictures in the folder so it does it 14 times). Furthermore I tried to set the Yen threshold with automatic values for each photo but it keeps asking me to determine which threshold method use in each picture.

Sorry for bothering.

No problem.

You need to write:

open(input + File.separator + file);

instead of

open(input, file);

Why do you turn on and off the batchmode?
Just curiosity.

There are some of your commands not necessary and some I don’t understand. An example that does work with minimal frizz is here:

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

setBatchMode(true);

processFolder(input);

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) {

setOption("BlackBackground", true);

open(input + File.separator + file);
originalTitle = getTitle();

run("8-bit");
run("Auto Threshold", "method=Yen white");

run("Watershed");
run("Analyze Particles...", "size=4-Infinity pixel circularity=0.30-1.00 summarize");

close(originalTitle);

}

For a good intensity based segmentation I would also recommend to use appropriate image processing. Filters to get rid of noise and background subtraction. Also reducing the bitdepth is with the AutoThreshold method not necessary and just reduces the amount of information in your images.

2 Likes

Thank you very much, the command to open separated files help me a lot

open(input + File.separator + file);

About the batchMode, I copied a list of commands from a post in another website (describing the autothreshold command), they assured that turning on/off the batch mode avoid confusions on the FIJI commands, and I didn’t think it to much because the command was working like that (now that I saw your answer I delete it).

While I was waiting for replies I also found another alternative that also works nicely. Because the pictures were opened like a single multi-stack picture, I changed the commands to process a multi-stack picture, obtaining separated results of each stack, like this:

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

open(input);
  	originalTitle = getTitle();
run("8-bit");
setAutoThreshold("Default dark");
//run("Threshold...");
setAutoThreshold("Yen dark");
setAutoThreshold("Yen dark");
setOption("BlackBackground", true);
run("Convert to Mask", "method=Yen background=Dark calculate black list");
run("Watershed", "stack");
run("Analyze Particles...", "size=4-Infinity pixel circularity=0.30-1.00 summarize stack");
selectWindow("Summary of INPUT (RED)");
saveAs("results", "route/ SynapticAutoResults.csv");

Following your list of commands I also tried:

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

processFolder(input);

function processFolder(input) {
	list = getFileList(input);
	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, list[i]);
	}
}

function processFile(input, file) {
open(input + File.separator + file);
  	originalTitle = getTitle();
run("8-bit");
setAutoThreshold("Default dark");
//run("Threshold...");
setAutoThreshold("Yen dark");
setAutoThreshold("Yen dark");
setOption("BlackBackground", true);
run("Convert to Mask", "method=Yen background=Dark calculate black list");
run("Watershed");
run("Analyze Particles...", "size=4-Infinity pixel circularity=0.30-1.00 summarize");
}
selectWindow("Summary");
saveAs("results", "route/ SynapticAutoResults.csv");

NICELY WORKS, THANKS!!

About the filters to get rid of the background noise, I’m working with the red channel of pictures that I previously processed with the subtract background option. Which other filter do you suggest?

Thank you very much!

Noise will be in your signal as well as the background. Depends on how much your images are affected by noise and what your segmentation aim is. If you see many single pixel objects in your mask you need a filter, in my experience that is usually always the case. I would perform a background subtraction then on the filtered image.

The Gaussian Blur is very good with noise and signal homogeneities but blurs out the edges of your objects. The Median Filter is okay with noise and preserves the edges.

If the noise is not very high and the objects that you want to segment are homogeneous in their signal go for the Median Filter. Just be aware that you cannot use the Median Filtered images for any image intensity quantification. It is a nonlinear filter.

Thanks! I will try the different filters you suggest.