How to save log files statistics using macro? [help]

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

How to save log files statistics using macro? [help]

solowunty
I am new to using Image J and I wish to create a macro which can save image data from the log for a set of images. I do not really have much of a clue of what I am doing, so my code is below:

        //Get histogram; ~should be 32 bins, not 256
               //TODO: Change to allow for 32 bins
        getHistogram (values , counts , 256) ;
       
        for(i=0; i< values . length ; i++)

        //Print to the log
        print ( values [i], counts [i]);

        //Open file
        f = File.open("C:/Users/user/Desktop/new.txt");

        //Select log window
        selectWindow("Log");

        //Get content from Log Window + Print to file
        content = getInfo();
  print(f,content);

        //Close file
  File.close(f);

This process automates everything for me which is what I want, however I do not think I am getting the correct information into the final text file, also the data is messy and I do not know how to tidy this up so that everything can be displayed neatly for further use.

Also, I would like to work with 32 bins for the histogram, however whenever I change the bin number to anything below 256 an error occurs.

Any hints or tips will be greatly appreciated!

Will.
Reply | Threaded
Open this post in threaded view
|

Re: How to save log files statistics using macro? [help]

Rasband, Wayne (NIH/NIMH) [E]
On Aug 9, 2012, at 7:20 AM, solowunty wrote:

> I am new to using Image J and I wish to create a macro which can save image
> data from the log for a set of images. I do not really have much of a clue
> of what I am doing, so my code is below:
>
> //Get histogram; ~should be 32 bins, not 256
>               //TODO: Change to allow for 32 bins
> getHistogram (values , counts , 256) ;
>
> for(i=0; i< values . length ; i++)
>
> //Print to the log
> print ( values [i], counts [i]);
>
> //Open file
> f = File.open("C:/Users/user/Desktop/new.txt");
>
> //Select log window
> selectWindow("Log");
>
> //Get content from Log Window + Print to file
> content = getInfo();
> print(f,content);
>
> //Close file
> File.close(f);
>
> This process automates everything for me which is what I want, however I do
> not think I am getting the correct information into the final text file,
> also the data is messy and I do not know how to tidy this up so that
> everything can be displayed neatly for further use.
>
> *Also, *I would like to work with 32 bins for the histogram, however
> whenever I change the bin number to anything below 256 an error occurs.

A better way to do this is to write the values and counts to the Results window and then save it. Here is an example :

  if (bitDepth==8) run("16-bit");
  getHistogram (values, counts, 32);
  run("Clear Results");
  for(i=0; i< values.length; i++) {
     setResult("Bin Start", i, values[i]);
     setResult("Count", i, counts[i]);
  }
  updateResults;
  path = getDirectory("home") + "counts.txt";
  saveAs("Results", path);

It converts 8-bit images to 16-bits because the getHistogram() macro function requires that the number of bins to be 256 with 8-bit images.

-wayne

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

Re: How to save log files statistics using macro? [help]

solowunty
Thank you for that, it is very helpful to me. One thing which I am trying to work on now is using this with a folder of images in the macro so that it can work through multiple files rather than having to load in each image individually so that data can be stored for multiple images.

Currently I can get the data for each image however this is all put into individual files, which is time consuming to go through every file to put the data into a single file. Is there a simple way in which I can run the macro on a group of images and have the data for each image be put into the same file?

Cheers. Will.

Reply | Threaded
Open this post in threaded view
|

Re: How to save log files statistics using macro? [help]

Rasband, Wayne (NIH/NIMH) [E]
On Aug 12, 2012, at 8:00 AM, solowunty wrote:

> Thank you for that, it is very helpful to me. One thing which I am trying to
> work on now is using this with a folder of images in the macro so that it
> can work through multiple files rather than having to load in each image
> individually so that data can be stored for multiple images.
>
> Currently I can get the data for each image however this is all put into
> individual files, which is time consuming to go through every file to put
> the data into a single file. Is there a simple way in which I can run the
> macro on a group of images and have the data for each image be put into the
> same file?

If the images are all the same size, open them as a virtual stack and run this macro:

  run("Clear Results");
  for (img=1; img<=nSlices; img++) {
     setSlice(img);
     getHistogram(values, counts, 32);
     for(i=0; i< values.length; i++) {
        setResult("Image", i, img);
        setResult("Bin Start", i, values[i]);
        setResult("Count", i, counts[i]);
      }
  }
  updateResults;
  path = getDirectory("home") + "counts.txt";
  saveAs("Results", path);

You can open a folder of images as a virtual stack by dragging it onto the "ImageJ" window and checking "Use virtual stack" in the dialog. With 8-bit images, this macro requires the 1.47b daily build, which fixes a bug that caused the getHistogram() macro function to not work if the number of bins was not 256.

Use this macro if the images are not all the same size:

  dir = getDirectory("Choose a Directory ");
  list = getFileList(dir);
  run("Clear Results");
  setBatchMode(true);
  row = 0;
  for (i=0; i<list.length; i++) {
     showProgress(i, list.length);
     open(dir+list[i]);
     getHistogram(values, counts, 32);
     for(bin=0; bin< values.length; bin++) {
        setResult("Label", row, list[i]);
        setResult("Bin", row, bin+1);
        setResult("Bin Start", row, values[bin]);
        setResult("Count", row, counts[bin]);
        row++;
      }
      close;
  }
  setOption("ShowRowNumbers", false);
  updateResults;
  path = getDirectory("home") + "counts.txt";
  saveAs("Results", path);

-wayne

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

Re: How to save log files statistics using macro? [help]

solowunty
Cheers, thank you so much for this assistance Wayne, you have been brilliant!

One last question, I am working with an Excel file for which I am putting the data into, is there any way that ImageJ can recognise a new column in the Excel file? This isn't really urgent as I am dealing with a small number of images at the moment, however I can imagine moving onto a larger set of images that it would be efficient to have the data all moved into their own columns?

Will.
Reply | Threaded
Open this post in threaded view
|

Re: How to save log files statistics using macro? [help]

Michael Schmid
Hi Will,

so you want to have one data column for each image file?
You can easily modify Wayne's macro:

  dir = getDirectory("Choose a Directory ");
  list = getFileList(dir);
  run("Clear Results");
  setBatchMode(true);
  row = 0;
  for (i=0; i<list.length; i++) {
     showProgress(i, list.length);
     open(dir+list[i]);
     getHistogram(values, counts, 32, 0, 255);
     for(bin=0; bin< values.length; bin++) {
        //setResult("Label", row, list[i]);
        row = bin;
        if (i==0) {
           setResult("Bin", row, bin+1);
           setResult("Bin Start", row, values[bin]);
        }
        setResult(list[i], row, counts[bin]);
        // alternatively:
        //setResult(File.nameWithoutExtension, row, counts[bin]);
        row++;
      }
      close;
  }
  setOption("ShowRowNumbers", false);
  updateResults;
  path = getDirectory("home") + "counts.txt";
  saveAs("Results", path);


I have not tried; there might be bugs in it.

Michael
________________________________________________________________
On Aug 13, 2012, at 22:34, solowunty wrote:

> Cheers, thank you so much for this assistance Wayne, you have been brilliant!
>
> One last question, I am working with an Excel file for which I am putting
> the data into, is there any way that ImageJ can recognise a new column in
> the Excel file? This isn't really urgent as I am dealing with a small number
> of images at the moment, however I can imagine moving onto a larger set of
> images that it would be efficient to have the data all moved into their own
> columns?
>
> Will.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html