LASfile <- system.file("extdata", "MixedConifer.laz", package="lidR")
las <- readLAS(LASfile, select = "xyzi", filter = "-inside 481250 3812980 481300 3813050")
# =================
# point-cloud-based
# =================
# 5x5 m fixed window size
ttops <- locate_trees(las, lmf(5))
#plot(las) |> add_treetops3d(ttops)
# variable windows size
f <- function(x) { x * 0.07 + 3}
ttops <- locate_trees(las, lmf(f))
#plot(las) |> add_treetops3d(ttops)
# Very custom variable windows size
f <- function(x, y, z) { x * 0.07 + y * 0.01 + z}
ws_args <- list(x = "Z", y = "Intensity", z = 3)
ttops <- locate_trees(las, lmf(f, ws_args = ws_args))
# ============
# raster-based
# ============
chm <- rasterize_canopy(las, res = 1, p2r(0.15), pkg = "terra")
ttops <- locate_trees(chm, lmf(5))
plot(chm, col = height.colors(30))
plot(sf::st_geometry(ttops), add = TRUE, col = "black", cex = 0.5, pch = 3)
# variable window size
f <- function(x) { x * 0.07 + 3 }
ttops <- locate_trees(chm, lmf(f))
plot(chm, col = height.colors(30))
plot(sf::st_geometry(ttops), add = TRUE, col = "black", cex = 0.5, pch = 3)
Run the code above in your browser using DataLab