Login  Register

Batch Z Projection

Posted by Mario Emmenlauer-3 on Jul 22, 2008; 6:42am
URL: http://imagej.273.s1.nabble.com/Batch-Z-Projection-tp3686091.html


Hi,

I wrote a Macro that batch Z projects a directory tree. There is one
thing that gives me a headache:
When I make a Z-projection, it results in a newly created image,
which is not *always* the active (selected) one. Subsequent calls
to save() sometimes save the MIP, sometimes the stack.

I would select the Image by string, but how do I know the user
has not just opened another image with the same title? Is there
  1) a way to be sure to select the image that was the result of the
     last operation, or
  2) a way to tell Z-project the name it should give to the resulting
     window, so I can generate my own string Id?

If there is a well-known way, please add it to the macros tutorial.

Thanks in advance, and keep up the good work,

    Mario Emmenlauer



// "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");
inputDir = getDirectory("Choose a Source Directory");
outputFormats = newArray("TIFF", "8-bit TIFF", "JPEG", "GIF", "PNG", "PGM", "BMP", "FITS", "Text Image", "ZIP", "Raw");
projectionTypes = newArray("Max Intensity", "Min Intensity", "Average Intensity", "Sum Slices", "Standard Deviation", "Median");
Dialog.create("Select MIP options");
Dialog.addMessage("Please select Batch Projection options");
Dialog.addChoice("Save projection as: ", outputFormats, "TIFF");
Dialog.addChoice("Use Projection Type: ", projectionTypes, "Max Intensity");
Dialog.addString("Default filename appendix", "_MIP");
Dialog.addCheckbox("Convert to 8 bit", true);
Dialog.addCheckbox("Recreate existing MIPs", true);
Dialog.addCheckbox("Select different output directory", false);
Dialog.show();
outputFormat = Dialog.getChoice();
projectionType = Dialog.getChoice();
outputAppendix = Dialog.getString();
outputEightBit = Dialog.getCheckbox();
shouldRecreate = Dialog.getCheckbox();
if( Dialog.getCheckbox()) {
  outputDir = getDirectory("Choose an Output Directory");
} else {
  outputDir = inputDir;
}

setBatchMode(true);
run("Bio-Formats Macro Extensions");
count = 0;
countFiles(inputDir);
n = 0;
processFiles(inputDir);
//print(count+" files processed");

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

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

function processFile(path) {
  outfileName = File.getName(path);
  outfileName = substring( outfileName, 0, lastIndexOf(outfileName, "."));

  // is the file itself a projection (we have created)?
  if( !endsWith(outfileName, outputAppendix)) {
    //outfileName = replace(path, ".", "_");
    //if( shouldRecreate || !File.exists(outputDir+outfileName)) {
      run("Bio-Formats Importer", "open=["+path+"] view=[Standard ImageJ] stack_order=Default split_channels split_timepoints open_all_series use_virtual_stack");
      if( nSlices > 1) {
        run("Z Project...", "start=1 stop="+nSlices+" projection=["+projectionType+"]");
        if( outputEightBit && bitDepth() % 16 == 0) {
          //getHistogram(values, counts, nBins[, histMin, histMax])
          //setMinAndMax(min, max);
          //setAutoThreshold();
          run("8-bit");
        }
        saveAs(outputFormat, outputDir+outfileName+outputAppendix);
        close();
      }
      close();
    //}
  }
}