Login  Register

Re: random regions of interest

Posted by Bo de Lange on Oct 13, 2005; 7:01am
URL: http://imagej.273.s1.nabble.com/random-regions-of-interest-tp3704680p3704683.html

Ray Gilbert wrote:

I wanted to do exactly that some time ago and adapted this macro
http://rsb.info.nih.gov/ij/macros/RandomSampleAndMeasure.txt.

// RandomSampleAndMeasureWithinSelection-1step.txt
// Randomly places predefined Regions of Interest completely within
// the boundaries of a larger Region of Interest, measures and labels them
// G. Landini at bham. ac. uk and Bo de Lange
// Start with opening the image, and drawing the large ROI (MUST be
smaller than total image).

macro "RandomSampleAndMeasureWithinROI [F1]" {
  run("Select None");    

  saveSettings();
  original=getTitle();
  setForegroundColor(255,0,0);

  width = getWidth()-30;   // width of the randomly placed ROI
  height = getHeight()-30; // height of the randomly placed ROI
  RoisN =50; // number of ROIs
  trials=100 ; //maximum trials to avoid infinite loop

  i=0;
  j=0;

  xa=newArray(RoisN);
  ya=newArray(RoisN);

  run("Duplicate...", "title=Reference");




  selectWindow("Reference");
  run("8-bit"); //makes it greyscale
  run("RGB Color"); //RGB to display colours
  run("Restore Selection");
  run("Make Inverse");
  run("Fill");
  run("Select None");
 



   while (i<RoisN && j<trials){
        w = 30;
        h = 30;
        x = random()*width;
        y = random()*height;
        j++;
        //Check for pixels with value (255,0,0):
        flag= -1;
        makeRectangle(x, y, w, h);
        //Scanning the rectangle perimeter should be faster than
scanning the whole box.
        //This is slower, as checks all the points in the box:
        for (xs=x;xs<x+w;xs++){
            for (ys=y;ys<y+h;ys++){
                if (getPixel(xs,ys)==-65536) // pixel is (255,0,0)
                    flag=0;
            }
        }
        if (flag==-1){
            xa[i]=x;
            ya[i]=y;
            run("Fill");
            i++;
        }
   }
     

  close();
  selectWindow(original);
  setForegroundColor(255,255,0);
  for (j=0;j<i;j++){
    makeRectangle(xa[j], ya[j], w, h);
      run("Measure");
      run("Label");
  }
 restoreSettings();      
 run("Select None");
 run("Jpeg...", "save");
 run("Close");

}

>Is there any way in Image J to sample random regions of interest of
>fixed size within a defined area in a larger image?
>
>
>
>
>
>
>
>Failing that is it possible to set the grid up within a specified area
>rather than over the entire image?
>
>
>
>
>
>
>
>Thanks
>
>
>
>--
>
>Ray Gilbert
>
>Senior Research Assistant
>
>Lactation Gene Expression Project
>
>Molecular and Chemical Neuroanatomy Group
>
>Dept of Anatomy with Radiology
>
>Faculty of Medical and Health Sciences
>
>University of Auckland
>
>Private Bag 92019
>
>Auckland
>
>New Zealand
>
>Ph 0064 9 3737 599 ext 86022
>
>fax 0064 9 3737484
>
>[hidden email] <mailto:[hidden email]>  
>
>
>  
>


--
R.P.J. de Lange, PhD
Rudolf Magnus Institute of Neurosciences
P.O. box 80040
3508 TA Utrecht
The Netherlands

visiting address:
Stratenum, room 4.241
Universiteitsweg 100
3584 CG Utrecht

tel: +31-30-253 8924
        +31-30-253 8837 (lab)
fax: +31-30-253 9032