Re: ROI post processing help: split in two equal surfaces
Posted by vischer on Mar 31, 2011; 9:52am
URL: http://imagej.273.s1.nabble.com/ROI-post-processing-help-split-in-two-equal-surfaces-tp3685206p3685208.html
Hi julien (and Wayne)
I have to make a correction to my previous mail where I suggested to fill the roi with a constant value- this would only give you the centroid, not divide the roi into equal parts.
Below is a demo how to divide a roi into two parts:
- The first macro creates a demo roi
- The second divides it into halves.
Wayne, there is a strange behavior, which I only cold work-around by inserting a "Copy" command. Roi operations shouldn't change the image, but if I omit the "Copy" work around, a part of the contour is painted into the image.
Norbert Vischer
macro "Create a roi [F1]"{
run("Leaf (36K)");
run("8-bit");
setThreshold(229, 255);
doWand(26, 223);
resetThreshold();
}
//divides current roi into left and right half
macro "divide roi [F2]"{
run("Clear Results");
roiManager("Reset");
run("Add to Manager");
roiManager("Select", 0);
getSelectionBounds(x, y, width, height)
getRawStatistics(totalArea);
minOff = 1e9;
for (w=1; w < width; w++){
roiManager("Select", 0);
setKeyDown("alt");
makeRectangle(x, y, w, height);
getRawStatistics(rightArea);
off = abs(2*rightArea - totalArea);
if (off < minOff){
minOff = off;
xHalf = x + w;
wHalf = w;
halfArea = rightArea;
}
}
//Display result:
roiManager("Select", 0);
setKeyDown("alt");
makeRectangle(x, y, wHalf, height);
run("Copy");//work around
run("Add to Manager");
run("Measure");
wait(500);
roiManager("Select", 0);
setKeyDown("alt");
makeRectangle(x+wHalf, y, width - wHalf, height);
run("Copy");//work around
run("Add to Manager");
run("Measure");
}