How to Batch the Pictures and Export the List of Histogram Instead of Grayscale?

I have captured many pictures.So I want to convert these pictures into grayscale and quantify the change among these pictures by grayscale values. However I can’t export the list of histogram by the routine’ Process→Batch→Macro’.The following is ‘Macro’:

How will you quantify the change? Do you need the whole histogram or could you use the mean of an area?

Second, I’m having trouble understanding your preliminary code. It seems to make 5 ROIs, and copy 3 of them but doesn’t seem to do anything with the copied information. Then a histogram is done on the last ROI.

The macro code below works in the batch window to make a single rectangle ROI and save the histogram values in an XLS file. It’s based on the HistogramLister example. You need to update your path in the line where the file is saved. There are ways to get the path from the open image, but this is a quick-and-dirty solution.

By the way, if you are posting code here, it’s good to paste text rather than screenshots so other people can use it easily. In the forum you can use the </> tool in the editor to automatically format it as below.

Hope this helps.


// get histogram values
nBins = 256;
run("Clear Results");
row = 0;
getHistogram(values, counts, nBins);

// put histogram values in the results window and save
for (i=0; i<nBins; i++) {
  setResult("Value", row, values[i]);
  setResult("Count", row, counts[i]);

// close all windows

First at all,Thank you very much!
I want to handle the pictures which I got in oil displacement experiments by the software.If I could get grayscale values of these picture ,It meas I knew how much oil was displaced and the residual oil saturation. Because the oil is blak and the oil displacement agent is colourless.So I can get oil recovery.
By the way , the code you upload is macro language or other language, I know little of programming,so it confused me.

1 Like

Thank you very much ! I get it.

1 Like

Glad it helped. I asked about the measurements because you may be able to simplify the detection of black pixels (oil). I’m still not sure how you are calculating oil recovery from the histogram, but if you’re interested you could look into the tutorials and user guides on on thresholding and measurements. For example, you could set an intensity threshold to distinguish oil from the displacement agent. Then measure the total area of thresholded pixels. That gives you one number per image instead of a whole histogram.
Just something to think about.

1 Like

Hello,tswayne! With regard to the calculation of oil recovery, I need a curve that can reflect the changing trends.So I need to handle lots of pictures and gain their grayscale values.Then import these grayscale values in an EXCEL and calculate oil recovery by a model formula. Just as you said, the setting of the threshold is crucial for calculation.
However, solve a question,there are more! Run the macro and get many txt files which contain three columns( “count”,“value” and “sin Nombre”) in each one, but it’s just “count” meaningful for me. I have a software that can import multiple txt files into EXCEL, but it import all numbers of txt file into a column of EXCEL, no matter how many columns the txt file contains. So I want to know whether the macro can only output “count”.
Thanks again,God Bless You!

The nameless column (“sin Nombre”) is most likely the row number of the Results table. You can suppress that with Edit > Options > Input/Output and uncheck Save Row Numbers (see screenshot).

To suppress the “Values” column, just delete the line that writes those numbers in the table:

setResult("Value", row, values[i]);

Then your Excel file will have just the “Counts” column.


1 Like