Posted by
Michael Schmid on
Jan 31, 2013; 5:42pm
URL: http://imagej.273.s1.nabble.com/Problem-with-normalized-variance-for-stack-tp5001625p5001627.html
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