excluding ROI Manager selections from Analyze Particles

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

excluding ROI Manager selections from Analyze Particles

Crowell Elizabeth
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();
                }
        }
   }
Reply | Threaded
Open this post in threaded view
|

Re: excluding ROI Manager selections from Analyze Particles

Michael Schmid
Hi Elizabeth,

what about combining the ROIs in the ROI Manager and then  
Edit>Selection>Make Inverse?

Michael
________________________________________________________________

On 10 Nov 2010, at 17:39, Crowell Elizabeth wrote:

> 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();
> }
> }
>    }
Reply | Threaded
Open this post in threaded view
|

Re: excluding ROI Manager selections from Analyze Particles

Crowell Elizabeth
Hello Michael,

I'm not sure I understand what you mean, but it sounds like this
procedure will allow me to run analyze particles on the regions outside
the ROIs.  In fact, the ROIs overlap with the thresholded areas that I
want to measure in many cases, so measuring outside these regions will
not solve the problem.

I will try to explain a bit better the behavior I observe while the
function "MeasureROIs" is running:

The first ROI is selected on my original image and enlarged...
The enlarged selection is duplicated to a new mini image...
The duplicated image is thresholded...
Then I want analyze particles to measure the thresholded regions in the
duplicated image.  However, what it does is to transfer each ROI
selection in the manager on my duplicated image and measure that.  After
finishing with the ROIs, analyze particles measures the thresholded regions.

So in the end, the results file represents measurements of each ROI
selection and the thresholded regions at the end of the list.  Oddly
enough, this only occurs for the first loop.  Afterwards, the ROIs are
not measured!

Since the mini duplicated image does not contain the same xy positions
as in the original image, all the ROI selections appear at the origin in
the duplicated image.  They include the thresholded region that I want
to measure.  Perhaps I can deposit for download an example image and ROI
set to illustrate this?

If only I could write: run("Analyze Particles...", "Not ROIs please!",
"size=10-1000 etc etc...

Kind Regards,
Elizabeth



Michael Schmid a écrit :

> Hi Elizabeth,
>
> what about combining the ROIs in the ROI Manager and then
> Edit>Selection>Make Inverse?
>
> Michael
> ________________________________________________________________
>
> On 10 Nov 2010, at 17:39, Crowell Elizabeth wrote:
>
>> 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
>> ----------------------------------------------------------------------