# Standard deviation in ROI of image Hello all,

I am calculating signal to noise ratio of spots in a bunch of images. I am specifying the ROI for the signal and an ROI for the noise using a mask wave where 0 is outside the ROI and 1 is inside.

Calculating the mean can be done using a nice one-liner from AG that I found in another thread. I'm curious if there is a similar one line MatrixOp for getting the standard deviation.

Here is my current code:
// snip
m0 = 0 // m0 is mask wave for signal ROI
MatrixOP/O aa = sum(m0 * imgMat) / sum(m0) // calculates the mean
qW[i] = aa
m1 = 0 // m1 is mask wave for noise ROI
MatrixOP/O bb = sum(m1 * imgMat) / sum(m1) // get mean
Duplicate/O/FREE imgMat,m2
m2 -=bb // subtract mean from image
m2[][] *=m1[p][q] // apply the mask
MatrixOP/O cc = sqrt(sum(m2 * m2) / sum(m1)) // calculate SD
qW[i] = cc
// snip

Getting SD is 5 lines and I wonder if it would be faster to push the values in the ROI to a 1D wave and do WaveStats, or if there is some MatrixOP magic I am missing.
Thanks for any help.

First let's take a look at how you could change your code snippet:

m0 = 0 // m0 is mask wave for signal ROI
MatrixOP/O aa = sum(m0 * imgMat) / sum(m0) // calculates the mean
qW[i] = aa
m1 = 0 // m1 is mask wave for noise ROI
MatrixOP/O bb = sum(m1 * imgMat) / sum(m1) // get mean
Variable bb0=bb
// There is no need to waste time on this duplication; MatrixOP takes care of it:
// Duplicate/O/FREE imgMat,m2
// m2 -=bb // subtract mean from image
// m2[][] *=m1[p][q] // apply the mask
// MatrixOP/O cc = sqrt(sum(m2 * m2) / sum(m1)) // calculate SD
MatrixOP/O cc=sqrt(sum(magsqr((imgMat-bb0)*m1))/sum(m1))
qW[i] = cc

In practice, depending on the size of your images and ROI's, ImageStats may be more efficient because in IP7 ImageStats is automatically multithreaded.

Also note that when you work with ROI in MatrixOP, included pixels correspond to a value of 1 in the mask while ImageStats requires that included pixels are marked by 0 in the ROI wave.

A.G.
WaveMetrics, Inc.