To explain a bit: the ROI interface doesn’t include getArea(), but the PathArea interface does. PolygonROI implements the both.
The idea was that some ROIs might not have an area… i.e. a LineROI. So if you wanted to be extra careful, you could check
if (roi instanceof PathArea)
before requesting the area…. or else just assume that it will probably work and carry on without the cast (Groovy doesn’t insist on it).
However, one warning: getArea() is defined in terms of pixels! It is a property of the ROI, which doesn’t know anything at all about the image it is part of. The aim was to keep the ROI class as simple as possible.
With that in mind you might actually want to use the
getScaledArea(double pixelWidth, double pixelHeight)
method.
It's a bit more effort; you could hard-code the pixel sizes if you know them to be constant, or query them in the script.
Here’s a demonstration to show it in action:
def imageData = getCurrentImageData()
def server = imageData.getServer()
def pixelWidthMicrons = server.getPixelWidthMicrons()
def pixelHeightMicrons = server.getPixelHeightMicrons()
def roi = getSelectedROI()
def areaPixels = roi.getArea()
def areaPixels2 = roi.getScaledArea(1, 1)
def areaMicrons = roi.getScaledArea(pixelWidthMicrons, pixelHeightMicrons)
def areaMM = roi.getScaledArea(pixelWidthMicrons/1000, pixelHeightMicrons/1000)
println "Area in pixels: " + areaPixels
println "Scaled area in pixels: " + areaPixels2
println "Area in microns: " + areaMicrons
println "Area in mm: " + areaMM