Saving histogram results in fiji

Hello,

I was writing a macro in fiji using python to obtain a histogram of an image. I used the record plug in to obtain the exact function used. Weirdly, I can save cropped images using IJ.saveAs(), but for csv files it doesn’t save even when I use the same command shown in the record box for saving these results.

from __future__ import with_statement, division
from ij import IJ, WindowManager

IJ.open("pic" + st + ".PNG")
IJ.run("Histogram");
IJ.saveAs("Results", "C:/Users/nt/Desktop/BlackPigmentsPics/Histogram.csv");

Thanks.

Hello, welcome to the forum !

Well it seems that to get the table from the histogram you click the List button of the histogram window, which is not recorded by the recorder unfortunately. Therefore executing the recorded macro does not work because there is no result table.

I can propose half of the solution : getting the histogram values in a list, and then you can probably use the built-in file writing option of python to save it as a csv.

Here it is

#@ ImagePlus imp
ImProc  = imp.getProcessor()
ListBin = ImProc.getHistogram()

ListBin contains the count of pixels, for each bin (0 to 255). The bins index are not stored explicitly but the list is ordered so the number of pixel of value X is simply the value of the item at position X in ListBin.
Hope this helps !

Thank you.

can you provide documentation for
.getProcessor()
.getHistogram()
What arguments does .getProcessor() function take?

Also, I can’t find the functions that ij package offers. Can you provide me a link?

Best

What is “imp” defined a?

Are you using imagej module and not jython? I am using the fiji script service and not something like jupyter notebook

See the ImagePlus#getProcessor() javadoc.

See the ImageProcessor#getHistogram() javadoc.

I assume you mean the ij.IJ class (with capital letters).

The first line in the code @LThomas posted (#@ ImagePlus imp) is a script parameter, such that imp is automatically populated with the current image by the SciJava scripting framework.

Hope these links help getting you started.