Batch processing in subfolders

imagej
batch-processing
macro

#1

Hi girls/guys,
your forum is very helpful and I found a lot of help here!
I am new to programming and ImageJ macro writing, so excuse my ignorance…
I copied some code snippets together and tried and tested already for a couple of days. But now I gut stuck here.

Here is what I want to do:
I have a folder with 106 subfolders, each containing a series of TIF images, named Series001_ch00.tif, Series001_ch01.tif and Series001_ch02.tif
The last subfolder is then named Series106 and also the files.

I want to process each subfolder after another, analysing the files in them.

I tried it with the template “Process folders”, but somehow the Images just get opened in a loop and are not being analyzed.

The code (without processing subfolders) is:

dir1 = getDirectory("Where are the images"); // This is where the ch00 images are
list = getFileList(dir1); // This lists all the files in ch00 folder
for (g=0; g<list.length; g++) { // This goes through each file in ch00 iteratively
ch1name = list[g]; // This takes the name of the file and makes it a variable called ch1name
ch2name = replace(ch1name, "ch00" ,  "ch02" ); // This generates Ch2name variable by replacing IDs. Choose a unique identifier in your images
if (File.exists(dir1 + ch2name)); { // This looks in the Ch2 folder for files matching ch2name. If there is, then it enters the loop.

print(ch1name);
open(dir1 + ch1name);
print(ch2name);
open(dir1 + ch2name);

// Step 1: generate ROIs from the channel ch00

ids=newArray(nImages); //checks how many images are currently open
n=nImages;
for (i=0;i<nImages;i++) {
selectImage(i+1);
title = getTitle; // registers the names of all open images
if (endsWith(title, "ch00.tif" )){

run("8-bit");
	run("Subtract Background...", "rolling=50");
 AUTO_THRESHOLD = 5000; 
 getRawStatistics(pixcount); 
 limit = pixcount/10; 
 threshold = pixcount/AUTO_THRESHOLD; 
 nBins = 256; 
 getHistogram(values, histA, nBins); 
 g = -1; 
 found = false; 
 do { 
         counts = histA[++g]; 
         if (counts > limit) counts = 0; 
         found = counts > threshold; 
 } while ((!found) && (g < histA.length-1)) 
 hmin = values[g]; 
 g = histA.length; 
 do { 
         counts = histA[--g]; 
         if (counts > limit) counts = 0; 
         found = counts > threshold; 
 } while ((!found) && (g > 0)) 
 hmax = values[g]; 

 setMinAndMax(hmin, hmax); 
 //print(hmin, hmax); 
 run("Apply LUT"); 
	run("Gaussian Blur...", "sigma=1.0"); 
	run("Auto Local Threshold", "method=Median radius=15 parameter_1=0 parameter_2=0");
	run("Watershed");
	run("Analyze Particles...", "size=150-15000 pixel display clear add");
}
}

// Step2: apply the ROIs to the channel ch02

ids=newArray(nImages); //checks how many images are currently open
n=nImages;
for (i=0;i<nImages;i++) {
selectImage(i+1);
title = getTitle;

  if (endsWith(title, "ch02.tif")){ 
     run("8-bit");
original = getImageID();
run("Duplicate...", " ");
copy = getImageID();
selectImage(copy);
run("Median...", "radius=2"); //runs a median filter to differenciate clustered and very bright dots
selectImage(original);
imageCalculator("Subtract", original,copy);
run("Invert LUT");
run("Auto Threshold", "method=Triangle white");
run("From ROI Manager");
name1 = getTitle; 
            dotIndex = indexOf(name1, "."); 
            title1 = substring(name1, 0, dotIndex); 
n = roiManager("count");
        for (f=0; f<n; f++)  {
        roiManager("select", f);
         run("Analyze Particles...", "size=1-30 pixel circularity=0.50-1.00 show=Nothing display summarize");}
selectWindow("Summary"); 
saveAs("Text", dir1 + File.separator + title1 + ".txt"); 
roiManager("reset"); 
 selectWindow("Results"); 
   run("Close"); 
selectWindow("ROI Manager"); 
run("Close"); 
    macro "Close All Windows" { 
      while (nImages>0) { 
          selectImage(nImages); 
          close(); 
      } 
  } 
  }
}	
}
}

Can you help me?


#2

IF you use ImageJ (not FIJI), you can put your code in a text window (Plugins>New>Macro), then activate the Debug feature by switching to the Macro.txt window and select Debug Macro from the (now changed) menu bar. Then you can single-step through your code and see what is contained in variables and why there is no processing of your images.

Once this all works, you can migrate your code to the batch processor/process folders/FIJI, if necessary.