Login  Register

Re: moving average in stack

Posted by Wayne Rasband on Jul 12, 2007; 9:52pm
URL: http://imagej.273.s1.nabble.com/moving-average-in-stack-tp3698864p3698867.html

> I wonder if this would also work? (haven't tried it):
>
> Reslice the stack starting at top
> Convolve with a kernel
> 0 1 0
> 0 1 0
> 0 1 0
> Reslice the stack back

This does appear to work. Here is a macro that does 3, 5 or 7 slice
moving averages in the z direction of a stack.

-wayne

   k3 = "[0 1 0 0 1 0 0 1 0]";
   k5 = "[0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0]";
   k7a = "[0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1";
   k7b = " 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0]";
   k7 = k7a + k7b;
   Dialog.create("Running Average");
   Dialog.addChoice("Slices to Average:", newArray("3", "5", "7"));
   Dialog.addCheckbox("Keep Source Stack", true);
   Dialog.show;
   n = Dialog.getChoice;
   keep = Dialog.getCheckbox;
   kernel = k3;
   if (n=="5")
       kernel = k5;
   else if (n=="7")
       kernel = k7;
   if (nSlices==1)
       exit("Stack required");
   id1 = getImageID;
   // re-slicing may not work if stack is scaled
   setVoxelSize(1, 1, 1, "pixel");
   getMinAndMax(min, max);
   run("Reslice [/]...", "input=1 output=1 start=Top");
   id2 = getImageID;
   if (!keep) {selectImage(id1); close;}
   selectImage(id2);
   run("Convolve...", "text1="+kernel+" normalize stack");
   run("Reslice [/]...", "input=1 output=1 start=Top");
   setMinAndMax(min, max);
   selectImage(id2);
   close;