Automating imageJ leaf area processing

2017-10-12

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

Tips for scanning leaves for use in imageJ

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

  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)
  1. 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.
  2. Select “Process” and wait for the macro to finish.
  3. 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