Login  Register

Re: Calculate local stdDev, using a sliding box

Posted by Kenneth Sloan-2 on Feb 09, 2016; 6:09pm
URL: http://imagej.273.s1.nabble.com/Calculate-local-stdDev-using-a-sliding-box-tp5015587p5015590.html

There are ways to stably update mean and stdDev - but you would have to roll your own.

Briefly, you keep track of Sum(x) and Sum(x*x).  When you move the window, you can subtract out the column (row) you are leaving behind and add in the column(row) you are adding.  to be extra clever, move in a serpentine pattern so you always move by 1 row or 1 col.

Be careful though - make sure all your accumulators stay within bounds.

At the level you are using now, I don’t think a speedup is possible, but I don’t know that for sure.  You may be making as many as 5 (unlikely, but…) passes over each window (plus a sort, if Median is a true median).  Perhaps I’m wrong about that - but I’ll bet there is at least 1, and perhaps 2 (I would probably use 2), complete passes over each window as you go.  The package can’t know your pattern of ROIs, and can’t optimize.  You can - but you have to write your own low level code to do that.

One more thing - unless I mis-read the code, you have edge effects issues, and the stdDev image is translated wrt the original image.  If I’m wrong, I’m sure someone will correct me.

--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.




> On Feb 9, 2016, at 10:43 , Steve Wolf <[hidden email]> 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