Login  Register

Problem with normalized variance for stack

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options Options
Embed post
Permalink
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Problem with normalized variance for stack

sathya1
Hi
I'm trying to extend the "Normalized variance" macro by Andy Weller from a single image to an entire stack. An then show those results in the results window. But I somehow am not able to make it work. Please help. This is the normalized variance macro by Andy Weller:

// A macro to determine image focal quality image-wide (not ROI-wide)
// Based on algorithm F-11 "Normalized Variance"
// In: Sun et al., 2004. MICROSCOPY RESEARCH AND TECHNIQUE 65, 139–149.
//
// Version: 0.3
// Date: 11/07/2006
// Author: Andy Weller
 
macro "Normalized_Variance" {
run("Clear Results");
run("Set Measurements...", "  mean redirect=None decimal=5");
run("Measure");
mean = getResult("Mean");
selectWindow("Results");
run("Close");
W = getWidth();
H = getHeight();
b = 0;
normVar = 0; // Set to 0 which is out of focus
for (j=0; j<H; j++) {
   for (i=0; i<W; i++) {
      p = getPixel(i,j);
      t = (p-mean)*(p-mean);
      b += t;
      }
   }
normVar = b/(H*W*mean); // Maximum value is best-focused, decreasing as defocus increases
print(normVar); // This can also (should) be changed to return(normVar)
}

thanks!
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Problem with normalized variance for stack

Michael Schmid
Hi Sathya,

you don't need the loop over the pixels; the variance is simply the square root of the standard deviation. Here is a macro for it:

macro "Normalized_Variance_for_Stack" {
  saveSettings();
  run("Clear Results");
  run("Set Measurements...", "  mean standard redirect=None decimal=5");
  for (slice=1; slice<=nSlices(); slice++) {
    setSlice(slice);
    run("Measure");
    mean = getResult("Mean", slice-1);
    stddev = getResult("StdDev", slice-1);
    normVar = stddev*stddev/mean;
    setResult("NormVar", slice-1, normVar);
  }
 restoreSettings();
}


Anyhow, for finding the best focus, this will work well only if you have many very small objects. Otherwise, the result will strongly depend on other factors than focus, such as noise and sloping background.
It may help to first filter the stack by strong unsharp masking, enhancing the high spatial frequencies, and maybe some noise reduction (depends on what the images look like).  Of course, there are much more sophisticated methods of finding the best focus...

Michael
________________________________________________________________
On Jan 31, 2013, at 17:47, sathya1 wrote:

> Hi
> I'm trying to extend the "Normalized variance" macro by Andy Weller from a
> single image to an entire stack. An then show those results in the results
> window. But I somehow am not able to make it work. Please help. This is the
> normalized variance macro by Andy Weller:
>
> // A macro to determine image focal quality image-wide (not ROI-wide)
> // Based on algorithm F-11 "Normalized Variance"
> // In: Sun et al., 2004. MICROSCOPY RESEARCH AND TECHNIQUE 65, 139–149.
> //
> // Version: 0.3
> // Date: 11/07/2006
> // Author: Andy Weller
>
> macro "Normalized_Variance" {
> run("Clear Results");
> run("Set Measurements...", "  mean redirect=None decimal=5");
> run("Measure");
> mean = getResult("Mean");
> selectWindow("Results");
> run("Close");
> W = getWidth();
> H = getHeight();
> b = 0;
> normVar = 0; // Set to 0 which is out of focus
> for (j=0; j<H; j++) {
>   for (i=0; i<W; i++) {
>      p = getPixel(i,j);
>      t = (p-mean)*(p-mean);
>      b += t;
>      }
>   }
> normVar = b/(H*W*mean); // Maximum value is best-focused, decreasing as
> defocus increases
> print(normVar); // This can also (should) be changed to return(normVar)
> }
>
> thanks!
>
>
>
> --
> View this message in context: http://imagej.1557.n6.nabble.com/Problem-with-normalized-variance-for-stack-tp5001625.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Problem with normalized variance for stack

sathya1
Thanks Michael! That helps a lot. I'm trying to detect focused images of isolated GFP-expressing cells within a stack. Right now, the normalized variance method works fairly decently. Its probably ~one frame off, but I will try your suggestion of the unsharp mask.
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Problem with normalized variance for stack

sathya1
In reply to this post by Michael Schmid
Hi Michael
Sorry to bother you again, but I'm having another problem. I'm finding it difficult to search for the highest value in the normalized variance column and report its index value. Can you help me out?
Thanks!
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Problem with normalized variance for stack

Michael Schmid
Hi Sathya,

this macro writes where it finds the maximum of the normalized variance.
Anyhow, it might be a good idea if you could have a look into macro programming yourself; it's not difficult, and you will be able to solve such problems yourself...

macro "Normalized_Variance_for_Stack" {
 saveSettings();
 run("Clear Results");
 run("Set Measurements...", "  mean standard redirect=None decimal=5");
 max = -1;
 sliceOfMax = -1;
 for (slice=1; slice<=nSlices(); slice++) {
   setSlice(slice);
   run("Measure");
   mean = getResult("Mean", slice-1);
   stddev = getResult("StdDev", slice-1);
   normVar = stddev*stddev/mean;
   if (normVar > max) {
     max = normVar;
     sliceOfMax = slice;
   }
   setResult("NormVar", slice-1, normVar);
 }
 print("Maximum in slice "+sliceOfMax);
restoreSettings();
}


Michael
________________________________________________________________
On Jan 31, 2013, at 22:39, sathya1 wrote:

> Hi Michael
> Sorry to bother you again, but I'm having another problem. I'm finding it
> difficult to search for the highest value in the normalized variance column
> and report its index value. Can you help me out?
> Thanks!
>
>
>
> --
> View this message in context: http://imagej.1557.n6.nabble.com/Problem-with-normalized-variance-for-stack-tp5001625p5001630.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Problem with normalized variance for stack

sathya1
Hi Michael

Thanks for your help. I eventually did manage to solve that problem myself. It was slightly long-winded and not as short as your macro. I used Array.getstatistics to get the maximum value and its index value. Anyway, I had fun writing the macro. Again, thanks for your help.
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Problem with normalized variance for stack

Michael Schmid
Hi Santhya,

using arrays is actually a good idea! Then you can also plot the values (see Plot.create) and you might even try fitting it with some function to determine the maximum with better accuracy (in case the curve looks noisy).

Michael
________________________________________________________________
On Feb 1, 2013, at 17:18, sathya1 wrote:

> Hi Michael
>
> Thanks for your help. I eventually did manage to solve that problem myself.
> It was slightly long-winded and not as short as your macro. I used
> Array.getstatistics to get the maximum value and its index value. Anyway, I
> had fun writing the macro. Again, thanks for your help.
>
>
>
> --
> View this message in context: http://imagej.1557.n6.nabble.com/Problem-with-normalized-variance-for-stack-tp5001625p5001635.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html