TITLE: Automating imageJ leaf area processing
DATE: 2017-10-12
AUTHOR: John L. Godlee
====================================================================


Last year when I was working at the University of Exeter I was
involved in a project that was measuring hydraulic properties of
leaves from Amazonian trees that had been droughted or not
droughted, as part of the long running DRYFLOR experiment in the
Baia de Caxiuana in Brazil.

I had ~1800 leaves that we scanned to measure their leaf area. The
method I’ve used in the past uses imageJ to threshold the image,
then analyse contiguous blocks of black and white to get the area,
so I wanted to continue using the program I was comfortable with.
However, I wasn’t looking forward to the prospect of measuring the
leaf area of that many leaves so I looked into using the macro
language for imageJ to automate the process.

You can find the macro I used saved as a .ijm file [here], and I
have another macro that also counts the number of leaf objects in
each image [here][1]

 [here]: https://johngodlee.github.io/files/imagej/LeafArea.ijm
 [1]: https://johngodlee.github.io/files/imagej/LeafArea_Count.ijm

Tips for scanning leaves for use in imageJ

-   One Scan per sample unit - normally a leaf or a branch of
   leaves - aids automation
-   Decide on a standard DPI for all scans - 300DPI is fine
-   Try to get the leaf as close to the middle of the page as
   possible
-   Make sure to add a ruler or something of known size in the scan
   area, so you can set your scale
-   Make sure to clean the scanner glass and cover frequently to
   stop bits of sap sticking to it
-   Name all scans with the sample number and _1 _2 _3 etc. if
   multiple scans per sampling unit
-   Label each scan with a written label on the image just in case
   the image names go weird
-   Once the images are scanned, open them up in paint and manually
   remove any elements connected to your leaf that you don’t want
   to include in your analysis by painting them white,
   e.g. petioles, dead leaf areas.

Manual imageJ leaf area

1.  Open image
2.  Convert to 8 bit [Image > Type > 8-bit]
3.  Preserve only the leaf using the Threshold [Image > Adjust >
   Threshold > Move sliders > Apply]
4.  Draw a line of known length over the image scale bar then set
   the scale [Analyze > Set Scale… > Change “Known Distance” and
   “Unit of length”] Set Global if all images from then on will
   have the same scale
5.  Get the area [Analyze > Analyze Particles… > Check Display
   Results]
6.  There might be lots of small particles but if the thresholding
   was done correctly then the leaf should be the largest by far.
   Can also choose “Show outlines” to get an image with the numbers
   written on it in red, which correspond to those on the table.

Preparing images for imageJ automated macro

-   To automate leaf area calculation, all images must have the same
   resolution. Resolution can be changed in Adobe Photoshop using
   an Action:

1.  Open an image in Photoshop
2.  Select Windows -> Actions
3.  Select “Create New Action”
4.  Give the Action an appropriate name and select “Record”
5.  Select Image -> Image Size…
6.  Change “Resolution” to 300
7.  Select “OK”
8.  Select File -> Save As…
9.  Select an appropriate location
10. Change “Format:” to JPEG
11. Go back to the Actions panel and select the Stop button
12. Select File -> Automate -> Batch…
13. Select the recently created Action from the dropdown menu
14. Under “Source:” choose where your images are stored
15. Under “Destination:” choose where your new images will be stored
16. Check the box labelled “Override Action”Save As" Commands"
17. Create an appropriate File Naming system, e.g. “document name” +
   “extension”
18. Select “OK”
19. Check that new images are the desired resolution by opening some
   and selecting Image -> Image Size…

Analyzing in imageJ using automated macro

1.  Open ImageJ
2.  Select Process -> Batch -> Macro…
3.  Select the appropriate input and output files
   -   Input should be where your images are
   -   Output should be where you want any files generated by the
       macro to go
4.  Insert the following code into the large box, can also be loaded
   from LeafArea.ijm file:

   // Calculate area of dark objects (leaves) against white background.
   // 79.7619px/cm a4-200dpi.
   // 120.006px/cm a4-300dpi.
   // Change `size min` to analyse smaller objects, but increase noise

   run("8-bit");
   setAutoThreshold("Default");
   //run("Threshold...");
   //setThreshold(0, 146);
   setOption("BlackBackground", false);
   run("Convert to Mask");
   run("Set Scale...", "distance=120.006 known=1 pixel=1 unit=cm global");
   run("Analyze Particles...", "size=0.70-Infinity show=Outlines display add");
   setOption(“Display Label”, true)

5.  Under “Output Format” selecgt “8 bit TIFF” to generate a file
   for each image containing outlines of all objects analysed. Use
   these to check that the macro worked properly.
6.  Select “Process” and wait for the macro to finish.
7.  A window should open containing a list of objects, their areas,
   and the file in which the objects were found. The results in
   this window can be copied and pasted into an excel file for
   analysis.

Customising the ImageJ automated leaf area macro

-   setThreshold(... is the minimum and maximum grey values to be
   selected for the image analysis. These values can be generated
   by doing a manual threshold and moving the sliders until only
   the leaves are highlighted.

-   distance=... is a distance given in pixels used to set the scale
   of the image. 120 is for 1 cm in a 300dpi a4 image. This value
   can be generated by manually setting the scale in ImageJ.

-   known=... is the real distance that the “distance” value
   inhabits in the image.

-   size=...-... is the minimum and maximum object size that will be
   analyzed by ImageJ with units of the scale specified by
   “distance” and “known”.

-   circularity=...-... is the minimum and maximum object
   circularity that will be analyzed, with 0 being a straight line
   and 1 being a perfect circle, useful for excluding rulers in the
   analysis. Could also be used to measure features on the leaves
   such as smut fungi, tar spots etc.