-
Guillaume Perréal authoreda73e273c
- Material
- Field inventory
- Airborne Laser Scanning data
- Data preparation
- Digital Elevation Models
- Visual comparison of field inventory and ALS data
- Tree delineation
- Tree segmentation
- Tree extraction
- Detection evaluation
- Tree matching
- Detection accuracy
- Height estimation accuracy
- Species Classification
- Points in segments
- Metrics computation
- Merge with reference and detected trees
- Exploratory analysis
- Display point cloud
- Batch processing
- References
title: "R workflow for tree segmentation from ALS data"
author: "Jean-Matthieu Monnet"
date: "`r Sys.Date()`"
output:
html_document: default
pdf_document: default
papersize: a4
bibliography: "../bib/bibliography.bib"
# erase all
cat("\014")
rm(list = ls())
# knit options
knitr::opts_chunk$set(echo = TRUE)
# Set so that long lines in R will be wrapped:
knitr::opts_chunk$set(tidy.opts = list(width.cutoff = 80), tidy = TRUE)
knitr::opts_chunk$set(fig.align = "center")
# for display of rgl in html
knitr::knit_hooks$set(webgl = rgl::hook_webgl)
# output to html
html <- TRUE
The code below presents a tree segmentation workflow from Airborne Laser Scanning (lidar remote sensing) data. The workflow is based on functions from R packages lidaRtRee
and lidR
, and it includes the following steps:
- treetop detection,
- crown segmentation,
- accuracy assessment with field inventory,
- species classification
Steps 1 and 3 are documented in [@Monnet10; @Monnet11c]. The detection performance of this algorithm was evaluated in a benchmark [@Eysn15].
Licence: GNU GPLv3 / source page Many thanks to Pascal Obstétar for checking code and improvement suggestions.
Material
Field inventory
The field inventory corresponds to a 50m x 50m plot located in the Chablais mountain (France) [@Monnet11c, pp. 34]. All trees with a diameter at breast height above 7.5 cm are inventoried. The data is available in package lidaRtRee
.
# load dataset from package (default)
data(tree_inventory_chablais3, package = "lidaRtRee")
Otherwise you can load your own data provided positions and heights are measured.
# import field inventory
fichier <- "chablais3_listeR.csv"
tree.inventory <- read.csv(file = fichier, sep = ";", header = F, stringsAsFactors = TRUE)
names(tree_inventory_chablais3) <- c("x", "y", "d", "h", "n", "s", "e", "t")
# save as rda for later access
# save(tree_inventory_chablais3,file="tree_inventory_chablais3.rda")
Attributes are:
-
x
easting coordinate in Lambert 93
-
y
northing coordinate in Lambert 93 -
d
diameter at breast height (cm) -
h
tree height (m) -
n
tree number -
s
species abreviated as GESP (GEnus SPecies) -
e
appearance (0: missing or lying, 1: normal, 2: broken treetop, 3: dead with branches, 4: snag) -
t
tilted (0: no, 1: yes)
head(tree_inventory_chablais3, n = 3L)
Function plot_tree_inventory
is designed to plot forest inventory data.
# display inventoried trees
lidaRtRee::plot_tree_inventory(tree_inventory_chablais3
[, c("x", "y")],
tree_inventory_chablais3$h,
species = as.character(tree_inventory_chablais3$s)
)
The ggplot2
package also provides nice outputs.
# use table of species of package lidaRtRee to always use the same color for a given species
plot.species <- lidaRtRee::species_color()[levels(tree_inventory_chablais3$s), "col"]
library(ggplot2)
ggplot(tree_inventory_chablais3, aes(x = x, y = y, group = s)) +
geom_point(aes(color = s, size = d)) +
coord_sf(datum = 2154) +
scale_color_manual(values = plot.species) +
scale_radius(name = "Diameter") +
geom_text(aes(label = n, size = 20), hjust = 0, vjust = 1) +
labs(color = "Species") # titre de la légende
We define the region of interest (ROI) to crop ALS data to corresponding extent before further processing. ROI is set on the extent of tree inventory, plus a 10 meter buffer.
# buffer to apply around ROI (meters)
ROI_buffer <- 10
# ROI limits
ROI_range <- data.frame(round(apply(tree_inventory_chablais3[, c("x", "y")], 2, range)))
Airborne Laser Scanning data
In this tutorial, ALS data available in the lidaRtRee
package is used.
# load data in package lidaRtRee (default)
data(las_chablais3, package = "lidaRtRee")
las_chablais3
Otherwise, ALS data is loaded with functions of package lidR
. First a catalog of files containing ALS data is built. Then points located inside our ROI are loaded.
# directory for laz files
lazdir <- "../data/tree.detection"
# build catalog of files
# specifying ALS data
cata <- lidR::readALSLAScatalog(lazdir)
# set coordinate system
lidR::projection(cata) <- 2154
# extract points in ROI plus additional buffer
las_chablais3 <- lidR::clip_rectangle(
cata,
ROI_range$x[1] - ROI_buffer - 5,
ROI_range$y[1] - ROI_buffer - 5,
ROI_range$x[2] + ROI_buffer + 5,
ROI_range$y[2] + ROI_buffer + 5
)
# save as rda for easier access:
# save(las_chablais3, file="las_chablais3.rda", compress = "bzip2")
Data preparation
Digital Elevation Models
From the ALS point cloud, digital elevation models are computed [@Monnet11c, pp. 43-46]. The Digital Terrain Model (DTM) represents the ground surface, it is computed by bilinear interpolation of points classified as ground. The Digital Surface Model (DSM) represents the canopy surface, it is computed by retaining in each raster's pixel the value of the highest point included in that pixel. The Canopy Height Model (CHM) is the normalized height of the canopy. It is computed by subtracting the DTM to the DSM.
# terrain model computed from points classified as ground
dtm <- lidaRtRee::points2DTM(lidR::filter_ground(las_chablais3),
res = 0.5, ROI_range$x[1] - ROI_buffer,
ROI_range$x[2] + ROI_buffer, ROI_range$y[1] - ROI_buffer,
ROI_range$y[2] + ROI_buffer
)
# surface model
dsm <- lidaRtRee::points2DSM(las_chablais3,
res = 0.5, dtm@extent@xmin,
dtm@extent@xmax, dtm@extent@ymin, dtm@extent@ymax
)
# canopy height model
chm <- dsm - dtm
par(mfrow = c(1, 3))
# display DTM
raster::plot(dtm, main = "DTM")
# display DSM
raster::plot(dsm, main = "DSM")
# display CHM
raster::plot(chm, main = "CHM")
Visual comparison of field inventory and ALS data
A plot mask is computed from the inventoried positions, using a height-dependent buffer. Indeed, tree tops are not necessairily located verticaly above the trunk positions. In order to compare detected tree tops with inventoried trunks, buffers have to be applied to trunk positions to account for the non-verticality of trees.
# select trees with height measures
selec <- which(!is.na(tree_inventory_chablais3$h))
# plot mask computation based on inventoried positions
# convex hull of plot
mask_chull <- lidaRtRee::raster_chull_mask(tree_inventory_chablais3[selec, c("x", "y")], dsm)
# union of buffers around trees
mask_tree <- lidaRtRee::raster_xy_mask(
tree_inventory_chablais3[selec, c("x", "y")],
2.1 + 0.14 * tree_inventory_chablais3$h[selec], dsm
)
# union of convexHull and tree buffers
mask_plot <- max(mask_chull, mask_tree)
# vectorize plot mask
mask_plot_v <- raster::rasterToPolygons(mask_plot, function(x) (x == 1), dissolve = T)
Displaying inventoried trees on the CHM shows a pretty good correspondance of crowns visible in the CHM with trunk locations and sizes.
# display CHM
raster::plot(chm,
col = gray(seq(0, 1, 1 / 255)),
main = "Canopy Height Model and tree positions"
)
# add inventoried trees
lidaRtRee::plot_tree_inventory(tree_inventory_chablais3[, c("x", "y")],
tree_inventory_chablais3$h,
species = as.character(tree_inventory_chablais3$s), add = TRUE
)
# display plot mask
raster::plot(mask_plot_v, border = "red", add = TRUE)
Tree delineation
Tree segmentation
Tree segmentation is performed on the Canopy Height Model by using a general function (tree_segmentation
) which consists in the following steps:
- image pre-processing (non-linear filtering and smoothing for noise removal)
- local maxima filtering and selection for tree top detection
- image segmentation with a watershed algorithm for tree delineation.
The first two steps are documented in @Monnet11c, pp. 47-52.
# tree detection (default settings), applied on canopy height model
segms <- lidaRtRee::tree_segmentation(chm)
#
par(mfrow = c(1, 3))
# display pre-processed chm
raster::plot(segms$smoothed_dem, main = "Pre-processed CHM")
# display selected local maxima
raster::plot(segms$local_maxima, main = "Selected local maxima")
# display segments, except ground segment
dummy <- segms$segments_id
dummy[dummy == 0] <- NA
raster::plot(dummy %% 8, main = "Segments (random colors)", col = rainbow(8), legend = FALSE)
Tree extraction
A data.frame of detected trees located in the plot mask is then extracted with the function tree_extraction.
Segments can be converted from raster to polygons but the operation is quite slow. Attributes are :
-
id
: tree id
-
x
: easting coordinate of tree top -
y
: northing coordinate of tree top -
h
: height of tree top -
dom_radius
: distance of tree top to nearest crown of neighbouring tree with larger height -
s
: crown surface -
v
: crown volume -
sp
: crown surface inside plot -
vp
: crown volume inside plot
# tree extraction only inside plot mask for subsequent comparison
trees <- lidaRtRee::tree_extraction(
segms$filled_dem,
segms$local_maxima,
segms$segments_id, mask_plot
)
head(trees, n = 3L)
# convert segments from raster to polygons
segments_v <- raster::rasterToPolygons(segms$segments_id, dissolve = T)
#
# display initial image
raster::plot(chm, col = gray(seq(0, 1, 1 / 255)), main = "CHM and detected positions")
# display segments border
sp::plot(segments_v, border = "white", add = T)
# display plot mask
sp::plot(mask_plot_v, border = "red", add = T)
# display inventoried trees
graphics::points(trees$x, trees$y, col = "blue", cex = trees$h / 20, pch = 2)
Detection evaluation
Tree matching
To assess detection accuracy, reference (field) trees should be linked to detected trees. Despite the possibility of error, automated matching has the advantage of making the comparison of results from different algorithms and settings reproducible and objective. The algorithm presented below is based on the 3D distance between detected treetops and inventory positions and heights [@Monnet10].
# match detected treetops with field trees based on relative distance of apices
matched <- lidaRtRee::tree_matching(
tree_inventory_chablais3[selec, c("x", "y", "h")],
cbind(trees@coords, trees$h)
)
# display matching results
lidaRtRee::plot_matched(
tree_inventory_chablais3[selec, c("x", "y", "h")],
cbind(trees@coords, trees$h), matched, chm, mask_plot_v
)
Detection accuracy
# height histogram of detections
detection_stats <- lidaRtRee::hist_detection(
tree_inventory_chablais3[selec, c("x", "y", "h")],
cbind(trees@coords, trees$h), matched
)
Detection accuracy is evaluated thanks to the number of correct detections (r detection_stats$true_detections
), false detections (r detection_stats$false_detections
) and omissions (r detection_stats$omissions
). In heterogeneous stands, detection accuracy is height-dependent, it is informative to display those categories on a height histogram.
# height histogram of detections
detection_stats <- lidaRtRee::hist_detection(
tree_inventory_chablais3[selec, c("x", "y", "h")],
cbind(trees@coords, trees$h), matched
)
Height estimation accuracy
height_reg <- lidaRtRee::height_regression(tree_inventory_chablais3[selec, c("x", "y", "h")],
cbind(trees@coords, trees$h),
matched,
species = tree_inventory_chablais3$s
)
For true detections, estimated heights can be compared to field-measured heights. Here, the root mean square error is r round(height_reg$stats$rmse,1)
m, while the bias is r round(height_reg$stats$bias,1)
m. The linear regression is displayed hereafter.
# linear regression between reference height and estimated height
height_reg <- lidaRtRee::height_regression(tree_inventory_chablais3[selec, c("x", "y", "h")],
cbind(trees@coords, trees$h),
matched,
species = tree_inventory_chablais3$s
)
Species Classification
Points in segments
Before computation of point cloud metrics in each segment, the whole point cloud is normalized to convert point altitude to height above ground. Points are then labeled with the id of the segment they belong to. A list of LAS objects corresponding to the segments is then created.
# normalize point cloud
lasn <- lidR::normalize_height(las_chablais3, lidR::tin())
# add segment id in LAS object
lasn <- lidR::add_attribute(lasn, raster::extract(segms$segments_id, lasn@data[, 1:2]), "seg_id")
# split las object by segment id
las_l <- split(lasn@data, lasn@data$seg_id)
# convert list of data.frames to list of las objects
las_l <- lapply(las_l, function(x) {
lidR::LAS(x, lasn@header)
})
# set coordinate system
for (i in 1:length(las_l)) {
lidR::projection(las_l[[i]]) <- 2154
}
Metrics computation
Basic point cloud metrics are computed in each segment (maximum, mean, standard deviation of heights, mean and standard deviation of intensity).
# compute basic las metrics in each segment
metrics <- lidaRtRee::clouds_metrics(las_l, func = ~ list(
maxZ = max(Z), meanZ = mean(Z),
sdZ = sd(Z), meanI = mean(Intensity),
sdI = sd(Intensity)
))
# create segment id attribute
metrics$seg_id <- row.names(metrics)
head(metrics, n = 3L)
Merge with reference and detected trees
Computed metrics are merged with reference and detected trees, thanks to the segment id.
# associate each reference tree with the segment that contains its trunk.
tree_inventory_chablais3$seg_id <- raster::extract(
segms$segments_id,
tree_inventory_chablais3[, c("x", "y")]
)
# create new data.frame by merging metrics and inventoried trees based on segment id
tree_metrics <- base::merge(tree_inventory_chablais3, metrics)
# remove non-tree segment
tree_metrics <- tree_metrics[tree_metrics$seg_id != 0, ]
# add metrics to extracted tree data.frame
trees <- base::merge(trees, metrics, by.x = "id", by.y = "seg_id", all.x = T)
Plotting the reference trees with the mean intensity of lidar points in the segments shows that when they are dominant, broadleaf trees tend to have higher mean intensity than coniferous trees. .
# create raster of segment' mean intensity
r_mean_intensity_segm <- segms$segments_id
# match segment id with id in metrics data.frame
dummy <- match(raster::values(r_mean_intensity_segm), trees$id)
# replace segment id with corresponding mean intensity
raster::values(r_mean_intensity_segm) <- trees$meanI[dummy]
# display tree inventory with mean intensity in segment background
raster::plot(r_mean_intensity_segm, main = "Mean intensity in segment")
# display tree inventory
lidaRtRee::plot_tree_inventory(tree_inventory_chablais3[, c("x", "y")],
tree_inventory_chablais3$h,
species = as.character(tree_inventory_chablais3$s), add = T
)
Exploratory analysis
A boxplot of mean intensity in segments per species shows that mean intensity distribution is different between species. The analysis can be run by considering only the highest inventoried trees in each segment, as smaller trees are likely to be suppressed and have smaller contribution to the point cloud.
# create new variable orderer by segment id then decreasing height
tree_metrics_h <- tree_metrics[order(tree_metrics$seg_id, -tree_metrics$h), ]
i <- 2
# leave only first (highest) tree per segment id
while (i < nrow(tree_metrics_h)) {
if (tree_metrics_h$seg_id[i] == tree_metrics_h$seg_id[i + 1]) {
tree_metrics_h <- tree_metrics_h[-(i + 1), ]
} else {
i <- i + 1
}
}
tree_metrics_h$s <- factor(tree_metrics_h$s)
par(mfrow = c(1, 2))
boxplot(meanI ~ s,
data = tree_metrics[, c("s", "maxZ", "meanZ", "sdZ", "meanI", "sdI")],
ylab = "Mean intensity in segment", xlab = "Specie",
main = "All inventoried trees", las = 2, varwidth = TRUE
)
boxplot(meanI ~ s,
data = tree_metrics_h, ylab = "Mean intensity in segment",
xlab = "Specie", main = "Highest inventoried tree in segment", las = 2,
varwidth = TRUE
)
A linear discriminant analysis shows that main species can be discriminated, thanks to a combination of height and intensity variables.
acp2 <- ade4::dudi.pca(tree_metrics_h[, c("maxZ", "meanZ", "sdZ", "meanI", "sdI")], scannf = F)
afd <- ade4::discrimin(acp2, tree_metrics_h$s, scannf = F, nf = 2)
plot(afd)
Display point cloud
The point cloud can be displayed colored by segment, with poles at the location of inventoried trees.
rgl::par3d(mouseMode = "trackball") # parameters for interaction with mouse
# select segment points and offset them to avoid truncated coordinates in 3d plot
points.seg <- lasn@data[which(lasn@data$seg_id != 0), c("X", "Y", "Z", "seg_id")]
points.seg$X <- points.seg$X - 974300
points.seg$Y <- points.seg$Y - 6581600
# plot point cloud
rgl::plot3d(points.seg[, c("X", "Y", "Z")], col = points.seg$seg_id %% 10 + 1, aspect = FALSE)
#
# add inventoried trees
tree_inventory_chablais3$z <- 0
for (i in 1:nrow(tree_inventory_chablais3))
{
rgl::lines3d(
rbind(
tree_inventory_chablais3$x[i] - 974300,
tree_inventory_chablais3$x[i] - 974300
),
rbind(
tree_inventory_chablais3$y[i] - 6581600,
tree_inventory_chablais3$y[i] - 6581600
),
rbind(
tree_inventory_chablais3$z[i],
tree_inventory_chablais3$z[i] + tree_inventory_chablais3$h[i]
)
)
}
# Inventoried trees
# Using package rLiDAR, a 3d view of the field inventory can be displayed
# shape different between coniferous / deciduous
rgl::rgl.open()
rgl::rgl.bg(color = "white")
for (j in 1:length(selec))
{
i <- selec[j]
if (is.na(tree_inventory_chablais3$h[i]) |
is.na(tree_inventory_chablais3$d[i])) {
next
}
if (!is.element(
as.character(tree_inventory_chablais3$s[i]),
c("ABAL", "PIAB", "TABA")
)) {
rLiDAR::LiDARForestStand(
crownshape = "halfellipsoid",
CL = 0.6 * tree_inventory_chablais3$h[i],
CW = tree_inventory_chablais3$h[i] / 4,
HCB = 0.4 * tree_inventory_chablais3$h[i],
dbh = tree_inventory_chablais3$d[i] / 50,
resolution = "high",
X = tree_inventory_chablais3$x[i],
Y = tree_inventory_chablais3$y[i],
mesh = F
)
} else {
rLiDAR::LiDARForestStand(
crownshape = "cone",
CL = 0.5 * tree_inventory_chablais3$h[i],
CW = tree_inventory_chablais3$h[i] / 4,
HCB = 0.5 * tree_inventory_chablais3$h[i],
dbh = tree_inventory_chablais3$d[i] / 50,
resolution = "high",
X = tree_inventory_chablais3$x[i],
Y = tree_inventory_chablais3$y[i],
mesh = F,
stemcolor = "burlywood4",
crowncolor = "darkgreen"
)
}
}
# virtual trees from detection
# shape different based on mean lidar intensity value (threshold 55)
library(rLiDAR)
rgl::rgl.open()
rgl::rgl.bg(color = "white")
for (i in 1:nrow(trees))
{
if (trees$meanI[i] > 55) {
rLiDAR::LiDARForestStand(
crownshape = "halfellipsoid",
CL = 0.6 * trees$h[i],
CW = sqrt(4 * trees$s[i] / pi),
HCB = 0.4 * trees$h[i],
dbh = trees$h[i] / 50,
resolution = "high",
X = trees$x[i],
Y = trees$y[i],
mesh = F
)
} else {
rLiDAR::LiDARForestStand(
crownshape = "cone",
CL = 0.5 * trees$h[i],
CW = sqrt(4 * trees$s[i] / pi),
HCB = 0.5 * trees$h[i],
dbh = trees$h[i] / 50,
resolution = "high",
X = trees$x[i],
Y = trees$y[i],
mesh = F,
stemcolor = "burlywood4",
crowncolor = "darkgreen"
)
}
}
Batch processing
The following code exemplifies how to process numerous LAS files and extract trees for the whole area with parallel processing. The processing runs faster if data is provided as chunks to the segmentation algorithm, and results are then aggregated, rather than running on the full coverage of the data. In order to avoid border effects, chunks are provided to the algorithm as overlapping tiles. Results are cropped to prevent the same tree from appearing twice in the final results. Tile size and buffer size are important parameters :
- tile size is a trade-off between the number of chunks to process and the amount of RAM required to process a single tile ;
- buffer size is a trade-off between redundant processing of the overlap area, and assuring that a tree is which treetop is located at the border of a tile has its full crown within the buffer size.
Tiles can be processed with parallel computing, within limits of the cluster's RAM and number of cores.
The steps for processing a batch of las/laz files are :
- build catalog of files
- provide tiling parameters
- provide segmentation parameters
- provide output parameters
- set cluster options for parallel processing
- compute the X and Y coordinates of tiles
- parallelize the processing with the buil-in package
parallel
, by sending to the clusters the coordinates of tiles to process, and a function with the instructions to proceed :- load tile corresponding to the coordinates
- compute CHM
- segment and extract trees
- aggregate list of results
Be aware that in case tree segments are vectorized, some obtained polygons might overlap. The segmentation algorithm might not be deterministic and borders are sometimes not consistent when adjacent polygons are pasted from different tiles.
rm(list = ls())
# BUILD CATALOG OF FILES
# folder containing the files
lazdir <- "../data/forest.structure.metrics"
# build catalog
cata <- lidR::readALSLAScatalog(lazdir)
# set coordinate system
lidR::projection(cata) <- 2154
#
# BATCH PROCESSING PARAMETERS
# tile size to split area into chunks to process
# trade-off between RAM capacity VS total number of tiles to process
tile_size <- 70 # here 70 for example purpose with small area
# buffer size: around each tile to avoid border effects on segmentation results
# trade-off between making sure a whole tree crown is processed in case its top
# is on the border VS duplicate processing
buffer_size <- 10 # 5 m is minimum, 10 is probably better depending on tree size
#
# TREE SEGMENTATION PARAMETERS
# set raster resolution
resolution <- 1
#
# OUTPUT PARAMETERS
# option to vectorize tree crowns (set to FALSE if too slow)
out_vectorize_trees <- TRUE
# output canopy height models ? (set to FALSE if too much RAM used)
out_chm <- TRUE
# save chms on disk
out_save_chm <- FALSE
#
# CLUSTER PARAMETERS
# working directory
wd <- getwd()
# run with two cores
clust <- parallel::makeCluster(getOption("cl.cores", 2), outfile = "~/output.txt")
# export global variables to cluster because they will be called inside the function
parallel::clusterExport(cl = clust, ls(), envir = .GlobalEnv)
#
# COORDINATES OF TILES
# low left corner
x <- seq(
from = floor(cata@bbox["x", "min"] / tile_size) * tile_size,
by = tile_size,
to = ceiling(cata@bbox["x", "max"] / tile_size - 1) * tile_size
) # [1:2]
length_x <- length(x)
y <- seq(
from = floor(cata@bbox["y", "min"] / tile_size) * tile_size,
by = tile_size,
to = ceiling(cata@bbox["y", "max"] / tile_size - 1) * tile_size
) # [1:2]
length_y <- length(y)
# repeat coordinates for tiling while area
x <- as.list(rep(x, length_y))
y <- as.list(rep(y, each = length_x))
#
# PARALLEL PROCESSING
# function takes coordinates of tile as arguments
resultats <- parallel::mcmapply(
# i and j are the coordinated of the low left corner of the tile to process
FUN = function(i, j) {
# set working directory
setwd(wd)
# initialize output
output <- list()
output$name <- paste0(i, "_", j)
# extract tile plus buffer
las_tile <- lidR::clip_rectangle(
cata,
i - buffer_size,
j - buffer_size,
i + tile_size + buffer_size,
j + tile_size + buffer_size
)
# check if points are present
if (nrow(las_tile@data) > 0) {
# normalization if required
# las_tile <- lidR::normalize_height(las_tile, lidR::tin())
# in this example LAS tiles are already normalized
# compute canopy height model
chm <- lidaRtRee::points2DSM(las_tile)
# check all chm is not NA
if (!all(is.na(raster::values(chm)))) {
# output chm if asked for
if (out_chm) {
output$chm <- raster::crop(chm, raster::extent(
i, i + tile_size,
j, j + tile_size
))
}
# save on disk
if (out_save_chm) {
raster::writeRaster(raster::crop(
chm,
raster::extent(
i, i + tile_size,
j, j + tile_size
)
),
file = paste0("chm_", i, "_", j, ".tif"),
overwrite = TRUE
)
}
#
# tree detection
segms <- lidaRtRee::tree_segmentation(chm)
# tree extraction
trees <- lidaRtRee::tree_extraction(
segms$filled_dem,
segms$local_maxima,
segms$segments_id
)
# remove trees in buffer area
trees <- trees[trees$x >= i & trees$x < i + tile_size &
trees$y >= j & trees$y < j + tile_size, ]
# add tile id to trees to avoid duplicates in final file
trees$tile <- paste0(i, "_", j)
# convert to vectors if option is TRUE
if (out_vectorize_trees) {
# vectorize
trees_v <- raster::rasterToPolygons(segms$segments_id, dissolve = T)
# remove polygons which treetop is in buffer
trees_v <- trees_v[is.element(trees_v$segments_id, trees$id), ]
# names(trees_v) <- "id"
# add attributes
# errors when using sp::merge so using sp::over even if it is probably slower
# merge(trees_v@data, trees, all.x = TRUE)
trees_v@data <- cbind(trees_v@data, sp::over(trees_v, trees))
# save in list
output$trees_v <- trees_v
}
# save trees in list
output$trees <- trees
} # end of raster is not all NAs check
} # end of nrow LAS check
output
}, x, y, SIMPLIFY = FALSE
) # function applied to the lists of coordinates (x and y)
parallel::stopCluster(cl = clust)
#
# RESULTS AGGREGATION
# extract results from nested list into separate lists and then bind data
id <- unlist(lapply(resultats, function(x) x[["name"]]))
#
# trees
trees <- lapply(resultats, function(x) x[["trees"]])
# remove NULL elements
trees[sapply(trees, is.null)] <- NULL
# bind remaining elements
trees <- do.call(rbind, trees)
#
# chm
if (out_chm) {
chm <- lapply(resultats, function(x) x[["chm"]])
# merge chm
# no names in list otherwise do.call returns an error
chm_all <- do.call(raster::merge, chm)
names(chm) <- id
}
# trees_v
if (out_vectorize_trees) {
trees_v <- lapply(resultats, function(x) x[["trees_v"]])
# remove NULL elements
trees_v[sapply(trees_v, is.null)] <- NULL
trees_v <- do.call(rbind, trees_v)
# 1-pixel overlapping in trees_v might be present because image segmentation
# is not fully identical in overlap areas of adjacent tiles.
}
The following image displays the results for the whole area.
# threshold outsiders in chm
chm_all[chm_all > 40] <- 40
chm_all[chm_all < 0] <- 0
# display chm
raster::plot(chm_all,
main = "Canopy Height Model and segmented trees"
)
# display segments border
sp::plot(trees_v, border = "white", add = T)
# add trees
sp::plot(trees, cex = trees$h / 40, add = TRUE, pch = 2)
The following lines save outputs to files.
# merged chm
raster::writeRaster(chm_all, file = "chm.tif", overwrite = TRUE)
# trees
sf::st_write(sf::st_as_sf(trees), 'trees_points.gpkg', 'trees_points', delete_dsn = T)
# vectorized trees
if (out_vectorize_trees) sf::st_write(sf::st_as_sf(trees_v), 'v_trees_points.gpkg', 'v_trees_points', delete_dsn = T)