Login  Register

Re: Calculate local stdDev, using a sliding box

Posted by Michael Schmid on Feb 09, 2016; 6:03pm
URL: http://imagej.273.s1.nabble.com/Calculate-local-stdDev-using-a-sliding-box-tp5015587p5015589.html

Hi Steve,

one quick thing: avoid the median in the options, it is not needed and is the slowest operation by far. Also the MIN_MAX is not needed.

For better performance:
Do the calculation by summing up the pixel values and their square for the first for the first ROI (use a double-precision number).
  variance = (sumOfSuares - sumOfValues*sumOfValues/n)/n  [OR divide by (n-1) instead of n, as you prefer]
  stddev = sqrt(variance)
Then, when moving the ROI, just add the values and their squares for the pixels that enter the ROI, and subtract for the pixels that leave the ROI. Then recalculate the stddev from the new sums.

Even faster: Do the sums over the boxWidth for each row (as above), and put them into arrays. Then apply the same strategy in y.

If it needn't be boxes but it can be circles, you can also use the ImageJ-builtin 'variance' filter. It needs about 55 seconds for a 4096x4096 image with radius=204 (diameter 409) on my old computer.

Michael
________________________________________________________________
On Feb 9, 2016, at 17:43, Steve Wolf wrote:

> Attempting to calculate local stdDev, using a sliding box, on a 4096x4096 image.  I’m using the following Python script, but it runs extremely slow, any suggestions on speeding it up?
>
>
> from ij import IJ
> from ij import ImagePlus  
> from ij.process import FloatProcessor  
> from array import zeros  
> from ij.process import ImageStatistics as IS  
>
> imp = IJ.getImage()
> ip = imp.getProcessor()
>
> options = IS.MEAN | IS.MEDIAN | IS.MIN_MAX | IS.STD_DEV
> stats = IS.getStatistics(ip, options, imp.getCalibration())
> boxW = 409
> boxH = 409
> imageW = 4096
> imageH = 4096
> i = 0
>
> nwidth = 4096
> nheight = 4096
> pixels = zeros('f', nwidth * nheight)
>
> for x in xrange(0,imageW,1):
>                for y in xrange (0,imageH,1):
>            imp.setRoi( y, x, boxW, boxH)
> #            rimp = imp.getRoi
>            stats = IS.getStatistics(ip, options, imp.getCalibration())
> #            print x, ",", y, ",", stats.stdDev
>            pixels[i] = stats.stdDev
>            i += 1
>
> #generate image of local stdDev
> fp = FloatProcessor(nwidth, nheight, pixels, None)  
> imp = ImagePlus("Local StdDev", fp)  
> imp.show()  
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

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