Extracting pixel values to results table
Posted by hjc on Jul 13, 2011; 1:15pm
URL: http://imagej.273.s1.nabble.com/Extracting-pixel-values-to-results-table-tp3683931.html
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");
}