Plotting mean of an ROI and saving to excel

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

Plotting mean of an ROI and saving to excel

DanielatAmthorLab
Hi all, and Thank you for checking out my question!

I am trying to use ImageJ to collect some .tiff sequences from a directory apply ROIs (saved in a second directory) and finally save the Mean of the ROI for each frame of each .tiff sequence in a third directory as an excel readable file. I have the directory navigation down, and I have several methods for collecting the mean of the ROIs. The end result should be an excel file for each ROI, with the plots of each video inside.

What I can't do...

 ...(after about a week) is save the mean values for each .tiff sequence to a file that I can read with excel. I would like columns to be the individual videos and the rows to be the mean of the ROI in each frame. How do I do this?

This is the code I have so far. It is in the Imagej macro language.

//this macro takes the z plot of different ROIs for a directory of tiff videos
 if (nImages!=0){exit("Close all open images!");} 
 //Ask user to select input/output
 dir1 = getDirectory("Select input directory"); //tiff sequences
 dir2 = getDirectory("Select output directory"); //excel file for each ROI 
 dir3 = getDirectory("Select saved ROI directory"); //where the .roi files are

 inlist=getFileList(dir1);
 ROIlist=getFileList(dir3);
namlng=inlist.length;
savfnamstart=replace(inlist[0], ".tif", " ");//name creation for excel files

 //set ROI manager to measure the mean of the ROIs
 run("Set Measurements...", "  mean redirect=None decimal=3");
 run("ROI Manager...");
 //roiManager("Deselect");
 //roiManager("Delete"); //clear any ROIs in the manager

 for (R=0; R<ROIlist.length; R++){
 	roiManager("Open", dir3+ROIlist[R] );}//Open all ROIs to manager	
 	 	
 for (H=0; H<ROIlist.length; H++){
 	roiManager("Select", H);
 	thisROI=ROIlist[H];
 	ROInam=replace(thisROI, ".roi", "");//get a name for current ROI

 	f = File.open(dir2+savfnamstart+"To"+namlng+ROInam+".xls");
 	//create an excel file to save data to.

	for (i=0; i<inlist.length; i++) {

           //this is where i have trouble. after I have looked at each video, how do I save its means for the ROI?

	}	 
}
Reply | Threaded
Open this post in threaded view
|

Re: Plotting mean of an ROI and saving to excel

Michael Schmid
Hi Daniel,

here are some functions you are missing; see
  http://rsb.info.nih.gov/ij/developer/macro/functions.html
I have not checked the code below, just to point out some functions that
may be helpful...

textline = "";                       //no results in the line yet
for (i=0; i<inlist.length; i++) {
  if (endsWith(inlist[i], ".tif")) { //ignores non-tiff files
    open(dir1+inlist[i]);            //open the file
    roiManager("Select", H);         //select the roi in the image
    getStatistics(area, mean);       //measure the average in the roi
    textline = textline + mean + "\t";
    close();
  }
}
print(f, textline);

...and close the text file f at the very end.


Michael

_________________________________________________________________
On Fri, January 16, 2015 04:22, DanielatAmthorLab wrote:

> Hi all, and Thank you for checking out my question!
>
> I am trying to use ImageJ to collect some .tiff sequences from a directory
> apply ROIs (saved in a second directory) and finally save the Mean of the
> ROI for each frame of each .tiff sequence in a third directory as an excel
> readable file. I have the directory navigation down, and I have several
> methods for collecting the mean of the ROIs. The end result should be an
> excel file for each ROI, with the plots of each video inside.
>
> What I can't do...
>  ...(after about a week) is save the mean values for each .tiff sequence
> to
> a file that I can read with excel. I would like columns to be the
> individual
> videos and the rows to be the mean of the ROI in each frame. How do I do
> this?
>
> This is the code I have so far. It is in the Imagej macro language.
<code pasted here; it was visible only on the Nabble page, not part of the
email>

//this macro takes the z plot of different ROIs for a directory of tiff
videos
 if (nImages!=0){exit("Close all open images!");}
 //Ask user to select input/output
 dir1 = getDirectory("Select input directory"); //tiff sequences
 dir2 = getDirectory("Select output directory"); //excel file for each ROI
 dir3 = getDirectory("Select saved ROI directory"); //where the .roi files
are

 inlist=getFileList(dir1);
 ROIlist=getFileList(dir3);
namlng=inlist.length;
savfnamstart=replace(inlist[0], ".tif", " ");//name creation for excel files

 //set ROI manager to measure the mean of the ROIs
 run("Set Measurements...", "  mean redirect=None decimal=3");
 run("ROI Manager...");
 //roiManager("Deselect");
 //roiManager("Delete"); //clear any ROIs in the manager

 for (R=0; R<ROIlist.length; R++){
  roiManager("Open", dir3+ROIlist[R] );}//Open all ROIs to manager

 for (H=0; H<ROIlist.length; H++){
  roiManager("Select", H);
  thisROI=ROIlist[H];
  ROInam=replace(thisROI, ".roi", "");//get a name for current ROI

  f = File.open(dir2+savfnamstart+"To"+namlng+ROInam+".xls");
  //create an excel file to save data to.

        for (i=0; i<inlist.length; i++) {

           //this is where i have trouble. after I have looked at each
video, how do I save its means for the ROI?

        }
}

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

Re: Plotting mean of an ROI and saving to excel

Kay Schink
In reply to this post by DanielatAmthorLab
Hi Daniel,

From your post I am not sure if you want to have one measurement per ROI (e.g each frame is an Excel file with a single measurement)  or multiple measurements (e.g one file contails the results of a ROI over the whole length of videos).
It would be easier if you could share the expected output format.

As for the postprocessing part:
Have you considered to simply save the output from your mean measurements as individual CSV files and do the postprocessing not in ImageJ, but rather in another language, e.g. Python?
I use the Python "pandas" package for this kind of task - e.g. gather all measured CSV files from a directory, extract a column with measurements from all of them, and then export them in an Excel-readable format.
If you want this kind of post-processing, I can share a short script - in this case an example of your desired output format would be crucial.

Best wishes

Kay
Reply | Threaded
Open this post in threaded view
|

Re: Plotting mean of an ROI and saving to excel

DanielatAmthorLab

Michael:

I tried some of that code you wrote, and it partially did what I needed it to.
Here are the changes I made.

for (H=0; H<ROIlist.length; H++){
 	//roiManager("Select", H);
 	thisROI=ROIlist[H];
 	ROInam=replace(thisROI, ".roi", "");//get a name for current ROI
	//create an excel file to save data to.
 	f = File.open(dir2+savfnamstart+"To"+namlng+ROInam+".xls");
	textline="";
	for (i=0; i<inlist.length; i++) {
		open(dir1+inlist[i]);
		roiManager("select", H);
		getStatistics(area, mean);
		textline=textline + mean + "\t";
		close();
	}
	print(f,textline);
	File.close(f);	 
}



Kay

I would like to be able to do it all in one script, especially in one imagej macro script. (because I have almost no python ability whatsoever. :/  )

with those adjustments above, it will write the mean value of the selected ROI into a txt / excel file, but it only does it for what seems to be the first frame of each video. What I need is a file for each ROI. Each one of those files will have the mean of every frame of the videos. So if I ran the script on 3 videos and used 2 ROIs I would end up with two excel files (one for each ROI). If I opened one of those excel files I would have 3 columns with the mean of each frame of that video (column1 has values of video1, column2 has video 2, etc.).


Thank you both so much for your help! I hope I did not take too long in getting back.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting mean of an ROI and saving to excel

Michael Schmid
Hi Kay,

the frames of a video become slices of an ImageStack in ImageJ, numbered from 1 to nSlices(). [Note that this is different from most other counters, which run from 0 to N-1]
If you want to get the values for each frame/slice, just have an additional loop for the stack slices. Roughly like this:

for (H=0; H<ROIlist.length; H++){
        //roiManager("Select", H);
        thisROI=ROIlist[H];
        ROInam=replace(thisROI, ".roi", "");//get a name for current ROI
        //create an excel file to save data to.
        f = File.open(dir2+savfnamstart+"To"+namlng+ROInam+".xls");
        open(dir1+inlist[0]); //just to read the number of slices
        n = NSlices();  // assumes that the number of slices is the same for all files
        close();
        for (s=1; s<=nSlices(); s++) {
                textline="";
                for (i=0; i<inlist.length; i++) {
                        open(dir1+inlist[i], s); //assumes tiff files
                        roiManager("select", H);
                        getStatistics(area, mean);
                        textline=textline + mean + "\t";
                        close();
                }
                print(f,textline);
                File.close(f);
        }
}

If your files are .AVI files, open them as virtual stack (use Plugins>Macros>Record to see how this is done in a macro) and use setSlice(s).

If it is too slow for you, reverse the two inner loops: Have the slices loop as the inner loop, and the 'i' (file counter) loop as middle loop. Then you have to open each file only once. But you cannot write directly to the text file, but you need an array of textlines (one for each slice) and you have to append to the correct one. When finished with a roi, have another loop to write all text lines to the text file.


Michael
________________________________________________________________
On Jan 21, 2015, at 23:20, DanielatAmthorLab wrote:

> Michael:
>
> I tried some of that code you wrote, and it partially did what I needed it
> to.
> Here are the changes I made.
>
>
>
>
>
> Kay
>
> I would like to be able to do it all in one script, especially in one imagej
> macro script. (because I have almost no python ability whatsoever. :/  )
>
> with those adjustments above, it will write the mean value of the selected
> ROI into a txt / excel file, but it only does it for what seems to be the
> first frame of each video. What I need is a file for each ROI. Each one of
> those files will have the mean of every frame of the videos. So if I ran the
> script on 3 videos and used 2 ROIs I would end up with two excel files (one
> for each ROI). If I opened one of those excel files I would have 3 columns
> with the mean of each frame of that video (column1 has values of video1,
> column2 has video 2, etc.).
>
>
> Thank you both so much for your help! I hope I did not take too long in
> getting back.
>
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/Plotting-mean-of-an-ROI-and-saving-to-excel-tp5011245p5011308.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

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