Macro for batch saving of histogram results

fiji
imagej
macro

#1

Hello
I want to create a macro for batch operation on a folder of files. It should record the pixel counts of the binarised images (therefore I only need value = 0 or value = 255) and save them ideally all to one excel sheet with the filenames and the pixel counts in two columns. Here is my very messy macro which I hope can be easily polished!

dir1 = getDirectory("Choose Source Directory ");
dir2 = getDirectory("Choose Destination Directory ");
list = getFileList(dir1);
setBatchMode(true);
nBins = 256;

for (i=0; i<list.length; i++) {
 showProgress(i+1, list.length);
 open(dir1+list[i]);
 setOption("BlackBackground", false);
getHistogram(value, count, nBins);
run("Histogram");
setResult("Value", i, value[i]);
setResult("Count", i, count[i]);
updateResults();
run("Read and Write Excel");
 close();
}