// "Convert Stack to Binary"
//
// This macro converts a stack to binary using locally calculated
// (per image) thresholds. This capability is built into the
// Process>Binary>Convert to Binary and Convert to Mask commands
// in ImageJ 1.38n or later.

  requires("1.38o");
  setBatchMode(true);
  run("Select None");
  run("8-bit");
  id = getImageID;
  for (i=1; i<=nSlices; i++) {
     setSlice(i);
     run("Duplicate...", "title=temp");
     run("Convert to Mask");
     invertingLUT = is("Inverting LUT");
     run("Copy");
     close;
     selectImage(id);
     run("Paste");
     if (i==1 && invertingLUT != is("Inverting LUT"))
         run("Invert LUT");
  }
  run("Select None");
