http://imagej.273.s1.nabble.com/Macro-to-save-histogram-as-IMAGE-tp5004027p5004041.html
to specify whether the foreground is bright and dark.
Replace "Default" with a different autothresholding method if required.
If you still find problems with light vs. dark foreground, check whether the some images have an inverted LUT (lookup table) or not. This is noted in the heading line of an image window as '(inverting LUT)'.
Mixing images with inverting LUT and others with plain LUT can cause a lot of problems.
(b) You can use Plugins>Utilities>Capture Image to get the histogram window as displayed, including the text below. You can't retrieve the text from it with a program; you need your eyes to read it. If you want to have mean and std deviation for use as a number in your macro, use getStatistics(area, mean, min, max, std, histogram). For the mode, you have to loop through the histogram array and find at which index it has the largest value. For 16- and 32-bit images, you have to calculate the mode as min + (max-min)*i/256 where I is the index of the maximum.
> Dear ImageJ users,
>
> I'm writing a macro to automate some particle measurements.I'm running into
> two distinct problems, perhaps liked with my low experience with ImageJ and
> macro.
>
> Here is the code I'm using:
> -----
>
> macro "dispersion routine" {
> dir1 = getDirectory("Choose Source Directory");
> list = getFileList(dir1);
> for (i=0; i<list.length; i++) {
> open(dir1+list[i]);
> imgname = list[i];
> number=i+1;
> status=number/list.length*100;
> selectImage(imgname);
> run("Histogram");
> selectImage(imgname);
> run("8-bit");
> run("Enhance Contrast", "saturated=4 normalize");
> setAutoThreshold();
> run("Convert to Mask");
> run("Watershed");
> run("Set Scale...", "distance=24 known=1 pixel=1 unit=µm global");
> run("Set Measurements...", "area standard perimeter shape
> area_fraction redirect=None decimal=3");
> run("Analyze Particles...", "size=0.0002-Infinity
> circularity=0.00-1.00 show=[Overlay Outlines] exclude clear include
> summarize");
> close();
> }
>
> -----
> The two problems are:
>
> 1) With some of the picture (which consist of black particles on light
> gray/white background) the threshold is selecting the white area instead of
> black. I inserted the run("invert") trying to overcome the issue. But
> reality is that it appears to be random. There is perhaps an easy fix to
> this.
>
> 2) I like to save the Histogram as IMAGE and to retrieve the mean, standard
> deviation and mode. So far I've found this code:
>
> macro "Plot Histogram" {
> getStatistics(area, mean, min, max, std, histogram);
> if (bitDepth==8 || bitDepth==24)
> Plot.create("Histogram", "Value", "Count", histogram);
> else {
> values = newArray(256);
> value = min;
> binWidth = (max-min)/256;
> for (i=0; i<256; i++) {
> values[i] = value;
> value += binWidth;
> }
> Plot.create("Histogram", "Value", "Count", values, histogram,
> "title=histogram");
> }
> saveAs("PNG", "/home/dg/Desktop/Histogram.png");
> }
>
> But i'm not able to save the histogram, only the image. I tried with
> selectWindow("Histogram") but I incur into errors... And i'm not sure how I
> can nest this macro into the previous..
>
> I'd like also to save the thresholded image after the measure...
>
> Any help is appreciated.
>
> Thanks,
> Don
>
> --
> ImageJ mailing list:
http://imagej.nih.gov/ij/list.html