 # Jython Script to calculate area fraction

Hi all! I am having some issues to calculate the area fraction form a Jyhton script. The idea is to load an image, generate some ROI’s across the image and calculate the area fraction inside each ROI and store that value into a table.

Apparently one can in Java you use List.setMeasurements and then
getValue(“Mean, Median, Feret, Area, etc.”) but i have not been able to make it work.

Below you can see my progress.

``````# Load 2D image
od = OpenDialog("Choose a file", None)
filename = od.getFileName()

if filename is None:
print "User canceled the dialog!"
else:
folder = od.getDirectory()
filepath = folder + filename
print "Selected file path:", filepath

# Load the image into a ImagePlus object
imp = IJ.openImage(os.path.join(folder, filename))

# Get imp processor
ip = imp.getProcessor()

# Create a table to store the results
table = ResultsTable()

# Create a ROI manager, to store each ROI (every cell)
roiM = RoiManager(True)

# We want to split the image e.g. into 4 sub ROI's
# height and widht of the ROI
nx=2
ny=2
w=imp.width/nx
h=imp.height/ny

# x,y values from the left corner
# generate nx and ny ROI's across the image and store it into the ROImanager
y=0
x=0
for i in xrange(nx):
x=1.1*i*w
for j in xrange(ny):
y=1.1*j*h
roi = Roi(x, y, w, h)

# Here I want to print the area of the full image
print "Full image ", imp.getStatistics().mean

# Calculate the area fraction from each ROI
for i in range(roiM.getCount()):
roi = roiM.getRoi(i)
imp.setRoi(roi)
print "Cell i: ", i, " Cell j: ", j, imp.getStatistics()

imp.show()```

Thanks!
Mauro``````

I think you are confusing the Macro API with the Java API. The functions you are citing are ImageJ macro functions.
However I’m aware of a new Java API function `IJ.getValue(imp,measurement)` @Wayne added just recently (latest Github commit) to get the measurements values which should also be applicable to Jython in a similar way and might work for you (not tested by myself - update ImageJ to the latest development version):

Also in this thread there is an similar example how to fill and save a Results Table in Java (which you have to convert to Jython):

http://imagej.1557.x6.nabble.com/IJ-renameResults-summaryTitle-quot-Results-quot-seems-to-have-a-timing-issue-td5022334.html

what do you mean by Area Fraction?
Have you thresholded it before, is it a Black and White image?

I am not exactly sure what you mean by area fraction inside each Roi
Do you want the to compute the ratio Roi_area/Image_area for each Roi ?
or do you want the area fraction as described here : the portion of thresholded pixel in the ROI

A few points from my side,

Beware that mean returns the mean gray level of the image, it is a measure of intensity not area, unless you have a binary image (not mentionned).

It’s probably the same confusion above between area or fraction area.
For the image size, you should use `imp.getStatistics().area` (or simply the product of `imp.width` x `imp.height`
For the areaFraction : `imp.getStatistics().areaFraction`.

Same for the ROI, you can use in your 1st for loop when you add the roi to the RoiManager (the second for loop with the measurement is no more necessary then):

``````roi.setImage(imp) # associate the roi stats to the underlying image
roiStat = roi.getStatistics()
area = roiStat.area
areaFraction = roiStat.areaFraction
``````

Finally you can store the results in an instance of ResultsTable using the `addValue` method.

Good luck !

1 Like

Yes, the image is already thresholded. I mean the ratio black area/total area, if I’m not wrong that is what you get when you can select “area fraction” in set measurements.

Thanks

ok perfect.
so I think the method proposed by @LThomas will work.

Hi thanks for your answer. Yes I want to do excactly that (portion of thresholded pixel in the ROI), an I haven’t mentioned but the image is already thresholded.

Yes, then I realized that and use it to get the area fraction indirectly as `imp.getStatistics().mean/imp.getStatistics().max*100.0`

That is what I was looking for! I wasn’t able to find the appropiate method. Is there a class reference somewere? When I check the documentation I didn’t find any reference to `areFraction`.

Thanks for the help, I will aply the sugested changes to te code!

There is the ImageStatistics class with a list of fields, including the areaFraction.
You get an object of type ImageStatistics when you call `imp.getStatistics()` or `roi.getStatistics()`, from the resulting object then you can access any of the field listed in the class (except the protected ones eventually).

The following JavaScript example uses two methods to get the area fraction. The first uses IJ.getValue(img,"%Area"), available in ImageJ 1.52q. The second uses ImagePlus.getAllStatistics(). To upgrade to ImageJ 1.52q, use the Help>Update ImageJ command and select “daily build” from the drop down menu.

``````  img = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
IJ.setAutoThreshold(img, "Default");
img.setRoi(new OvalRoi(81,54,36,40));
areaFraction = IJ.getValue(img, "%Area");
IJ.log("Area fraction="+areaFraction);
areaFraction2 = img.getAllStatistics().areaFraction;
IJ.log("Area fraction2="+areaFraction2);
``````
2 Likes  Great, thanks for the help!