Loop for ROIs

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

Loop for ROIs

LP
Dear ImageJ community,

I'm trying to make a macro that from picture with multiple cells creates one file per cell based on a threshold.

However I don't know how to make a loop for the ROIs.
So far I have this:
______________________________________________
input="C:\\Users\\"
output=input+"zoutput\\"
list=getFileList(input);

for(i=0;i<list.length;i++){
    open(input+list[i]);
    name = File.nameWithoutExtension();
    setBatchMode(false);

selectWindow(name + ".tif");
run("Split Channels");
selectWindow("C2-" + name + ".tif");
run("Duplicate...", " ");
setAutoThreshold("Percentile dark");
run("Convert to Mask");
run("Analyze Particles...", "size=100-Infinity show=Outlines display exclude clear include add");

selectWindow("C1-" + name + ".tif");
roiManager("Select", <b>0);
run("Duplicate...", " ");
selectWindow("C2-" + name + ".tif");
roiManager("Select", 0);
run("Duplicate...", " ");
selectWindow("C3-" + name + ".tif");
roiManager("Select", 0);
run("Duplicate...", " ");

run("Merge Channels...", "c1=C1-"+name+"-1.tif c2=C2-"+name+"-2.tif c3=C3-"+name+"-1.tif create keep");
Stack.setDisplayMode("color");
roiManager("Select", 0);
run("Clear Outside", "stack");
saveAs("tiff", output + name + "0");

close();
selectWindow("C3-" + name + "-1.tif");
close();
selectWindow("C2-" + name + "-2.tif");
close();
selectWindow("C1-" + name + "-1.tif");
close();
selectWindow("Drawing of C2-" + name +"-1.tif");
close();
_____________________________________________________________


So I'd like to replace all the 0 by a variable.
I tried to use
i= roiManager("index");

But with no success.

Any suggestion and help will be very appreciated.
Thank you very much for your help in advance,
LP
Reply | Threaded
Open this post in threaded view
|

Re: Loop for ROIs

Brandon Hurr
Could you provide an image? I don't get the same names as you for the
channels so I can't replicate your process very easily.

More generally, it looks like you are splitting the image into 3 channels
(C1,C2,C3; I get (red), (green), (blue)), implementing a threshold in a
single channel to find your ROIs, copying those ROIs from each channel and
then merging those.

My suggestion would be something like this:

input="C:\\Users\\"
output=input+"zoutput\\" // your output folder needs to exist
list=getFileList(input);

setBatchMode(false); // set this once outside of the loop

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

// open file

open(input+list[i]);

// get window name

name = getTitle();

selectWindow(name);

// duplicate window for segmentation and finding ROIs

run("Duplicate...", "segment");

run("Split Channels");
setAutoThreshold("Percentile dark");
run("Convert to Mask");

// get ROIs (cells?)

run("Analyze Particles...", "size=100-Infinity show=Outlines display
exclude clear include add");

// close split channels

[ insert code here for that]

// select the original window again

selectWindow(name);

// loop through ROIs and save
for (m = 0; m < roiManager("count"); m++){

selectWindow(name);
roiManager("Select", m);

// duplicate ROI for saving
run("Duplicate...", "saveout");
run("Clear Outside");

// save out individual cell

saveAs("tiff", output + name + m);

run("Close"); // close saved window


} // end ROI loop

selectWindow(name);

run("Close"); // close original window

} // end image loop

// end macro

This way you only duplicate once from the original image for every ROI.
Less splitting and whatnot is always a good thing, because it's hard to
keep track of those things.

HTH,
B



On Sat, Jan 7, 2017 at 11:00 AM, LP <[hidden email]> wrote:

> Dear ImageJ community,
>
> I'm trying to make a macro that from picture with multiple cells creates
> one
> file per cell based on a threshold.
>
> However I don't know how to make a loop for the ROIs.
> So far I have this:
> ______________________________________________
> input="C:\\Users\\"
> output=input+"zoutput\\"
> list=getFileList(input);
>
> for(i=0;i<list.length;i++){
>     open(input+list[i]);
>     name = File.nameWithoutExtension();
>     setBatchMode(false);
>
> selectWindow(name + ".tif");
> run("Split Channels");
> selectWindow("C2-" + name + ".tif");
> run("Duplicate...", " ");
> setAutoThreshold("Percentile dark");
> run("Convert to Mask");
> run("Analyze Particles...", "size=100-Infinity show=Outlines
> display exclude clear include add");
>
> selectWindow("C1-" + name + ".tif");
> roiManager("Select", <b>0*);
> run("Duplicate...", " ");
> selectWindow("C2-" + name + ".tif");
> roiManager("Select", *0*);
> run("Duplicate...", " ");
> selectWindow("C3-" + name + ".tif");
> roiManager("Select", *0*);
> run("Duplicate...", " ");
>
> run("Merge Channels...", "c1=C1-"+name+"-1.tif c2=C2-"+name+"-2.tif
> c3=C3-"+name+"-1.tif create keep");
> Stack.setDisplayMode("color");
> roiManager("Select", *0*);
> run("Clear Outside", "stack");
> saveAs("tiff", output + name + "*0*");
>
> close();
> selectWindow("C3-" + name + "-1.tif");
> close();
> selectWindow("C2-" + name + "-2.tif");
> close();
> selectWindow("C1-" + name + "-1.tif");
> close();
> selectWindow("Drawing of C2-" + name +"-1.tif");
> close();
> _____________________________________________________________
>
>
> So I'd like to replace all the *0* by a variable.
> I tried to use
> i= roiManager("index");
>
> But with no success.
>
> Any suggestion and help will be very appreciated.
> Thank you very much for your help in advance,
> LP
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.
> com/Loop-for-ROIs-tp5017852.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
LP
Reply | Threaded
Open this post in threaded view
|

RE: Loop for ROIs

LP

Dear Brandon,


Thank you very much for your help. I couldn't send you a picture. The server kept sending me back the email.


The macro is now perfect. Thank you very very much.

There were a few issues but I managed to fix them.


The macro is now the following:


input="C:\\Users\\"
output=input+"zoutput\\"
list=getFileList(input);
setBatchMode(true);
for (i=0;i<list.length;i++) {
open(input+list[i]);
name = File.nameWithoutExtension();

selectWindow(name + ".tif");
// duplicate window for segmentation and finding ROIs
//run("Duplicate...", "segment");
run("Duplicate...", "duplicate");
run("Split Channels");
selectWindow("C2-" + name + "-1" + ".tif");
setAutoThreshold("Percentile dark");
run("Convert to Mask");
run("Analyze Particles...", "size=100-Infinity show=Outlines exclude clear include add");


selectWindow("C1-" + name + "-1" + ".tif");
close();
selectWindow("C2-" + name + "-1" + ".tif");
close();
selectWindow("C3-" + name + "-1" + ".tif");
close();
selectWindow("Drawing of C2-" + name+ "-1" + ".tif");
close();

// select the original window again
selectWindow(name + ".tif");
// loop through ROIs and save
for (m = 0; m < roiManager("count"); m++){
selectWindow(name + ".tif");
roiManager("Select", m);
// duplicate ROI for saving
run("Duplicate...", "duplicate");
//run("Duplicate...", "saveout");
//run("Crop");

saveAs("tiff", output + name + m);
run("Close");
} // end ROI loop
selectWindow(name + ".tif");
run("Close");
}

__________________________________________

Thank you so much,

LP





De : Brandon Hurr [via ImageJ] <ml-node+[hidden email]>
Envoyé : dimanche 8 janvier 2017 04:57
À : LP
Objet : Re: Loop for ROIs
 
Could you provide an image? I don't get the same names as you for the
channels so I can't replicate your process very easily.

More generally, it looks like you are splitting the image into 3 channels
(C1,C2,C3; I get (red), (green), (blue)), implementing a threshold in a
single channel to find your ROIs, copying those ROIs from each channel and
then merging those.

My suggestion would be something like this:

input="C:\\Users\\"
output=input+"zoutput\\" // your output folder needs to exist
list=getFileList(input);

setBatchMode(false); // set this once outside of the loop

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

// open file

open(input+list[i]);

// get window name

name = getTitle();

selectWindow(name);

// duplicate window for segmentation and finding ROIs

run("Duplicate...", "segment");

run("Split Channels");
setAutoThreshold("Percentile dark");
run("Convert to Mask");

// get ROIs (cells?)

run("Analyze Particles...", "size=100-Infinity show=Outlines display
exclude clear include add");

// close split channels

[ insert code here for that]

// select the original window again

selectWindow(name);

// loop through ROIs and save
for (m = 0; m < roiManager("count"); m++){

selectWindow(name);
roiManager("Select", m);

// duplicate ROI for saving
run("Duplicate...", "saveout");
run("Clear Outside");

// save out individual cell

saveAs("tiff", output + name + m);

run("Close"); // close saved window


} // end ROI loop

selectWindow(name);

run("Close"); // close original window

} // end image loop

// end macro

This way you only duplicate once from the original image for every ROI.
Less splitting and whatnot is always a good thing, because it's hard to
keep track of those things.

HTH,
B



On Sat, Jan 7, 2017 at 11:00 AM, LP <[hidden email]> wrote:

> Dear ImageJ community,
>
> I'm trying to make a macro that from picture with multiple cells creates
> one
> file per cell based on a threshold.
>
> However I don't know how to make a loop for the ROIs.
> So far I have this:
> ______________________________________________
> input="C:\\Users\\"
> output=input+"zoutput\\"
> list=getFileList(input);
>
> for(i=0;i<list.length;i++){
>     open(input+list[i]);
>     name = File.nameWithoutExtension();
>     setBatchMode(false);
>
> selectWindow(name + ".tif");
> run("Split Channels");
> selectWindow("C2-" + name + ".tif");
> run("Duplicate...", " ");
> setAutoThreshold("Percentile dark");
> run("Convert to Mask");
> run("Analyze Particles...", "size=100-Infinity show=Outlines
> display exclude clear include add");
>
> selectWindow("C1-" + name + ".tif");
> roiManager("Select", <b>0*);
> run("Duplicate...", " ");
> selectWindow("C2-" + name + ".tif");
> roiManager("Select", *0*);
> run("Duplicate...", " ");
> selectWindow("C3-" + name + ".tif");
> roiManager("Select", *0*);
> run("Duplicate...", " ");
>
> run("Merge Channels...", "c1=C1-"+name+"-1.tif c2=C2-"+name+"-2.tif
> c3=C3-"+name+"-1.tif create keep");
> Stack.setDisplayMode("color");
> roiManager("Select", *0*);
> run("Clear Outside", "stack");
> saveAs("tiff", output + name + "*0*");
>
> close();
> selectWindow("C3-" + name + "-1.tif");
> close();
> selectWindow("C2-" + name + "-2.tif");
> close();
> selectWindow("C1-" + name + "-1.tif");
> close();
> selectWindow("Drawing of C2-" + name +"-1.tif");
> close();
> _____________________________________________________________
>
>
> So I'd like to replace all the *0* by a variable.
> I tried to use
> i= roiManager("index");
>
> But with no success.
>
> Any suggestion and help will be very appreciated.
> Thank you very much for your help in advance,
> LP
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.
> com/Loop-for-ROIs-tp5017852.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



If you reply to this email, your message will be added to the discussion below:
http://imagej.1557.x6.nabble.com/Loop-for-ROIs-tp5017852p5017853.html
To unsubscribe from Loop for ROIs, click here.
NAML