I am trying to use the ForestTools::mcws() function and segment_trees in R to segment a LiDAR point cloud using a CHM raster. However, when I try to run the function with the segment_trees() function from the lidR package, I get the following error :
Error: Invalid function provided as algorithm.
Here is the code I am using:
library(lidR)
library(ForestTools)
library(raster)
library(sf)
LASfile <- system.file("extdata", "Megaplot.laz", package="lidR")
las = readLAS(LASfile)
chm <- rasterize_canopy(las, 1, p2r(0.15), pkg = "raster")
vwf_fun <- function(x){x * 0.06 + 0.5}
WindowFilter <- vwf(CHM = chm, winFun = vwf_fun)
las <- segment_trees(las, ForestTools::mcws(WindowFilter, chm, minHeight = 2, format = "raster"))
I have checked the arguments I am passing to mcws(), and they seem to be correct. What could be causing this error, and how can I fix it?
ForestTools::mcwsis a function from the packageForestToolsand is not designed to be compatible withlidR.lidRcannot accept any function as input and the developers must respect some convention to make their function compatible withlidR.The function return a
rasterand thus can be made compatible withlidR. You can make the function compatible yourselfThen