README.md

FIELDimageR: A Tool to Analyze Images From Agricultural Field Trials and Lab in R.

This package is a compilation of functions to analyze orthomosaic images from research fields. To prepare the image it first allows to crop the image, remove soil and weeds and rotate the image. The package also builds a plot shapefile in order to extract information for each plot to evaluate different wavelengths, vegetation indices, stand count, canopy percentage, and plant height.

--------------------------------------------- ## Resources [Installation](#instal) [1. First steps](#p1) [2. Loading mosaics and visualizing](#p2) [3. Removing soil using vegetation indices](#p3) [4. Building the plot shapefile](#p5) [5. Building vegetation indices](#p6) [6. Counting the number of objects (e.g. plants, seeds, etc)](#p7) [7. Evaluating the object area percentage (e.g. canopy)](#p8) [8. Extracting data from field images](#p9) [9. Estimating plant height (e.g., biomass) and creating interpolated mosaics based on sampled points](#p10) [10. Distance between plants, objects length, and removing objects (plot, cloud, weed, etc.)](#p11) [11. Resolution and computing time](#p12) [12. Crop growth cycle](#p13) [13. Multispectral and Hyperspectral images](#p14) [14. Building shapefile with polygons (field blocks, pest damage, soil differences, etc)](#p15) [15. Making plots](#p16) [16. Saving output files](#p17) [Orthomosaic using the open source software OpenDroneMap](#p18) [Parallel and loop to evaluate multiple images (e.g. images from roots, leaves, ears, seed, damaged area, etc.)](#p19) [Quick tips (memory limits, splitting shapefile, using shapefile from other software, etc)](#p20) [Contact](#p21)
--------------------------------------------- ### Installation > If desired, one can [build](#Instal_with_docker) a [rocker/rstudio](https://hub.docker.com/r/rocker/rstudio) based [Docker](https://www.docker.com/) image with all the requirements already installed by using the [Dockerfile](https://github.com/OpenDroneMap/FIELDimageR/blob/master/Dockerfile) in this repository.
**With RStudio** > First of all, install [R](https://www.r-project.org/) and [RStudio](https://rstudio.com/). > Then, in order to install R/FIELDimageR from GitHub [GitHub repository](https://github.com/OpenDroneMap/FIELDimageR), you need to install the following packages in R. For Windows users who have an R version higher than 4.0, you need to install RTools, tutorial [RTools For Windows](https://cran.r-project.org/bin/windows/Rtools/). > Now install R/FIELDimageR using the `install_github` function from [devtools](https://github.com/hadley/devtools) package. If necessary, use the argument [*type="source"*](https://www.rdocumentation.org/packages/ghit/versions/0.2.18/topics/install_github). wzxhzdk:0 > If the method above doesn't work, use the next lines by downloading the FIELDimageR-master.zip file wzxhzdk:1

**With Docker** > When building the Docker image you will need the [Dockerfile](https://github.com/OpenDroneMap/FIELDimageR/blob/master/Dockerfile) in this repository available on the local machine. > Another requirement is that Docker is [installed](https://docs.docker.com/get-docker/) on the machine as well. > > Open a terminal window and at the command prompt enter the following command to [build](https://docs.docker.com/engine/reference/commandline/build/) the Docker image: wzxhzdk:2 > The different command line parameters are as follows: >* `docker` is the Docker command itself >* `build` tells Docker that we want to build an image >* `-t` indicates that we will be specifying then tag (name) of the created image >* `fieldimager` is the tag (name) of the image and can be any acceptable name; this needs to immediately follow the `-t` parameter >* `-f` indicates that we will be providing the name of the Dockerfile containing the instructions for building the image >* `./Dockerfile` is the full path to the Dockerfile containing the instructions (in this case, it's in the current folder); this needs to immediately follow the `-f` parameter >* `./` specifies that Docker build should use the current folder as needed (required by Docker build) > > The container includes a copy of RStudio Server and `tidyverse` (see [rocker/tidyverse](https://hub.docker.com/r/rocker/tidyverse/)). Alternatively, you can substitute `rocker/tidyverse` in the first line of `Dockerfile` with `rocker/rstudio` for a RStudio environment without the `tidyverse` package. > > Once the docker image is built, you use the [Docker run](https://docs.docker.com/engine/reference/run/) command to access the image using the suggested [rocker/rstudio](https://hub.docker.com/r/rocker/rstudio/) command: wzxhzdk:3 > Open a web browser window and enter `http://localhost:8787` to access the running container. > To log into the instance use the username and password of `rstudio` and `yourpasswordhere`. #### If you are using anaconda and Linux > To install this package on Linux and anaconda it is necessary to use a series of commands before the recommendations * Install Xorg dependencies for the plot system (on conda shell) wzxhzdk:4 * Install the BiocManager package manager wzxhzdk:5 * Use the BiocManager to install the EBIMAGE package wzxhzdk:6 * If there is an error in the fftw3 library header (fftw3.h) install the dependency (on conda shell) wzxhzdk:7 * If there is an error in the dependency doParallel wzxhzdk:8 * Continue installation wzxhzdk:9 [Menu](#menu)
--------------------------------------------- ### Using R/FIELDimageR

#### 1. First steps > **Taking the first step:** wzxhzdk:10 [Menu](#menu)
--------------------------------------------- #### 2. Loading mosaics and visualizing > The following example uses an image available to download here: [EX1_RGB.tif](https://drive.google.com/open?id=1S9MyX12De94swjtDuEXMZKhIIHbXkXKt). If necessary, the image/mosaic size can be reduced around the field boundaries for faster image analysis using the function: **`fieldCrop`**. wzxhzdk:11

wzxhzdk:12

[Menu](#menu)
--------------------------------------------- #### 3. Removing soil using vegetation indices > The presence of soil can introduce bias in the data extracted from the image. Therefore, removing soil from the image is one of the most important steps for image analysis in agricultural science. Function to use: **`fieldMask`** wzxhzdk:13

[Menu](#menu)
--------------------------------------------- #### 4. Building the plot shape file > Once the field has reached a correct straight position, the plot shape file can be drawn by selecting at least four points at the corners of the experiment. The number of columns and rows must be informed. At this point the experimental borders can be eliminated, in the example bellow the borders were removed in all the sides. Function to use is from [FIELDimageR.Extra: **`fieldShape_render`**](https://github.com/filipematias23/FIELDimageR.Extra#p3) wzxhzdk:14

> **Attention:** The plots are identified in ascending order from *left to right* and *bottom to top* (this is another difference from the **`FIELDimageR::fieldShape`** ) being evenly spaced and distributed inside the selected area independent of alleys.

> One matrix can be used to identify the plots position according to the image above. The function **`fieldMap`** can be used to specify the plot *ID* automatic or any other matrix (manually built) also can be used. For instance, the new column **PlotName** will be the new identification. You can download an external table example here: [DataTable.csv](https://drive.google.com/open?id=18YE4dlSY1Czk2nKeHgwd9xBX8Yu6RCl7). wzxhzdk:15

wzxhzdk:16

wzxhzdk:17

[Menu](#menu)
--------------------------------------------- #### 5. Building vegetation indices > A general number of indices are implemented in *FIELDimageR* using the function **`fieldIndex`**. Also, you can build your own index using the parameter `myIndex`.

wzxhzdk:18

> *Sugestion:* This function could also be used to build an index to remove soil or weeds. First it is necessary to identify the threshold to differentiate soil from the plant material. At the example below (B), all values above 0.7 were considered as soil and further removed using **`fieldMask`** (C & D). wzxhzdk:19

[Menu](#menu)
--------------------------------------------- #### 6. Counting the number of objects (e.g. plants, seeds, etc) > *FIELDimageR* can be used to evaluate stand count during early stages. A good weed control practice should be performed to avoid misidentification inside the plot. The *mask* output from **`fieldMask`** and the *fieldshape* output from **`fieldShape`** must be used. Function to use: **`fieldCount`**. wzxhzdk:20

wzxhzdk:21

> To refine stand count, we can further eliminate weeds (small plants) or outlying branches from the output using the parameter *min Size*. The following example uses an image available to download here:[EX_StandCount.tif](https://drive.google.com/open?id=1PzWcIsYQMxgEozR5HHUhT0RKcstMOfSk) wzxhzdk:22

wzxhzdk:23

wzxhzdk:24

> **`fieldCount`** also can be used to count other objects (e.g. seed, pollen, etc). The example below *FIELDimageR* pipeline was used to count seeds in a corn ear.

> **`fieldMask`** with index BIM (Brightness Index Modified) was used to identify pollen and **`fieldCount`** was used to count total and germinated pollens per sample. [Download EX_Pollen.jpeg](https://drive.google.com/open?id=1Tyr4cEvEBoaWqaHw8UWPW-X1unzEMAfk) wzxhzdk:25

[Menu](#menu)
--------------------------------------------- #### 7. Evaluating the object area percentage (e.g. canopy) > *FIELDimageR* can also be used to evaluate the canopy percentage per plot. The *mask* output from **`fieldMask`** and the *fieldshape* output from **`fieldShape`** must be used. Function to use: **`fieldArea`**. The parameter *n.core* is used to accelerate the canopy extraction (parallel). wzxhzdk:26

> **`fieldArea`** also can be associated with the function **`fieldMask`** to evaluate the proportion of seed colors in Glass Gems Maize. An important step is to choose the right contrasting background to differentiate samples. The same approach can be used to evaluate the percentage of disease/pests’ damage, land degradation, deforestation, etc.

[Menu](#menu)
--------------------------------------------- #### 8. Extracting data from field images > The function *extract* from **[terra](https://CRAN.R-project.org/package=terra)** is adapted for agricultural field experiments through function [**`FIELDimageR.Extra::fieldInfo_extra`**](https://github.com/filipematias23/FIELDimageR.Extra#p6). wzxhzdk:27

[Menu](#menu)
--------------------------------------------- #### 9. Estimating plant height and biomass > The plant height can be estimated by calculating the Canopy Height Model (CHM) and biomass by calculating Canopy Volume Model (CVM). This model uses the difference between the Digital Surface Model (DSM) from the soil base (before there is any sproute, [Download EX_DSM0.tif](https://drive.google.com/open?id=1lrq-5T6x_GrbkCtpDSDiX1ldvSwEBFX-)) and the DSM file from the vegetative growth (once plants are grown, [Download EX_DSM1.tif](https://drive.google.com/open?id=1q_H4Ef1f1yQJOPtkVMJfcb2SvHcxJ3ya)). To calculate CHM and CVM we used the function **`fieldHeight`**, where CVM=cellSize(CHM)*CHM. The next step is removing the soil effect with **`fieldMask`** and then use **`fieldInfo_extra`** to extract information as mean, max, min, sum, and quantile. wzxhzdk:28

### Creating interpolated mosaics based on sampled points > Attention: In case the user has only one flight (e.g., one DSM) the function **`fieldInterpolate`** can be used to creat the soil-reference based on sampled points at the DSM1. wzxhzdk:29

[Menu](#menu)
--------------------------------------------- #### 10. Distance between plants, objects length, and removing objects (plot, cloud, weed, etc.) > The function **`fieldObject`** can be used to take relative measurement from objects (e.g., area, "x" distance, "y" distance, number, extent, etc.) in the entire mosaic or per plot using the fieldShape file. Download example here: [EX_Obj.jpg](https://drive.google.com/file/d/1F189a1LKA9_l0Xn8hdBOshL5eC_fQLPs/view?usp=sharing). wzxhzdk:30

> The function **`fieldCrop`** can be used to remove objects from the field image. For instance, the parameter *remove=TRUE* and *nPoint* should be used to select the object boundaries to be removed. [Download EX_RemObj.tif](https://drive.google.com/open?id=1wfxSQANRrPOvJWwNZ6UU0UjXwzKfkKH0)). wzxhzdk:31

> The function **`fieldDraw`** can be used to draw lines or polygons in the field image. This function allows to extract information of specific positions in the field (x, y, value). Also, this function can be used to evaluate distances between objects (for example: distance between plants in a line) or either objects length (for example: seed length, threes diameter, etc.). Let's use the image above to evaluate the distance between potato plots and later to extract NGRDI values from a line with soil and vegetation to observe the profile in a distance plot. wzxhzdk:32

[Menu](#menu)
--------------------------------------------- #### 11. Resolution and computing time > The influence of image resolution was evaluated at different steps of the FIELDimageR pipeline. For this propose, the resolution of image *EX1_RGB_HighResolution.tif* [Download](https://drive.google.com/open?id=1elZe2jfq4bQSZM8cFAS4q7fRrnXbSBgH) was reduced using the function **raster::aggregate** in order to simulate different flown altitudes Above Ground Surface (AGS). The parameter *fact* was used to modify the original image resolution (0.4x0.4 cm with 15m AGS) to: first, **fact=2** to reduce the original image to 0.8x0.8 cm (simulating 30m AGS), and **fact=4** to reduce the original image to 1.6x1.6 (simulating 60m AGS). The steps (*i*) removing soil, (*ii*) building vegetation index (BGI), and (*iii*) getting information were evaluated using the function **system.time** output *elapsed* (R base). wzxhzdk:33 > The time to run one function using the image with pixel size of 0.4x0.4 cm can be 10 (**`fieldInfo`**) to 70 times (**`fieldIndex`**) slower than the image with pixel size of 1.6x1.6 cm (Table 1). The computing time to extract BGI index (one layer) with 0.4x0.4 cm was ~23 min whereas only ~7 min with the 0.8x0.8 cm image, and ~2 min using the 1.6x1.6 cm image. the time to extract the RGB information (three layers) was ~2.3 min for the 1.6x1.6 cm image and ~66 min for the 0.4x0.4 cm image. It is important to highlight that the resolution did not affect the plots mean, it has a correlation >99% between 0.4x0.4 cm and 1.6x1.6 (Table 2). High resolution images showed to require higher computational performance, memory, and storage space. We experienced that during the image collection in the field a low altitudes flight needs more batteries and a much greater number of pictures, and consequently longer preprocessing images steps to build ortho-mosaics.

[Menu](#menu)
--------------------------------------------- #### 12. Crop growth cycle > The same rotation theta from step 3, mask from step 4, and plot shape file from step 5, can be used to evaluate mosaics from other stages in the crop growth cycle. Here you can download specific images from flowering and senecense stages in potatoes. ([**Flowering: EX2_RGB.tif**](https://drive.google.com/open?id=1B1HrIYUVqSpKdDN8E8VudpI8jT8MYbWY) and [**Senescence: EX3_RGB.tif**](https://drive.google.com/open?id=15GpLy669mICpkorbUk1M9vqfSUMHbdc5))

wzxhzdk:34

[Menu](#menu)
--------------------------------------------- #### 13. Multispectral and Hyperspectral images > **`FIELDimageR`** can be used to analyze multispectral and hyperspectral images. The same rotation theta, mask, and plot shape file used to analyze RGB mosaic above can be used to analyze multispectral or hyperspectral mosaic from the same field. **Multispectral:** You can dowload a multispectral example here: [**EX1_5Band.tif**](https://drive.google.com/open?id=1vYb3l41yHgzBiscXm_va8HInQsJR1d5Y) wzxhzdk:35 **Hyperspectral:** in the following example you should download the hyperspectral tif file ([**EX_HYP.tif**](https://drive.google.com/file/d/1oOKUJ4sA1skyU7NU7ozTQZrT7_s24luW/view?usp=sharing)), the 474 wavelength names ([**namesHYP.csv**](https://drive.google.com/file/d/1n-3JX0ho1rMSOiGrrYmBKHkeF4hyHQw1/view?usp=sharing)), and the field map data ([**DataHYP.csv**](https://drive.google.com/file/d/1u78jb9BlA45kVaXUu2RKM6_KJjXIpJXq/view?usp=sharing)). wzxhzdk:36

[Menu](#menu)
--------------------------------------------- #### 14. Building shapefile with polygons (field blocks, pest damage, soil differences, etc) > If your field area does not have a pattern to draw plots using the function **fieldShape** you can draw different polygons using the function **fieldPolygon**. To make the fieldshape file the number of polygons must be informed. For instance, for each polygon, you should select at least four points at the polygon boundaries area. This function is recommended to make shapefile to extract data from specific field blocks, pest damage area, soil differences, etc. If *extent=TRUE* the whole image area will be the shapefile (used to analyze multiple images for example to evaluate seeds, ears, leaves, diseases, etc.). Function to use: **fieldPolygon**. The following example uses an image available to download here: [EX_polygonShape.tif](https://drive.google.com/open?id=1b42EAi3A3X54z00JWjFvsmUAm3yQClPk). wzxhzdk:37

[Menu](#menu)
--------------------------------------------- #### 15. Making plots > Graphic visualization of trait values for each plot using the **fieldShape file** and the **Mosaic** of your preference. Function to use: **`fieldPlot`**. wzxhzdk:38

[Menu](#menu)
--------------------------------------------- #### 16. Saving output files wzxhzdk:39 [Menu](#menu)
--------------------------------------------- #### Orthomosaic using the open source software **[OpenDroneMap](https://www.opendronemap.org)** > Image stitching from remote sensing phenotyping platforms (sensors attached to aerial or ground vehicles) in one orthophoto. 1) Follow the [OpenDroneMap’s documentation](https://docs.opendronemap.org/index.html) according to your operating system (Windows, macOS or Linux) to install [**WebODM**](https://www.opendronemap.org/webodm/). 2) Start [WebODM](https://www.opendronemap.org/webodm/) and **+Add Project** to upload your pictures (.tif or .jpg). Attached is one example of RGB images from experimental trials of [UW-Madison Potato Breeding and Genetics Laboratory](https://potatobreeding.cals.wisc.edu) during the flowering time at [Hancock Agricultural Research Station](https://hancock.ars.wisc.edu). Flight altitude was 60 m above ground, flight speed was 24 km/h, and image overlap was 75%. Donwload pictures [here](https://drive.google.com/open?id=1t0kjcBy6QzmIz_fVs6vsgZXi9Afqe09b). 3) After the running process '*completed*', download the **odm_orthophoto.tif** and **dsm.tif** to upload in R. Then follow the pipeline of *FIELDimageR*. [Donwload the final *odm_orthophoto.tif](https://drive.google.com/open?id=1XCDvbFdzHDmRA1dJSMp_veX_xC_ThiqC)*. wzxhzdk:40

[Menu](#menu)
--------------------------------------------- #### Parallel and loop to evaluate multiple images > The following code can be used to evaluate multiple images (e.g. root area, leave indices, damaged area, ears, seed, structures, etc.). The example below is evaluating disease damage in images of leaves (affected area and indices). Download the example [here](https://drive.google.com/open?id=1zgOZFd7KuTu4sERcG1wFoAWLCahIJIlu). wzxhzdk:41

[Menu](#menu)
--------------------------------------------- #### Quick tips (image analyze in R) 1) [Changing memory limits in R](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Memory-limits.html) wzxhzdk:42 2) [Reducing resolution for fast analysis](https://www.rdocumentation.org/packages/raster/versions/3.0-12/topics/aggregate) wzxhzdk:43 3) Removing previous files after used (more space in R memory): If you do not need one mosaic you should remove it to save memory to the next steps (e.g. the first input mosaic) wzxhzdk:44 4) Using ShapeFile from other software (e.g. [QGIS](https://www.qgis.org/en/site/)) wzxhzdk:45 5) Combining ShapeFiles: sometimes it is better to split the mosaic into smaller areas to better draw the shapefile. For instance, the user can combine the split shapefiles to one for the next step as extractions wzxhzdk:46 6) External window to amplify the RStudio plotting area (It helps to visualize and click when using functions: **fieldCrop**, **fieldRotate**, **fieldShape**, and **fieldPolygon**). The default graphics device is normally "RStudioGD". To change use "windows" on Windows, "quartz" on MacOS, and "X11" on Linux. wzxhzdk:47 [Menu](#menu)
--------------------------------------------- ### YouTube Tutorial > FIELDimageR: A tool to analyze orthomosaic images from agricultural field trials in R (Basic Pipeline)

> FIELDimageR: Counting the number of plants (fieldCount)

> FIELDimageR: Calculating vegetation indices (NDVI and NDRE)

> FIELDimageR: Estimate plant height using the canopy height model (CHM)

### FIELDimageR courses * **Embrapa Beef-Cattle**: https://fieldimager.cnpgc.embrapa.br/course.html

* **Embrapa Maize & Sorghum**: http://fieldimager.cnpms.embrapa.br/course.html

* **Universidade Federal de Viçosa (GenMelhor)**: https://genmelhor-fieldimager.000webhostapp.com/FIELDimageR_Course%20(2).html

* **PhenomeForce (Fridays Hands-On: A Workshop Series in Data+Plant Sciences)**: https://phenome-force.github.io/FIELDimageR-workshop/

* **The Plant Phenome Journal**: https://www.youtube.com/watch?v=DOD0ZX_J8tk

### Google Groups Forum > This discussion group provides an online source of information about the FIELDimageR package. Report a bug and ask a question at: * https://groups.google.com/forum/#!forum/fieldimager * https://community.opendronemap.org/t/about-the-fieldimager-category/4130 ### Developers > **Help improve FIELDimageR pipeline**. The easiest way to modify the package is by cloning the repository and making changes using [R projects](https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects). > If you have questions, join the forum group at https://groups.google.com/forum/#!forum/fieldimager >Try to keep commits clean and simple >Submit a **_pull request_** with detailed changes and test results. **Let's work together and help more people (students, professors, farmers, etc) to have access to this knowledge. Thus, anyone anywhere can learn how to apply remote sensing in agriculture.** ### Licenses > The R/FIELDimageR package as a whole is distributed under [GPL-2 (GNU General Public License version 2)](https://www.gnu.org/licenses/gpl-2.0.en.html). ### Citation > *Matias FI, Caraza-Harter MV, Endelman JB.* FIELDimageR: An R package to analyze orthomosaic images from agricultural field trials. **The Plant Phenome J.** 2020; [https://doi.org/10.1002/ppj2.20005](https://doi.org/10.1002/ppj2.20005) > * Pawar P & Matias FI.* FIELDimageR.Extra. *Pawar P. & Matias FI.* FIELDimageR.Extra: Advancing user experience and computational efficiency for analysis of orthomosaic from agricultural field trials. **The Plant Phenome J.** 2023; [https://doi.org/10.1002/ppj2.20083](https://doi.org/10.1002/ppj2.20083) ### Author > * [Filipe Inacio Matias](https://github.com/filipematias23) ### Acknowledgments > * [University of Wisconsin - Madison](https://horticulture.wisc.edu) > * [UW Potato Breeding and Genetics Laboratory](https://potatobreeding.cals.wisc.edu) > * [Dr Jeffrey Endelman, PhD Student Maria Caraza-Harter, and MS Student Lin Song](https://potatobreeding.cals.wisc.edu/people/) > * [OpenDroneMap](https://www.opendronemap.org/) [Menu](#menu)


OpenDroneMap/FIELDimageR documentation built on May 4, 2024, 4:24 a.m.