// This macro creates a composite selection from a binary mask or
// thresholded image. It runs the particle analyzer to find objects
// in the mask, inverts the image so holes become objects, runs the 
// particle analyzer a second time, then creates a composite ROI 
// by adding the object outlines and subtracting the hole outlines.

// In ImageJ 1.37 and later, create composite selections using
// the Edit>Selection>Create Selection command, which is 
// much faster than this macro.

  setBatchMode(true);
  run("Set Measurements...", "area perimeter circularity decimal=3");
  run("Analyze Particles...", "size=1 circularity=0 show=Nothing clear record");
  count1 = nResults;
  run("Invert");
  run("Analyze Particles...", "size=1 circularity=0 show=Nothing record exclude");
  run("Invert");
  for (i=0; i<nResults; i++) {
      x = getResult('XStart', i);
      y = getResult('YStart', i);
      if (i<count1)
          setKeyDown("shift"); //add
      else
          setKeyDown("alt"); //subtract
      doWand(x,y);
  }
