Login  Register

Re: Combined surface area of all ROI in an image

Posted by mmettlen on May 16, 2017; 1:49pm
URL: http://imagej.273.s1.nabble.com/Combined-surface-area-of-all-ROI-in-an-image-tp5018716p5018732.html

Thanks for your replies! I've worked on it too and wrote a little macro that does what Kees suggested. In a nutshell, the macro takes a stack of images and the corresponding ROI manager that contains all the ROIs. It than makes a new 8-bit black stack with the same dimension as the original stack, copies the ROIs into it, flattens them, and uses them to quantify the total area of all ROIs/slice:

if (isOpen("Results")) {selectWindow("Results"); run ("Close");}
dir = getDirectory("image"); //the original stack has to be open with its corresponding ROI manager
title = getTitle();
getDimensions(width, height, channels, slices, frames);
FolderName = split(title,".");
DirName = FolderName[0];
File.makeDirectory(dir+File.separator+DirName);
while (nImages>0) {
          selectImage(nImages);
        close();}
run("Set Measurements...", "area limit display redirect=None decimal=2");
newImage("Untitled", "8-bit black", width, height, frames);
roiManager("Set Fill Color", "white"); //copies all ROIs into a given slice - pulled from ROI manager
roiManager("Show All"); //copies all ROIs into a given slice - pulled from ROI manager
for (i=1; i<=nSlices; i++)
{run("Set Slice...", "slice="+i);
setBatchMode(true);
run("Flatten", "slice"); //'burn's white ROIs into black image
run("8-bit");
saveAs("tif", dir+DirName+File.separator+"Mask_"+i+".tif"); close();}; //the saved images will be used below to automatically generate a stack
while (nImages>0) {
          selectImage(nImages);
        close();}
setBatchMode(false);
run("Image Sequence...", "open="+dir+DirName+File.separator+"Mask_1.tif sort");
setThreshold(100, 255); //stack is thresholded and used to measure surface area of all ROIs
for (i=1; i<=nSlices; i++)
{run("Set Slice...", "slice="+i);
run("Measure");}
selectWindow("Results");
saveAs("txt", dir+File.separator+"Results_"+DirName+".txt");
run("Close");
while (nImages>0) {
          selectImage(nImages);
        close();}
if (isOpen("Log")) {selectWindow("Log"); run ("Close");}