Login  Register

Re: Plotting mean of an ROI and saving to excel

Posted by Michael Schmid on Jan 22, 2015; 8:45am
URL: http://imagej.273.s1.nabble.com/Plotting-mean-of-an-ROI-and-saving-to-excel-tp5011245p5011311.html

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