Tree segmentation with R

Simple guide with R LiDAR point cloud processing [with code and implementation samples]

Yu Kai Him Otto
Forestree
3 min readOct 14, 2023

--

Canopy height model based segmentation in the sample ploted in 3d and 2d graph

Package “lidR”

Sample video for the Basic processing and tree point detection

You can install the R package with the following code

install.packages("lidR")

# Load the lidR package
library(lidR)

Read the LAS file with R

# Read the LAS file
las <- readLAS(<your lidar file>)

Noted that, the las can be further specifies with

las <- readLAS("file.las", select = "xyzi")
# By the select tag, can only select some of the .las file attributes for processing

In the select, imeans the intensity and a - scan angle, n - number of returns, r - return number, c - classification. (there are more in the documentry.

For example, choose the first return and classification = vegetation (3,4,5):

las <- readLAS("file.las", select = "-keep_first -keep_class 3 4 5")

For more details, u can run the, the r console will display all possible commend:

las <- readLAS("file.las", filter = "-help")

Generate a Canopy Height Model

# Generate a Canopy Height Model (CHM)
chm <- rasterize_canopy(las, 0.25, pitfree(subcircle = 0.1))

Then we will plot, detect the tree from the canopy height model:

# Define a function to calculate tree heights
f <- function(x) {x * 0.1 + 3}

# Define a sequence of tree heights
heights <- seq(0, 30, 5)

# Apply the function to the heights to get the tree widths
ws <- f(heights)

# Plot the relationship between heights and widths
plot(heights, ws, type = "l", ylim = c(0, 6))

# Locate tree tops using the height function
ttops <- locate_trees(las, lmf(f))

# Plot the Canopy Height Model (CHM)
plot(chm, col = height.colors(50))

# Plot the tree tops on top of the CHM
plot(sf::st_geometry(ttops), add = TRUE, pch = 3)

# get the location of the trees
ttops <- locate_trees(las, lmf(ws = 5))

# plot the point cloud
offsets <- plot(las, bg = "white", size = 3)
add_treetops3d(offsets, ttops)

For the individual segmentation, we can segment by the two resolution method or the Li’s method introduced in 2012:

Tree segmentation with various methods
# Read the LAS file
las <- readLAS("your path")

# Canopy height model
chm_p2r_05 <- rasterize_canopy(las, 0.5, p2r(subcircle = 0.2), pkg = "terra")

# Smoothed by median filter
kernel <- matrix(1, 3, 3)
chm_p2r_05_smoothed <- terra::focal(chm_p2r_05, w = kernel, fun = median, na.rm = TRUE)
ttops_chm_p2r_05_smoothed <- locate_trees(chm_p2r_05_smoothed, lmf(5))

# Apply the algorithm
algo <- dalponte2016(chm_p2r_05_smoothed, ttops_chm_p2r_05_smoothed)
las <- segment_trees(las, algo)

# Plot with height labels and tree crowns
plot(las, bg = "white", size = 4, color = "treeID", height = TRUE, treetops = TRUE)

Reference materials
https://cran.r-project.org/web/packages/lidR/lidR.pdf

Reference and Acknowledgments

This is a volunteering pilot study from the Team of Forestree, Remote Sensing and Forestry, used to study the close-ranging photogrammetry, image processing and computer vision.

--

--

Yu Kai Him Otto
Forestree

Student from Hong Kong, studying in Land Surveying and Geo-informatics, PolyU.