ImageJ: How to measure multiple ROI in Batch Process using Macro and save each ROI's result in individual measurement table

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

ImageJ: How to measure multiple ROI in Batch Process using Macro and save each ROI's result in individual measurement table

Arreis
This post was updated on .
I have 1000 images in a folder. Each images have four ROIs labelled A, B ,C and D and I wanted to measure it's grey value (min, max and mean). Is it possible to run a Macro from Batch Process that can measure each ROI and return four individual ROI results like a set of measurements for A in one table then a set of measurements for B in another table and so on.

I tried the following Macro to analyze ROI labelled A then automatically save it to A.xls.

run("8-bit");
run("Set Measurements...", "area mean standard min bounding area_fraction stack display redirect=None decimal=3");
makeRectangle(0, 0, 356, 301);
run("Measure");
dir = getDirectory("image");
index = lastIndexOf(dir, ".");
name = "A" + ".xls";
saveAs("Measurements", dir+name);

Naively , I copied the same Macro and changed the makeRectangle value for ROI B then paste it on the next line hoping that it measures the ROI and save it to B.xls. Now it looks like this:

run("8-bit");
run("Set Measurements...", "area mean standard min bounding area_fraction stack display redirect=None decimal=3");
makeRectangle(0, 0, 356, 301);
run("Measure");
dir = getDirectory("image");
index = lastIndexOf(dir, ".");
name = "A" + ".xls";
saveAs("Measurements", dir+name);

run("8-bit");
run("Set Measurements...", "area mean standard min bounding area_fraction stack display redirect=None decimal=3");
makeRectangle(410, 0, 310, 246);
run("Measure");
dir = getDirectory("image");
index = lastIndexOf(dir, ".");
name = "B" + ".xls";
saveAs("Measurements", dir+name);
close();

It does save the B.xls but it contains ROI A measurements as well which means the result is a combination of region A and B. I need A.xls to have ROI A measurements only and B.xls to contain ROI B only. Any help is appreciated. Thank you darlings.
- Love, Arreis
Reply | Threaded
Open this post in threaded view
|

Re: ImageJ: How to measure multiple ROI in Batch Process using Macro and save each ROI's result in individual measurement table

gankaku
Hi Arreis,

the following macro should do what you need and gives you the freedom for
every folder to decide how many ROIs you want to image in all the images
contained in this folder.
You do not need to run it under >Process >Batch (that won't work). The
macro does its own batch processing (see the loops) with initial ROI
specification. The latter is done on the very first image in the chosen
folder. You should only make sure, that there is nothing else in the folder
but only your images you want to analyze.

Just drag it in ImageJ or Fiji and hit the run button or start it under the
macro editor run menu.

Hope it helps (feedback welcome :-) )

Kind regards,
Jan


//-----------------macro start


dir = getDirectory("Choose a directory");
fileList = getFileList(dir);
label =
newArray("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
open(dir + fileList[0]);

selections = getNumber("how many selections do you want to create?", 4);
roiManager("Reset");
for(s=1; s<=selections; s++) {
waitForUser("Create Selection " + s);
run("Add to Manager");
}

close(fileList[0]);


setBatchMode(true);
for(i=0; i<fileList.length; i++) {
open(dir + fileList[i]);
run("8-bit");
run("Set Measurements...", "area mean standard min bounding area_fraction
stack display redirect=None decimal=3");
for(r=0; r<selections; r++) {
if(i>0) {
IJ.renameResults(label[r], "Results");  //runs only after frist run when
there are already results tables open
}
selectWindow(fileList[i]);
roiManager("Select", r);
roiManager("Measure");
saveAs("Results", dir + label[r] + ".xls" ); //.tsv might even be a better
file extension to handle it in Excel later
IJ.renameResults("Results", label[r]);  //enables switching between the
tables since there can only be handled 1 active "Results" table in IJ
}
close(fileList[i]);
}

setBatchMode(false);


//-----------------------macro end

2015-10-12 21:34 GMT+02:00 Arreis <[hidden email]>:

> I have 1000 images in a folder. Each images have four ROIs labelled A, B ,C
> and D and I wanted to measure it's grey value (min, max and mean). Is it
> possible to run a Macro from Batch Process that can measure each ROI and
> return four individual ROI results like a set of measurements for A in one
> table then a set of measurements for B in another table and so on.
>
> I tried the following Macro to analyze ROI labelled A then automatically
> save it to A.xls.
>
> run("8-bit");
> run("Set Measurements...", "area mean standard min bounding area_fraction
> stack display redirect=None decimal=3");
> makeRectangle(0, 0, 356, 301);
> run("Measure");
> dir = getDirectory("image");
> index = lastIndexOf(dir, ".");
> name = "A" + ".xls";
> saveAs("Measurements", dir+name);
>
> Naively , I copied the same Macro and changed the makeRectangle value for
> ROI B then paste it on the next line hoping that it measures the ROI and
> save it to B.xls. Now it looks like this:
>
> run("8-bit");
> run("Set Measurements...", "area mean standard min bounding area_fraction
> stack display redirect=None decimal=3");
> makeRectangle(0, 0, 356, 301);
> run("Measure");
> dir = getDirectory("image");
> index = lastIndexOf(dir, ".");
> name = "A" + ".xls";
> saveAs("Measurements", dir+name);
>
> run("8-bit");
> run("Set Measurements...", "area mean standard min bounding area_fraction
> stack display redirect=None decimal=3");
> makeRectangle(410, 0, 310, 246);
> run("Measure");
> dir = getDirectory("image");
> index = lastIndexOf(dir, ".");
> name = "B" + ".xls";
> saveAs("Measurements", dir+name);
> close();
>
> It does save the B.xls but it contains ROI label A measurements as well. I
> need A.xls to have ROI A measurements only and B.xls to contain ROI B only.
> Any help is appreciated.
>
>
>
> --
> View this message in context:
> http://imagej.1557.x6.nabble.com/ImageJ-How-to-measure-multiple-ROI-in-Batch-Process-using-Macro-and-save-each-ROI-s-result-in-indivie-tp5014612.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>



--

CEO: Dr. rer. nat. Jan Brocher
phone:  +49 (0)6234 917 03 39
mobile: +49 (0)176 705 746 81
e-mail: [hidden email]
info: [hidden email]
inquiries: [hidden email]
web: www.biovoxxel.de

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

Re: ImageJ: How to measure multiple ROI in Batch Process using Macro and save each ROI's result in individual measurement table

Arreis
Thank you very much, I've tested the macro and it works just the way I need it.

Thank you, Jan!

- Love, Arreis