Dear all,
I am trying to determine the normalised variance of an image. For this I need to minus the grey-level mean from each pixel's grey-level and square this value. I figure the best way to do this is to determine each pixel value by using getPixel() in a for loop. My first question: is the initial (origin) pixel getPixel(0,0) or getPixel(1,1)? My second question: if I need to sum these values, how can I keep an 'updater' variable in my for-loop? So for example, I have: W = getWidth(); H = getHeight(); for (i=0; i<=H-1; i++) { // Presuming that the origin is 0,0 // else I guess for (i=1; i<=H; i++)? for (i=0; i<=W; i++) { p = getPixel(W,H); b = (p-mean)^2; SOME UPDATER HERE TO A SUM OF b } } Once out of the loop, I can do things to the total. Cheers, Andy |
Hi,
First, yes, the origin is (0,0). And second, if you mean that you need to have a sum of all b values for all the pixels you just have to add it to the same variable as: double b=0; W = getWidth(); H = getHeight(); for (i=0; i<H; i++) { for (j=0; j<W; j++) { //you need to define another variable here p = getPixel(i,j); //you need a loop to all the pair of coordinates i,j b += (p-mean)*(p-mean); //adds the value to the variable b. At the end of the loop you'll have } // the sum of all (p-mean)*(p-mean) } Anyway, I think that there is a way of asking for that value with some method, but I'm sorry to say that I don't know how. Hope it helps, Agnès On 27/06/06, Andy Weller <[hidden email]> wrote: > > Dear all, > > I am trying to determine the normalised variance of an image. For this I > need to minus the grey-level mean from each pixel's grey-level and > square this value. I figure the best way to do this is to determine each > pixel value by using getPixel() in a for loop. > > My first question: is the initial (origin) pixel getPixel(0,0) or > getPixel(1,1)? > > My second question: if I need to sum these values, how can I keep an > 'updater' variable in my for-loop? > > So for example, I have: > > W = getWidth(); > H = getHeight(); > for (i=0; i<=H-1; i++) { // Presuming that the origin is 0,0 > // else I guess for (i=1; i<=H; i++)? > for (i=0; i<=W; i++) { > p = getPixel(W,H); > b = (p-mean)^2; > SOME UPDATER HERE TO A SUM OF b > } > } > Once out of the loop, I can do things to the total. > > Cheers, Andy > |
In reply to this post by Weller Andrew Francis
Is the grey-level mean for the entire image or a ROI? Would it be
more efficient to use the image calculator? M At 08:47 AM 6/27/2006, you wrote: >Dear all, > >I am trying to determine the normalised variance of an image. For this I >need to minus the grey-level mean from each pixel's grey-level and >square this value. I figure the best way to do this is to determine each >pixel value by using getPixel() in a for loop. > >My first question: is the initial (origin) pixel getPixel(0,0) or >getPixel(1,1)? > >My second question: if I need to sum these values, how can I keep an >'updater' variable in my for-loop? > >So for example, I have: > >W = getWidth(); >H = getHeight(); >for (i=0; i<=H-1; i++) { // Presuming that the origin is 0,0 > // else I guess for (i=1; i<=H; i++)? > for (i=0; i<=W; i++) { > p = getPixel(W,H); > b = (p-mean)^2; > SOME UPDATER HERE TO A SUM OF b > } > } >Once out of the loop, I can do things to the total. > >Cheers, Andy |
In reply to this post by Weller Andrew Francis
You can get the standard deviation directly from the Analyze/Measure
command in imageJ. The statistics you want can be defined in Analyze/ Set Measurements... Try the following macro which measures several statistics for a series of images in a stack. The results are included in a result table which can be imported into e.g. excel. run("Set Measurements...", " mean standard modal min median skewness kurtosis slice redirect=None decimal=3"); run("Clear Results"); for (i=1; i<=nSlices;i++){ setSlice(i); run("Measure"); } Gary. On Jun 27, 2006, at 3:47 PM, Andy Weller wrote: > Dear all, > > I am trying to determine the normalised variance of an image. For > this I > need to minus the grey-level mean from each pixel's grey-level and > square this value. I figure the best way to do this is to determine > each > pixel value by using getPixel() in a for loop. > > My first question: is the initial (origin) pixel getPixel(0,0) or > getPixel(1,1)? > > My second question: if I need to sum these values, how can I keep an > 'updater' variable in my for-loop? > > So for example, I have: > > W = getWidth(); > H = getHeight(); > for (i=0; i<=H-1; i++) { // Presuming that the origin is 0,0 > // else I guess for (i=1; i<=H; i++)? > for (i=0; i<=W; i++) { > p = getPixel(W,H); > b = (p-mean)^2; > SOME UPDATER HERE TO A SUM OF b > } > } > Once out of the loop, I can do things to the total. > > Cheers, Andy > > |
Il giorno 27/giu/06, alle ore 16:47, Gary Chinga ha scritto:
> You can get the standard deviation directly from the Analyze/ > Measure command in imageJ. The statistics you want can be defined > in Analyze/Set Measurements... by the way, this will be much faster... Vincenzo |
In reply to this post by Weller Andrew Francis
Hi,
To your questions (if I understand correctly): (1) getPixel(0,0) (2) simply declare it and update it (See below) \begin{macrocode} SOME_UPDATER=0; mean=42; //need this so I can test W = getWidth(); H = getHeight(); for (i=0; i<=H-1; i++) { for (j=0; j<=W; j++) { //changed i to j p = getPixel(j,i); //changed from p = getPixel(W,H); b = (p-mean)^2; //SOME UPDATER HERE TO A SUM OF b SOME_UPDATER+=b; } } print(SOME_UPDATER); \end{macrocode} Here's a different version/Approach. I use the calculator instead of a loop (example image is called "R^2_pre1" without the quotes) \begin{macrocode} run("Subtract...", "stack value=42"); //subtract run("Image Calculator...", "image1=R^2_pre1 operation=Multiply image2=R^2_pre1 create 32-bit stack"); //square run("Measure"); //get the mean (similar to the updater) \end{macrocode} Hope it helps! TeM -----Ursprüngliche Nachricht----- Von: ImageJ Interest Group [mailto:[hidden email]] Im Auftrag von Andy Weller Gesendet: Dienstag, 27. Juni 2006 15:48 An: [hidden email] Betreff: getPixel() for loop Dear all, I am trying to determine the normalised variance of an image. For this I need to minus the grey-level mean from each pixel's grey-level and square this value. I figure the best way to do this is to determine each pixel value by using getPixel() in a for loop. My first question: is the initial (origin) pixel getPixel(0,0) or getPixel(1,1)? My second question: if I need to sum these values, how can I keep an 'updater' variable in my for-loop? So for example, I have: W = getWidth(); H = getHeight(); for (i=0; i<=H-1; i++) { // Presuming that the origin is 0,0 // else I guess for (i=1; i<=H; i++)? for (i=0; i<=W; i++) { p = getPixel(W,H); b = (p-mean)^2; SOME UPDATER HERE TO A SUM OF b } } Once out of the loop, I can do things to the total. Cheers, Andy |
Dear all,
If the 'origin' of getPixel is (0,0), then in the following for-loop, what should the 'condition' be set to? My feeling is that as the 'initialization' is set to "0" that the 'condition' should be set to "j<H-1" - is this correct? Or should it be "j<H", "j<=H", etc? I've also tried in vain to replicate the result I get here with the Analyze -> Measure command (as previously suggested for speed), but have been unable to using various equations conatining the mean, standard deviation, area, etc!? Perhaps I am missing something trivial...? run("Set Measurements...", " mean standard redirect=None decimal=5"); run("Measure"); mean = getResult("Mean"); stdev = getResult("StdDev"); W = getWidth(); H = getHeight(); b = 0; for (j=0; j<H-1; j++) { for (i=0; i<W-1; i++) { p = getPixel(i,j); t = (p-mean)^2; b += t; } } normVar = b/(H*W*mean); Cheers, Andy |
HI,
>If the 'origin' of getPixel is (0,0), then in the following for-loop, >what should the 'condition' be set to? My feeling is that as the >'initialization' is set to "0" that the 'condition' should be set to >"j<H-1" - is this correct? Or should it be "j<H", "j<=H", etc? If H is the # of elements, and the index j starts from 0, it should be either j < H or j <= H-1, which are both equivalent!! (It is 100 elements going from 0 to 99 inclusive!) JW ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________ |
Free forum by Nabble | Edit this page |