Batch processing on image sequence in two steps



Hello everybody,

I spent the last week working on a code able to batch process a series of binary images representing 2-D sections of rock particles embedded in epoxy resin. Each original grayscale image contained a certain number of particles, each internally composed by glass, crystals and bubbles. What I want is calculate each component for each particle in one image, then repeat the action for all the images in a specific folder.

Through image processing in Photoshop and then Imagej (using another code I wrote), I isolated each single feature from each processed grayscale image creating three separate files with the feature (i.e., glass, vesicles and crystals) in black and the rest in white. In addition, as it essential to calculate the relative volume of each feature inside each particle, I created a fourth image containing the whole particles in black, with the background in white.

The result should consist in the four binary slices where each features have been numbered, and a final csv table result containing the area fraction calculation for all the particles for all the images.

In my head, the code should do, in order:

  1. open the input folder containing the files, which are named like this:
  • filename_001_form.tif
  • filename_001_glass.tif
  • filename_001_ves.tif
  • filename_001_xls.tif
  • filename_002_form.tif
  • filename_002_glass.tif
  • filename_002_ves.tif
  • filename_002_xls.tif
  • etc…
  1. Select the _001_form.tif file
  2. measure the area for all the particles and add it to the ROI manager,
  3. Save a tiff with the numbered selection for the form slice
  4. select now the 001_glass, 001_ves and 001_xls files and calculate the area fractions for them, adding each feature to the ROI and results to the results window
  5. Save a tiff for each numbered feature slice
  6. Repeat the step 2-4 for all the file numbers
  7. Save the final results window using read and write excel plugin in Fiji.

Now this is my code which partially works, but with the following issues:

  1. It keeps asking me for the image sequence, I suppose because of the position of that part of the code
  2. The form file is saved but not numbered
  3. I would like to have each STACK label in the results window renamed for the appropriate component form, glass, vesicles, crystals
  4. Ideally, I would also like to have the corresponding number for each feature, for each particle, for each image.
//@ File (label = "Input directory", style = "directory") input
//@ File (label = "Output directory", style = "directory") output
//@ String (label = "File suffix", value = ".tif") suffix

//-- Call the initial function to get a list of files


//-- FUNCTIONS ---
    function processFolder(input) {
    	list = getFileList(input);
    	list = Array.sort(list);
    	for (i = 0; i < list.length; i++) {
    		if(endsWith(list[i], suffix))
//-- Call the file processing function
    			processFile(input, output, list[i]);

    function processFile(input, output, file) {
    print("Processing: " + input + File.separator + file);
    //-- Open the file
    //dotIndex = indexOf(title, "."); 
    //title = substring(title, 0, dotIndex);

	//Start Loop
	run("Image Sequence...", "sort");
	run("Set Measurements...", "area_fraction display redirect=None decimal=3");
	run("Analyze Particles...", "exclude add slice");
	run("Select All");
	roiManager("multi-measure measure_all one append");
	//End Loop
	//-- Save out the labelled image and the results tables here:

	run("Read and Write Excel");
	//-- Close each image sequence before the next image is opened

I attached the input files (in png format as I was having problem uploading them) and some screenshot.

I cannot thank you all a lot in advance for the massive help you will give me, I am going mad trying to let it work.


  1. I am a bit confused. If each image is a separate file and you are processing them separately, why do you need to import an image sequence?
  2. Could you give an example of what you mean by “not numbered”? From what I can tell, you append to the existing name, so the number should already be in the existing name.
  3. Yes, use setSlice(number) together with setMetadata("Label", string).
  4. Not sure on this one.


Hi Andrew,

Thanks for your message and patience in trying to understand my mess. :slight_smile:

  1. So, let me give you a bit of background. The 3 (xls, glass, ves, excluding the _form file) files for a same file number (e.g., 002) represent different ROIs of the particles, so they come from processing in Photoshop first and ImageJ after one original grayscale image. Now the measurement itself works on taking the area and contour of the “form” file first in order to separate each particle contained in one image, whose coordinates are added to the ROI manager. With this information, the code should calculate the area fraction of each component (ROI) within each particle, for each image, for all the images. Ideally, in this macro, the form file has to be opened first and left there, then the image sequence with the three ROI files imported, and their results added to the ROI manager and recorded on the results table.
  2. I am sorry for this bad sentence, my brain processed correctly but the sentence made no sense. Basically what I see on the screen after the code runs is the image with a yellow selection of the perimeter (identifying each particle) and a number (see screenshot), which is recorded in the ROI manager. Going in the output folder though, the image is saved without such information, looking exactly the same as the corresponding input image.
  3. Where should I insert the command setSlice? sorry I am a bit rusty in coding, I have not been doing it for years.

It would be very helpful if someone could help me to adjust the code.

Thanks a lot!


Sorry for being away for so long. Do you still need help with this? If so, which parts?


Hey Andrew,

Don’t worry about that, I perfectly understand. In the end I solved the problem in another way, extracting one particle at time and calculating the internal features on each one on batch processing.