Extracting pixel values to results table

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
hjc
Reply | Threaded
Open this post in threaded view
|

Extracting pixel values to results table

hjc
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.

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<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>=0)run("Clear Results");
      print("\\Clear");           // Clears the log
      i = nResults;
     
         
      for (x=0; x<w; x++){                        // Setup nested loops to go through each x,y value in image
           
           showProgress(x+1, w);
           
           for (y=0; y<h; y++){  
           v = getPixel(x, y);                    // Get a pixel value
           r = (v & 0xff0000)>>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");
  }

Reply | Threaded
Open this post in threaded view
|

Re: Extracting pixel values to results table

Rasband, Wayne (NIH/NIMH) [E]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Extracting pixel values to results table

dschones
Hi There,

i'm trying a similar thing as Henri. I want to extract the pixel values of a
single, 8-bit grayscale picture to txt for further processing in a table. I
don't know anything about macros but running existing ones. Can somebody
help me on editing that one of Wayne?

I would use the "pixel inspection tool", if it where possible to make
individual selection of pixels, but that tool is minimized to a maximum
radius, which is too small for my purposes. Is it easy to set a bigger
radius (like 500pixels)?

Thanks, Jonas




--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Extracting pixel values to results table

Michael Schmid-3
Hi Jonas,

you can crop the image or duplicate the selection and save it as text image.

Michael
________________________________________________________________
On 08/01/2018 22:01, dschones wrote:

> Hi There,
>
> i'm trying a similar thing as Henri. I want to extract the pixel values of a
> single, 8-bit grayscale picture to txt for further processing in a table. I
> don't know anything about macros but running existing ones. Can somebody
> help me on editing that one of Wayne?
>
> I would use the "pixel inspection tool", if it where possible to make
> individual selection of pixels, but that tool is minimized to a maximum
> radius, which is too small for my purposes. Is it easy to set a bigger
> radius (like 500pixels)?
>
> Thanks, Jonas
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Extracting pixel values to results table

Wayne Rasband-2
In reply to this post by dschones
> On Jan 8, 2018, at 4:01 PM, dschones <[hidden email]> wrote:
>
> Hi There,
>
> i'm trying a similar thing as Henri. I want to extract the pixel values of a
> single, 8-bit grayscale picture to txt for further processing in a table. I
> don't know anything about macros but running existing ones. Can somebody
> help me on editing that one of Wayne?

The Image>Transform>Image to Results command extracts the pixel values of the active image or selection to the Results table.

-wayne

> I would use the "pixel inspection tool", if it where possible to make
> individual selection of pixels, but that tool is minimized to a maximum
> radius, which is too small for my purposes. Is it easy to set a bigger
> radius (like 500pixels)?
>
> Thanks, Jonas
>
> --
> Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Extracting pixel values to results table

dschones
works perfect! merci to all of you :)



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html