Login  Register

Re: Method of creating a stack zprojection image using the mode / modal value

Posted by Gabriel Landini on Jan 01, 2009; 6:19pm
URL: http://imagej.273.s1.nabble.com/Method-of-creating-a-stack-zprojection-image-using-the-mode-modal-value-tp3694140p3694145.html

On Thursday 01 January 2009, jeremydouglass wrote:
>  It appears to be working correctly although it is quite inefficient.

The modification below runs about 3 times faster on the mri-stack.tif file.
If implemented as a plugin it might run even faster.

Cheers

Gabriel

//------------------8<-------------------------
// Smallest valued mode of a stack
if (nSlices<2) exit("This macro requires a stack.");
makeModalZProject();

function makeModalZProject() {
   //t=getTime();
   setBatchMode(true);
   run("Reslice [/]...", "input=1.000 output=1.000 start=Top");
   depth = nSlices;
   width=getWidth();
   height=getHeight();
   for (z=0; z<depth; z++) { // loop through slices
        setSlice(z+1);
        for (x=0; x<width; x++) { // get the z pixels
            h=newArray(256);
            modef=0;
            zmode=-1;
            for (y=0;y<height;y++){
               p= getPixel(x,y);
               h[p]++;
               if (h[p]>modef)
                 modef=h[p];
            }
            // the following will get the smallest valued mode
            i=0;      // change to 255 to get the largest valued mode
            while(h[i]<modef)  
               i++;   // change to i--; to get the largest valued mode
            zmode=i;
            setPixel(x, 0, zmode);
        }
  }
  run("Reslice [/]...", "input=1.000 output=1.000 start=Top");
  run("Duplicate...", "title=stack_mode");
  setBatchMode(false); // display
  //print (getTime()-t);
}
//------------------8<-------------------------