0

I have recently discovered macros and how they can surprisingly ease my life working on Fiji/ImageJ.

I created this macro:

run("Image Sequence...", "open=/home/mario/Desktop/prueba/1/Image-000002.tif");
selectWindow("1");
//setTool("rectangle");
makeRectangle(406, 346, 1132, 845);
run("Z Project...", "projection=[Average Intensity]");
saveAs("Tiff", "/home/mario/Desktop/prueba/1/AVG_1.tif");

What this macro does is to import an image sequence stored in the referenced folder, to align the stack of images using the Template matching plugin, to do focus stacking using the Z Project function (Image > Stacks > Z Project...) and to save the newly generated image in the same folder using the tiff extension.

However, I do have a general folder with plenty subfolders filled with tiff files, so applying one by one the previous macro in each folder could become a tedious task as well. I came across with this macro that deals with batch processing:

// "BatchProcessFolders"
//
// This macro batch processes all the files in a folder and any
// subfolders in that folder. In this example, it runs the Subtract 
// Background command of TIFF files. For other kinds of processing,
// edit the processFile() function at the end of this macro.
   requires("1.33s"); 
   dir = getDirectory("Choose a Directory ");
   setBatchMode(true);
   count = 0;
   countFiles(dir);
   n = 0;
   processFiles(dir);
   //print(count+" files processed");

   function countFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              countFiles(""+dir+list[i]);
          else
              count++;
      }
  }

   function processFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              processFiles(""+dir+list[i]);
          else {
             showProgress(n++, count);
             path = dir+list[i];
             processFile(path);
          }
      }
  }

  function processFile(path) {
       if (endsWith(path, ".tif")) {
           open(path);
           run("Subtract Background...", "rolling=50 white");
           save(path);
           close();
      }
  }

However, I do not know how to merge my automated tasks written in my macro with the latter, as I am not an expert in coding.

To sum up, I would like to run my macro in every folder and subfolder automatically from a root directory of my selection.

Can anybody edit and merge the previous macros in order to accomplish my requirements?

antecessor
  • 2,688
  • 6
  • 29
  • 61
  • You asked [the same question](http://forum.imagej.net/t/automate-tasks-using-macros-in-images-placed-in-folders-and-subfolders/9468?u=imagejan) on the ImageJ forum and received some answers there. When cross-posting on different locations, please _always_ disclose that you're doing so: you might not be the only one having this issue, and others also want to be able to find the answer to your question. – Jan Eglinger Feb 21 '18 at 15:58

1 Answers1

0

Just in case somebody came across this question, the answer can be found in the ImageJ forum.

antecessor
  • 2,688
  • 6
  • 29
  • 61