Login  Register

Re: How to get area of Roi programatically; where is "Roi.getArea()"

Posted by Nathaniel Ryckman on Jun 03, 2011; 9:49pm
URL: http://imagej.273.s1.nabble.com/How-to-get-area-of-Roi-programatically-where-is-Roi-getArea-tp3684371p3684373.html

Why does the returned value differ from the value returned by using Analyze->Measure?

Is the analyzer class or the statistics class more reliable?

Wayne Rasband wrote
> I need to get the area of FreeRois programatically (not through the
> ImageJ GUI) and have spent a few hours on this without any luck.
> I would expect that the Roi class would have a method like
> 'getArea()'...
> after all, it has getFeretsDiameter()  ;-)
>
> I looked through the JavaDocs, the ImageJ forum, and googled
> around a bit with no luck.
>
> I walked through the code starting with the 'Measure' button on the
> RoiManager and soon found myself in pluging country with somebody
> calling Executer.run( "Measure");
>
> I feel there has to be simpler way that I am just missing.  Can
> anybody give me a hand here?

Here a two ways to get the area of a selection in a plugin:

     ImagePlus img = IJ.getImage();
     double area1 = img.getStatistics().area;

     ImageProcessor ip = img.getProcessor();
     ip.setRoi(img.getRoi());
     ImageStatistics stats = ImageStatistics.getStatistics(ip,
Measurements.MEAN, img.getCalibration());
     double area2 = stats.area;

-wayne