Help with macro iterating slices of a stack

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

Help with macro iterating slices of a stack

Pedro J CamelloDr Pedro J Camello
Hi all,

I´m stuck with a macro written to iterate on all the slices of a binary stack.
In each slice, the code below should run Analyze particles, place the particles in the ROI Manager and Measure all of them.
The problem is that, after the first iteration, once i=2 the code does not generate a set of ROIs in the ROI Manager (line 8).

I can´t understand why the Analyze particles command runs in the first instance and fails afterwards…

I don´t want the Analyze particles command to run all the slices because I want to Measure each slice separately (maybe I could arrange it using the labels column of the Results table, but I would prefer to avoid it)

Any help will be welcome

Code:
1  // Up to this point I have a binary stack with all the nuclei (watershed) ready to be counted;
2  for (i=1; i<nSlices+1;i++) {
3   Stack.setSlice(i);
4   Stack.getPosition(channel, slice, frame);
5   print("Slice number = "+slice); //Just to check the slice number
6  // waitForUser("Check i and slice", "i =  "+i+"  Slice  =  "+slice);
7   run("Set Measurements...", "area display redirect=None decimal=2");
8   run("Analyze Particles...", "size=150-Infinity pixel clear include add slice");
9  // waitForUser("Check rois OK", "Ther are   "+roiManager("count")+" ROIs");  
10   roiManager("multi-measure");
12   print("Slice num = " +slice+"Cells = "+roiManager("count")); roiManager("reset");
13  }

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

Re: Help with macro iterating slices of a stack

Jeremy Adler
Hi Pedro,
Line 10, multi measure
Uses all ROIs to measure all images in your stack.
 Delete it, Line 8 makes the measurements and generates the ROIs.

Line 8, analyse particles - the "clear" will empty the results window each in each iteration, so you delete the results from the previous slice.
 Try removing the "clear".

Line 7, set measurements does not need to be in the loop, run it before the loop.
 It doesn't do any harm in the loop, but is neater.

Best wishes


Jeremy Adler
BioVis

-----Original Message-----
From: ImageJ Interest Group <[hidden email]> On Behalf Of Pedro J Camello
Sent: Sunday, May 3, 2020 5:19 PM
To: [hidden email]
Subject: Help with macro iterating slices of a stack

Hi all,

I´m stuck with a macro written to iterate on all the slices of a binary stack.
In each slice, the code below should run Analyze particles, place the particles in the ROI Manager and Measure all of them.
The problem is that, after the first iteration, once i=2 the code does not generate a set of ROIs in the ROI Manager (line 8).

I can´t understand why the Analyze particles command runs in the first instance and fails afterwards…

I don´t want the Analyze particles command to run all the slices because I want to Measure each slice separately (maybe I could arrange it using the labels column of the Results table, but I would prefer to avoid it)

Any help will be welcome

Code:
1  // Up to this point I have a binary stack with all the nuclei (watershed) ready to be counted;
2  for (i=1; i<nSlices+1;i++) {
3  Stack.setSlice(i);
4  Stack.getPosition(channel, slice, frame);
5  print("Slice number = "+slice); //Just to check the slice number
6  //waitForUser("Check i and slice", "i =  "+i+"  Slice  =  "+slice);
7  run("Set Measurements...", "area display redirect=None decimal=2");
8  run("Analyze Particles...", "size=150-Infinity pixel clear include add slice");
9  //waitForUser("Check rois OK", "Ther are   "+roiManager("count")+" ROIs");
10  roiManager("multi-measure");
12  print("Slice num = " +slice+"Cells = "+roiManager("count")); roiManager("reset");
13  }

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








När du har kontakt med oss på Uppsala universitet med e-post så innebär det att vi behandlar dina personuppgifter. För att läsa mer om hur vi gör det kan du läsa här: http://www.uu.se/om-uu/dataskydd-personuppgifter/

E-mailing Uppsala University means that we will process your personal data. For more information on how this is performed, please read here: http://www.uu.se/en/about-uu/data-protection-policy

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

Re: Help with macro iterating slices of a stack

CARL Philippe (LBP)
In reply to this post by Pedro J CamelloDr Pedro J Camello
Hola Pedro,
Your code is way too complicated for things that can be done way more easily..
Indeed, within the "Analyze Particles..." tool you have already the possibility to go through the whole stack (with the option "stack") as well as measure the parameters set within "Set Measurements..." by activating the option "display".
The following macro demonstrates how easy and short your code could alternatively be:
        run("Bat Cochlea Volume");
        run("Set Measurements...", "area display redirect=None decimal=2");
        run("Analyze Particles...", "display clear include add stack");
Quod erat demonstrandum!
My best regards,
Philippe

Philippe CARL
Laboratoire de Bioimagerie et Pathologies
UMR 7021 CNRS - Université de Strasbourg
Faculté de Pharmacie
74 route du Rhin
67401 ILLKIRCH
Tel : +33(0)3 68 85 42 89

----- Le 3 Mai 20, à 17:18, Pedro J Camello [hidden email] a écrit :

Hi all,

I´m stuck with a macro written to iterate on all the slices of a binary stack.
In each slice, the code below should run Analyze particles, place the particles in the ROI Manager and Measure all of them.
The problem is that, after the first iteration, once i=2 the code does not generate a set of ROIs in the ROI Manager (line 8).

I can´t understand why the Analyze particles command runs in the first instance and fails afterwards…

I don´t want the Analyze particles command to run all the slices because I want to Measure each slice separately (maybe I could arrange it using the labels column of the Results table, but I would prefer to avoid it)

Any help will be welcome

Code:
1  // Up to this point I have a binary stack with all the nuclei (watershed) ready to be counted;
2  for (i=1; i<nSlices+1;i++) {
3   Stack.setSlice(i);
4   Stack.getPosition(channel, slice, frame);
5   print("Slice number = "+slice); //Just to check the slice number
6  // waitForUser("Check i and slice", "i =  "+i+"  Slice  =  "+slice);
7   run("Set Measurements...", "area display redirect=None decimal=2");
8   run("Analyze Particles...", "size=150-Infinity pixel clear include add slice");
9  // waitForUser("Check rois OK", "Ther are   "+roiManager("count")+" ROIs");  
10   roiManager("multi-measure");
12   print("Slice num = " +slice+"Cells = "+roiManager("count")); roiManager("reset");
13  }

--
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: Help with macro iterating slices of a stack

Pedro J CamelloDr Pedro J Camello
In reply to this post by Pedro J CamelloDr Pedro J Camello
Hi Jeremy,

in my setup the multimeasure  operatrs only on the slice. I have discovered that Analyze particles has troubles operating on Stacks. The solution was to make a copy of each slice  or making a 1 slice substack before using it.

Thanks a lot for your help

Pedro

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

Re: Help with macro iterating slices of a stack

Pedro J CamelloDr Pedro J Camello
In reply to this post by Pedro J CamelloDr Pedro J Camello
Dear Philippe,

You are right that a simpler way is to apply the Analyze particles command to the stack, but at the moment I prefer to get separated ROIs and Results for each slice (the rest of the macro will generate a text file for each slice).

Thanks for your help

Pedro

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

Re: Help with macro iterating slices of a stack

gnelson
In reply to this post by Pedro J CamelloDr Pedro J Camello
Hi Pedro,

Spookily I was doing something very similar on Friday in the same manner as Philippe suggests.  My stack ended up with about 40,000 rows of objects found over my timeseries.  In these cases, R is your friend, not spreadsheets ( https://cran.r-project.org ).  I wrote this code (and learned a bit about how easy regression curves are to do with ggplot2 too) as a simple r script to start analysis based on mean area  covered per timepoint- super easy and very quick (and you can see I added other calculations per timepoint into the new data table- you could add more- see https://www.rdocumentation.org/packages/dplyr/versions/0.7.8/topics/summarise ):

*Summarise data for multiple objects per image slice in r*
library(dplyr)

library(ggplot2)
mika <-read.csv("/Users/glyn_nelson/Documents/mikolaj_noLargeVesicles.csv", header=T, sep="\t")

mika_mean <-mika %>%
  group_by(slice) %>%
  summarise(tot=sum(Area), avg=mean(Area), SD=sd(Area), count=n()) %>%
  mutate(total_calc=avg*count)

ggplot(mika_mean, aes(x=slice, y=avg)) +
  geom_point(color='#2980B9', size = 4) +
  geom_smooth(method=lm, color='#2C3E50')

--
Apologies, almost off topic, but it saves so much time compared to messing with spreadhseets with so much data!

Glyn.

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

Re: Help with macro iterating slices of a stack

Krs5
In reply to this post by Pedro J CamelloDr Pedro J Camello
Dear Pedro,

Don't think that is really necessary. If you would add  run("Select None");    after roiManager("reset"); in your original macro, I think all will work.

Best wishes

Kees

________________________________


Hi Jeremy,

in my setup the multimeasure  operatrs only on the slice. I have discovered that Analyze particles has troubles operating on Stacks. The solution was to make a copy of each slice  or making a 1 slice substack before using it.

Thanks a lot for your help

Pedro

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=02%7C01%7Ckrs5%40leicester.ac.uk%7C3b801b5ea58448a6246908d7f00efbb3%7Caebecd6a31d44b0195ce8274afe853d9%7C0%7C1%7C637241819455295043&amp;sdata=8ND85ZYZFE85B9Nkm8cmaaFkfTGaGYiZtEREDrc%2BIHs%3D&amp;reserved=0

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

Re: Help with macro iterating slices of a stack

CARL Philippe (LBP)
In reply to this post by Pedro J CamelloDr Pedro J Camello
Dear Pedro,
As I already pointed out, there is either the easy way:
        run("Bat Cochlea Volume");
        setOption("BlackBackground", true);
        run("Set Measurements...", "area display redirect=None decimal=2");
        run("Analyze Particles...", "display clear include summarize add stack");
or the hard way [Quentin Tarantino even said: “There are two ways: my way and the highway.”]
        run("Bat Cochlea Volume");
        setOption("BlackBackground", true);
        run("Set Measurements...", "area display redirect=None decimal=2");
        run("Analyze Particles...", "clear include add stack");
// roiManager("Show None");
        for(i = 0; i != roiManager("count"); i++)
        {
                roiManager("Select", i);
                roiManager("Measure");
        }
Both will give you exactly the same results!
Now if "the rest of the macro will" or needs to "generate a text file for each slice" you can of course add a "call("ij.plugin.frame.RoiManager.getName", index)" inside of the "hard way" "for" loop and work it out from there.
I would alternatively rather use the "easy way" version of the code and then "generate a text file for each slice" by using the getResultString("Column", row) and/or getResult("Column", row) methods of the obtained results table.
From my point of view, this solution is way easier, shorter, gracefuler, cooler,... but in the same time all is relative...
My best regards,
Philippe

Philippe CARL
Laboratoire de Bioimagerie et Pathologies
UMR 7021 CNRS - Université de Strasbourg
Faculté de Pharmacie
74 route du Rhin
67401 ILLKIRCH
Tel : +33(0)3 68 85 42 89

----- Le 4 Mai 20, à 1:38, Pedro J Camello [hidden email] a écrit :

Dear Philippe,

You are right that a simpler way is to apply the Analyze particles command to the stack, but at the moment I prefer to get separated ROIs and Results for each slice (the rest of the macro will generate a text file for each slice).

Thanks for your help

Pedro

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

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