Login  Register

Re: Extracting pixel values to results table

Posted by Rasband, Wayne (NIH/NIMH) [E] on Jul 14, 2011; 2:02am
URL: http://imagej.273.s1.nabble.com/Extracting-pixel-values-to-results-table-tp3683931p3683932.html

On Jul 13, 2011, at 9:15 AM, hjc wrote:

> Hi
>
> I am trying to calculate NDVI (normalise difference vegetation index) values
> for every pixel in a series of images in a specified folder.  I am using the
> macro below.  The macro runs but I cannot get it to save the values of each
> pixel in the results table.  When the macro is finished only the last pixel
> value is shown in the results table.  The NDVI values are then also not
> computed correctly always stating 0.

The results table contains only one row because the 'i' variable (row index) in the setResult() calls is never incremented. Here is a simplified version of the macro that does work for a single RGB image. You will need to upgrade to the ImageJ 1.45l daily build to avoid a bug that causes the headings to not be saved with non-displayed results tables.

-wayne

     w = getWidth();
     h = getHeight();
     run("Clear Results");
     i = 0;
     for (x=0; x<w; x++) {
         showProgress(x, w);
         for (y=0; y<h; y++) {  
            v = getPixel(x, y);
            r = (v & 0xff0000)>>16;
            g = (v & 0x00ff00)>>8;      
            b = (v & 0x0000ff);            
            NDVI = ((r-g)/(r+g));
            setResult("O", i, i);
            setResult("Pr", i, x+1);
            setResult("Pc", i, y+1);
            setResult("NDVI", i, NDVI);
            i++;
         }
     }
     //updateResults();
      saveAs("Results", getDirectory("home")+"ndvi.txt");
      run("Clear Results");


> I have been able to write the results to the log screen, however this takes
> extremely long to process and is not feasible.  Simularly if the results is
> printed the macro takes extremely long.
>
> I would appreciate any help.
>
> Thanks
> Henri
>
> //extract surface area and ndvi over the whole plot for trees and grasses
>
> run("Memory & Threads...", "maximum=1600 parallel=8 run");
>
> dir1 = getDirectory("Choose Source Directory "); //prompt user for source
> directory
>
> dir2 = getDirectory("Choose Destination Directory ");  //prompt user for
> destination directory
>
> dir3 = getDirectory("Choose Results Directory ");  //promt user for
> destination directory for results file
>
> dir4 = getDirectory("Choose NDVI Results Directory ") //promt for ndvi dir
>
> list2 = getFileList(dir2); //get a list of the files  
>
> setBatchMode(true);
>
> for (z=0; z&lt;list2.length; z++) {
>      open(dir2+list2[z]);               //opens every image
>      //showProgress(z+1, list2.length);  
>
>      start = getTime();                                                  
> // Get current time for progress bar
>
>      w = getWidth();             // Get picture width
>      h = getHeight();            // Get picture height
>      tt = getTitle();
>      if(nResults&gt;=0)run("Clear Results");
>      print("\\Clear");           // Clears the log
>      i = nResults;
>
>
>      for (x=0; x&lt;w; x++){                        // Setup nested loops
> to go through each x,y value in image
>
>           showProgress(x+1, w);
>
>           for (y=0; y&lt;h; y++){  
>           v = getPixel(x, y);                    // Get a pixel value
>           r = (v &amp; 0xff0000)&gt;>16;                // Extract out RGB
> values from packed integer with pixel value
>           g = (v & 0x00ff00)>>8;      
>           b = (v & 0x0000ff);            
>           NDVI = ((r-g)/(r+g));                  // Calculate the NDVI.
>
>
>           //print(u,tt,x+1,y+1,r,g,b,NDVI);
>
>           setResult("O", i, i);
>           setResult("Pr", i, x+1);
>           setResult("Pc", i, y+1);
>           setResult("NDVI", i, NDVI);
>           //updateResults();
>           //print(nResults);
>
>           }
>
>        }
>
>    //selectWindow("Log");
>    //saveAs("Text", dir4+tt+"datandvi.txt");
>    saveAs("Results", dir4+tt+"datandvi.txt");
>  }
>
>
>
> --
> View this message in context: http://imagej.588099.n2.nabble.com/Extracting-pixel-values-to-results-table-tp6578961p6578961.html
> Sent from the ImageJ mailing list archive at Nabble.com.