macroshistogramimagejimagej-macro

ImageJ getHistogram function output


I'm trying to understand why the output of the getHistogram function is different from what is displayed when I click on the List button at the bottom of the histogram window.

For testing, I created a simple jpeg image in paint - it has a white background (500px x 500px) with a 20x20 black rectangle in the upper left corner.

Here is my script:

////
compiledDataFile=File.open("C:/test/testfile.txt");

open("C:/Users/dnewman/OneDrive - Innovative Micro Technology/Pictures/white and black.jpg");
run("8-bit");
setAutoThreshold("Default dark no-reset");
run("Threshold...");
setThreshold(0, 150);
run("Convert to Mask");
run("Histogram");
getHistogram(values, counts, 256);

Array.print(counts)
////

When I click on the list button at the bottom of the histogram window, it shows as I would expect - 249600 pixels in the 0 size category, and 400 pixels in the 255 size category.

But the "counts" array shows much different values - a majority of the numbers are 10, counts[0] = 944, counts[255] = 67358, and then there are some oddball numbers in the remaining spots where there are no 10's.

The values array outputs as I would expect; 0 - 255.

What am I doing wrong?


Solution

  • In your script, you call run("Histogram") before calling getHistogram()

    When run("Histogram") is called, it makes a small window containing the histogram, some text and an LUT display. Your script then gets the histogram of this window and not your image. I suspect the 10 values probably come from the LUT, which seems about 10 pixels high.

    To get values of the histogram of your image, you'd need to select the intended window after making the histogram GUI, using something like:

    win = getTitle();
    run("Histogram");
    selectWindow(win);
    getHistogram(values, counts, 256);
    
    Array.print(counts)