Macro to save histogram as IMAGE

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Macro to save histogram as IMAGE

Donato Giovannelli
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
Reply | Threaded
Open this post in threaded view
|

Re: Macro to save histogram as IMAGE

Michael Schmid
Hi Don,

two points, not sure whether they solve your problem:

(a) For Auto Threshold by the default method, I'd suggest to use the macro call
  setAutoThreshold("Default") or
  setAutoThreshold("Default dark")
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.


Michael
________________________________________________________________
On Jul 18, 2013, at 20:31, Donato Giovannelli wrote:

> 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

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro to save histogram as IMAGE

tarika
In reply to this post by Donato Giovannelli
Hi Don!

I think you must have figured out how to save the plot by now. If not, it's really simple! Just go to File-> Save As and select the format you want to save it in. If it doesn't work for what plot you get the first time as results, duplicate the plot like mentioned in the previous post and then try this. It should work in the first case itself but I am not sure since this is what I did.

Good luck!