Login  Register

Macro to save histogram as IMAGE

Posted by Donato Giovannelli on Jul 18, 2013; 6:31pm
URL: http://imagej.273.s1.nabble.com/Macro-to-save-histogram-as-IMAGE-tp5004027.html

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