excluding ROI Manager selections from Analyze Particles

Posted by Crowell Elizabeth on
URL: http://imagej.273.s1.nabble.com/excluding-ROI-Manager-selections-from-Analyze-Particles-tp3686462.html

Hello list members,

I have a very precise question: how can I run analyze particles from a
macro on a thresholded image and exclude the ROI selections in the ROI
Manager from the particle analysis?

Basically, I want to generate some ROIs and store them in the manager,
then loop through each ROI selection and duplicate it to a new
individual image.  Then I threshold the new image.  I would like to
analyze just the thresholded objects in the new image, but analyze
particles also gives me measurements for the ROIs in the manager.
I cannot reset the manager, because I will need the ROIs during the
whole loop!  The macro where this problem arises is attached.  I realize
I could reset the ROI Manager and reopen it after each loop, but this
appears terribly awkward and will slow down the macro.

Is there any clever way to fix this problem?
In fact, I now have several colleagues interested by this same question,
because we have all encountered instances where we would like to keep
separate ROI Manager lists active simultaneously.
Maybe an option could be added to Analyze Particles so that it can
process either thresholded regions or ROIs in the Manager or both...

Thank you for any comments,

Elizabeth CROWELL

----------------------------------------------------------------------
Membrane Traffic and Cell Division Research Group
Institut Pasteur
28 rue du Dr Roux
75015 PARIS, France

Tel :  01.44.38.94.07
Fax : 01.45.68.89.54
----------------------------------------------------------------------

// "BatchMeasureCilia"
//
// This macro batch processes all the files in a folder and any
// subfolders in that folder. It opens all the FITC images and segments them
// after removing noise using a Gaussian blur subtraction.  Then it
// opens the corresponding Cy3 image (with the same number), thresholds, and
// measures the intensity in each ROI.
// The DAPIROIs are saved in the same folder as the images.  The results
// of the measurements are saved in a new folder called
// condition_ROImeasurements, one level up from the images.  Each results
// file is named after the image used for measurement (FITC000X).
// If no ROIs were selected in an image (due to automatic thresholding
// problems, for example) the macro prints out the name of the image
// in the log and moves on to the next image.

// The current settings are for selecting Ninein staining imaged at 60X.
// The macro does not work if there are images in the source directory.
// The images must be organized into subfolders (for ex. series1,
// series2, etc) and cannot be present at the same level as the subfolders.

// Modified from "BatchMeasureROIs" macro by E. Crowell 9/11/2010.

   requires("1.33s");
   dir = getDirectory("Please choose a source directory.");
   setBatchMode(false);
   
   GetFiles(dir);
   
   function GetFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/")) {
              GetFiles(""+dir+list[i]);
          } else {
             if (endsWith(list[i], ".tif")) {
                if (endsWith(list[i], "FITCmax.tif")) {
                   path = dir+list[i];
                   GetROIs(path);
                      MeasureROIs(path);
                }
             }
          }
      }
   }

   function GetROIs(path) {
        open(path);
        rename("filter1");
        run("Duplicate...", "title=filter2");

        // the sigma parameters might need to be adjusted here.
        selectImage("filter1");
        run("Gaussian Blur...", "sigma=5");
        run("Variance...", "radius=3");
        selectImage("filter2");
        run("Gaussian Blur...", "sigma=25");
        imageCalculator("Subtract create", "filter1","filter2");
        rename("Difference");
        selectImage("filter1");
        close();
        selectImage("filter2");
        close();
        selectImage("Difference");
        run("OtsuThresholding 16Bit");
        run("Convert to Mask", "calculate");
        run("Fill Holes", "slice");
        run("Watershed", "slice");

        // the filter size will need to be adjusted here.
        run("Analyze Particles...", "size=50-1000 circularity=0.50-1.00 show=Nothing add");

        length1 = roiManager("count");
        if (length1>0) {
                roiManager("Save", substring(path,0,lastIndexOf(path, "."))+"_ROI.zip");
        }
        selectImage("Difference");
        close();
    }



   function MeasureROIs(path) {
        corresp = replace(path,"FITC","Cy3");
        open(corresp);
        MeasImage=getImageID();

        parentdir = File.getParent(corresp);
        dest = parentdir+"_ROImeasurements";
        File.makeDirectory(dest);
        number = substring(corresp,lastIndexOf(corresp,"/"),lastIndexOf(corresp,"Cy3max"));
        length = roiManager("count");
       
        if (length>0) {
                for(j=0;j<length;j++){
                        roiManager("Select", j);
                        run("Enlarge...", "enlarge=50");
                        run("Duplicate...", "title=dup");
                        run("Select None");
                        run("OtsuThresholding 16Bit");
                        selectImage("dup");
                        run("Analyze Particles...", "size=10-1000 circularity=0.00-0.80 show=[Overlay Outlines]");
                        wait(5000);
                        selectImage("dup");
                        close();
                        saveAs("Measurements", dest+number+"_"+j);
                        run("Clear Results");
                }

/* if (isOpen("Results")) {
                        selectWindow("Results");
                        run("Close");
                }
*/
                if (isOpen(MeasImage)) {
                        selectImage(MeasImage);
                        close();
                }
                roiManager("reset");
        } else {
                print("No ROIs were selected in image "+corresp);
                if (isOpen(MeasImage)) {
                        selectImage(MeasImage);
                        close();
                }
        }
   }