A learnr tutorial for RGB orthomosaic analysis and plot-level GLI traits.
This repository contains a practical, self-paced R learnr tutorial for processing RGB orthomosaic imagery and extracting plot-level crop traits. Learners progress through 12 hands-on activities that transform red, green, and blue image bands into the Green Leaf Index (GLI), classify vegetation using Otsu thresholding, estimate canopy cover, and summarize phenotypic traits within field-plot boundaries.
The tutorial is designed for students, researchers, plant breeders, agronomists, and others learning remote sensing and high-throughput phenotyping. It runs as a Shiny-prerendered learnr tutorial and uses standard R plots and printed tables rather than interactive maps, Plotly graphics, or interactive data tables.
After completing the tutorial, learners will be able to:
- Inspect the structure, resolution, extent, and coordinate reference system of an RGB orthomosaic.
- Identify and name the Red, Green, and Blue raster bands.
- Display true-color imagery and compare individual spectral bands.
- Calculate the Green Leaf Index from RGB pixel values.
- Examine the distribution of GLI values.
- Calculate an image-specific vegetation threshold using Otsu's method.
- Create a binary vegetation mask.
- Produce a vegetation-only GLI raster.
- Inspect field-plot polygons, attributes, identifiers, and coordinate reference systems.
- Align and overlay plot boundaries with raster products.
- Extract plot-level mean GLI, GLI variability, and canopy cover.
- Visualize plot-level mean GLI and canopy cover as heatmaps over the RGB orthomosaic.
- Export plot-level crop traits as a CSV file.
The tutorial guides learners through the following activities:
Load the orthomosaic and examine its number of layers, spatial resolution, extent, and coordinate reference system.
Create a true-color composite using the first three raster bands in the expected order:
- Red
- Green
- Blue
Assign descriptive band names and display the Red, Green, and Blue layers separately to compare their spatial patterns.
Calculate the Green Leaf Index for every valid image pixel:
GLI = (2G - R - B) / (2G + R + B)
Pixels with a denominator of zero are assigned NA. The resulting raster is saved as outputs/gli.tif.
Apply Otsu's method to a regular sample of valid GLI values. The method identifies a threshold that maximizes between-class variance, helping separate likely vegetation from background pixels. The threshold is saved as outputs/otsu_threshold.rds.
Classify pixels as vegetation when their GLI value exceeds the Otsu threshold:
Vegetation = 1 when GLI > Otsu threshold
Background = 0 otherwise
The binary mask is saved as outputs/vegetation_mask.tif.
Use the vegetation mask to retain GLI values only for classified vegetation pixels. Background pixels are assigned NA, and the output is saved as outputs/vegetation_gli.tif.
Examine the plot polygons, attribute fields, geometry type, feature count, and coordinate reference system. The activity also displays each polygon with its plot_id label.
If the source data do not contain a recognized plot identifier, the tutorial creates identifiers in the form Plot_1, Plot_2, and so forth.
Check the raster and vector coordinate reference systems. When necessary, project the plot polygons to the raster coordinate reference system before overlaying them on the vegetation-only GLI layer.
Use fractional pixel coverage at polygon boundaries to calculate:
- Mean vegetation GLI
- Standard deviation of vegetation GLI
- Canopy cover percentage
The resulting trait table is saved as outputs/plot_level_rgb_gli_traits.csv.
Join mean vegetation GLI values to the plot polygons and display transparent, color-coded plot overlays on the RGB orthomosaic. Each plot is labeled with its mean GLI value.
Display plot-level canopy cover as transparent green overlays on the RGB orthomosaic. Each polygon is labeled with its canopy-cover percentage.
The tutorial expects the following input files:
data/
├── rgb_orthomosaic.tif
└── plot_boundaries.geojson
- The orthomosaic must contain at least three bands.
- The first three bands must be ordered as Red, Green, and Blue.
- The raster must contain valid numeric pixel values.
- The tutorial uses only the first three bands if the source image contains additional layers.
The GeoJSON file must contain polygon features representing experimental plots. The tutorial searches for a plot identifier using the following field names, in order:
plot_id, Plot_ID, plot, Plot, id, ID
If none of these fields exists, sequential identifiers are created automatically.
A recommended repository structure is:
Phenomic_Tutorial/
├── data/
│ ├── rgb_orthomosaic.tif
│ └── plot_boundaries.geojson
├── outputs/
├── RGB_GLI_Plot_Traits.Rmd
├── install.R
├── Dockerfile
├── runtime.txt
├── LICENSE
└── README.md
The outputs/ directory is created automatically when the tutorial starts.
The tutorial uses R and the following packages:
learnrfor the self-paced tutorial environmentshinyfor the tutorial runtimeterrafor raster and vector processingsffor spatial vector objects and geometry operationsexactextractrfor fractional-coverage polygon extraction
A current R installation and RStudio are recommended for local use.
Run the following command in R:
install.packages(c(
"learnr",
"shiny",
"terra",
"sf",
"exactextractr"
))If the repository includes install.R, packages can instead be installed with:
source("install.R")Some spatial packages rely on system libraries. On Linux, installation may require GDAL, GEOS, PROJ, and related development libraries. A repository-provided Docker or Binder environment can avoid most local system-configuration issues.
git clone https://github.com/pspawar71/Phenomic_Tutorial.git
cd Phenomic_TutorialVerify that these paths exist:
data/rgb_orthomosaic.tif
data/plot_boundaries.geojson
Open the .Rmd tutorial file in RStudio and select Run Document, or run it from the R console:
learnr::run_tutorial("pawar/Plot_Level_GLI_Tutorial.Rmd")Replace the filename in the command if the tutorial file uses a different name.
The tutorial includes path-resolution logic for Binder environments where the project is stored in /home/pawar.
Launch the Phenomic Tutorial repository
Then run the following command in the RStudio Console:
learnr::run_tutorial("pawar/Plot_Level_GLI_Tutorial.Rmd")When using a temporary Binder session:
- Start the tutorial from the repository's RStudio environment.
- Complete the activities in order because later activities reuse files created earlier.
- Download the CSV and any raster outputs before closing the session.
- Remember that files created during a Binder session may be deleted after the session ends.
The tutorial writes intermediate and final products to the outputs/ directory:
outputs/
├── gli.tif
├── otsu_threshold.rds
├── vegetation_mask.tif
├── vegetation_gli.tif
└── plot_level_rgb_gli_traits.csv
gli.tif: Continuous pixel-level Green Leaf Index raster.otsu_threshold.rds: R object containing the calculated Otsu GLI threshold.vegetation_mask.tif: Binary raster in which 1 represents vegetation and 0 represents background.vegetation_gli.tif: GLI raster retaining only pixels classified as vegetation.plot_level_rgb_gli_traits.csv: Plot-level trait table for subsequent analysis.
The final CSV contains the following columns:
| Column | Description |
|---|---|
plot_id |
Plot identifier obtained from the polygon attribute table or generated automatically. |
mean_vegetation_gli |
Mean GLI among vegetation pixels within the plot, rounded to four decimal places. |
sd_vegetation_gli |
Standard deviation of vegetation GLI within the plot, rounded to four decimal places. |
canopy_cover_percent |
Percentage of the plot classified as vegetation, rounded to two decimal places. |
exactextractr accounts for fractional pixel coverage along plot boundaries during trait extraction.
Activities should be run in order. Several later activities read files created by earlier activities rather than recalculating them. For example:
- Activity 5 requires
gli.tiffrom Activity 4. - Activity 6 requires
gli.tifandotsu_threshold.rds. - Activity 7 requires
gli.tifandvegetation_mask.tif. - Activity 10 requires
vegetation_gli.tifandvegetation_mask.tif. - Activities 11 and 12 require the final trait CSV.
The plot polygons are automatically projected to the raster coordinate reference system when the two datasets do not match. Nevertheless, users should inspect both coordinate reference systems and verify that the overlay is spatially correct.
The setup specifies:
learnr::tutorial_options(
exercise.eval = FALSE,
exercise.timelimit = 300
)Individual raster-processing activities use longer limits where needed, including up to 600 seconds for plot-level trait extraction. Learners should execute the exercise code in the displayed environment or RStudio Console according to the deployed tutorial configuration.
The tutorial intentionally uses standard R graphics and printed tables. It does not use:
- Interactive raster maps
- Interactive vector maps
- Plotly charts
- Interactive data tables
This design reduces software complexity and helps the tutorial run reliably in local and Binder environments.
GLI is an RGB-based greenness indicator, not a direct measurement of plant biomass, yield, nitrogen status, or physiological performance. Its values can be affected by:
- Illumination and cloud conditions
- Shadows
- Soil and crop-residue background
- Camera exposure and white balance
- Orthomosaic processing
- Crop growth stage
- Weeds within plots
- Image spatial resolution
- Plot-boundary accuracy
Before using the extracted traits in research analyses:
- Visually inspect the RGB image, GLI raster, vegetation mask, and plot overlays.
- Confirm that the first three raster bands are truly Red, Green, and Blue.
- Check whether the Otsu threshold separates crop vegetation appropriately.
- Verify plot-boundary alignment at image edges and between neighboring plots.
- Compare image-derived values with field measurements when possible.
- Use consistent acquisition and processing settings when comparing dates, genotypes, or treatments.
Expected message:
Could not locate the tutorial data folder.
Confirm that both required files exist under the repository's data/ directory and that their filenames match exactly.
Use an RGB GeoTIFF with at least three raster layers. Confirm the band count with:
rgb <- terra::rast("data/rgb_orthomosaic.tif")
terra::nlyr(rgb)The setup generates sequential identifiers if no supported ID field is available. If a meaningful experimental identifier is required, add a unique plot_id field to plot_boundaries.geojson before running the tutorial.
Inspect their coordinate reference systems and extents:
terra::crs(rgb_raw)
terra::crs(plots_raw)
terra::ext(rgb_raw)
terra::ext(plots_raw)A dataset with an incorrect or missing coordinate reference system should be corrected at the source rather than assigned an assumed CRS without verification.
Otsu's method is data-driven and may not always separate crop vegetation perfectly. Inspect the GLI histogram and mask. Strong illumination differences, green weeds, crop residue, or unusual soil backgrounds may require additional preprocessing or a validated thresholding approach.
Confirm that the project directory is writable and that no output file is locked by another application. The tutorial creates outputs/ automatically and overwrites raster products when activities are rerun.
A practical teaching sequence is:
- Introduce RGB reflectance and the GLI equation.
- Ask learners to inspect the raster and predict which areas will have higher GLI.
- Compare the three image bands.
- Calculate GLI and inspect its histogram.
- Discuss how Otsu thresholding separates pixel classes.
- Evaluate the vegetation mask against the RGB image.
- Inspect plot geometry and attributes.
- Extract plot-level traits.
- Compare mean GLI, within-plot variability, and canopy cover.
- Discuss limitations and potential validation measurements.
If you use this tutorial in teaching, workshops, extension, or research, please cite the repository:
Pawar, P. S. From RGB Pixels to Plot-Level Crop Traits: A Self-Paced RGB Orthomosaic and Green Leaf Index Tutorial. GitHub repository. View the Phenomic Tutorial repository
Contributions are welcome. You can:
- Fork the repository.
- Create a feature branch.
- Add or revise an activity.
- Test the full workflow from a clean session.
- Submit a pull request describing the change.
You may also open an issue to report an error, suggest an additional trait, or recommend an instructional improvement.
See the repository's LICENSE file for the applicable terms of use, modification, and distribution.
Popat Shivaji Pawar
Hands-On Remote Sensing and High-Throughput Phenotyping
View the GitHub profile
If this tutorial supports your teaching or research, consider starring the repository and sharing it with learners working in remote sensing and high-throughput crop phenotyping.