Login  Register

Re: Calculate local stdDev, using a sliding box

Posted by Stefan Helfrich-2 on Feb 10, 2016; 3:07pm
URL: http://imagej.273.s1.nabble.com/Calculate-local-stdDev-using-a-sliding-box-tp5015587p5015592.html

Dear Steve,

you could also use ImageJ Ops [1] to achieve your goal. Just copy the following to a new Jython script in the Script Editor and run it with an open image. It will ask for a windowSpan, which is the span of your sliding window (span==3 --> 7x7 window).

# @Dataset input
# @Integer windowSpan
# @OpService ops
# @UIService ui

from net.imglib2.algorithm.neighborhood import RectangleShape
from net.imglib2.outofbounds import OutOfBoundsMirrorFactory
from net.imglib2.outofbounds.OutOfBoundsMirrorFactory import Boundary

# Generate output image that has same dimensions as input
output = input.getImgPlus().factory().create(input, input.getImgPlus().firstElement())

# Apply mean filter using a RectangleShape with a span of windowSpan pixels
ops.filter().mean(output, input, RectangleShape(windowSpan, False), OutOfBoundsMirrorFactory(Boundary.SINGLE))

# Display the output
ui.show("Output", output)

All the best,
Stefan

[1] http://imagej.net/ImageJ_Ops

On Tue, 9 Feb 2016 11:43:57 -0500, 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