r-lidar / lidR

Airborne LiDAR data manipulation and visualisation for forestry application
https://CRAN.R-project.org/package=lidR
GNU General Public License v3.0
582 stars 130 forks source link

Error in watershed algoritm #760

Closed aitorhazi closed 2 months ago

aitorhazi commented 3 months ago

Hi,

when I use the watershed algorithm to segment the trees it reports the following error:

Error in validImage(x) : object must be an array

I have converted the object into an array and when I run the watershed algorithm again it reports the following error:

Error: Invalid function provided as algorithm.

My code is:

nlas <- readLAS("C:/Users/5075-47595.laz",  filter = "-set_withheld_flag 0")
las <- normalize_height(nlas, knnidw())
col <- height.colors(50)
chm <- rasterize_canopy(las = las, res = 0.5, algorithm = p2r(0.15))
kernel <- matrix(1, 3, 3)
schm <- terra::focal(x = chm, w = kernel, fun = median, na.rm = TRUE)
algo <-  watershed(schm, th = 4)
SEGtrees  <-  segment_trees(las, algo)

I have the EBImage package installed and the R version is 4.4.0

How can I overcome this obstacle?

Thanks.

Jean-Romain commented 3 months ago

I ran this without issue. Please report a minimal reproducible example including data if you can't reproduce with embedded datasets

LASfile <- system.file("extdata", "Megaplot.laz", package="lidR")
nlas = readLAS(LASfile)
las <- normalize_height(nlas, knnidw())
col <- height.colors(50)
chm <- rasterize_canopy(las = las, res = 0.5, algorithm = p2r(0.15))
kernel <- matrix(1, 3, 3)
schm <- terra::focal(x = chm, w = kernel, fun = median, na.rm = TRUE)
algo <-  watershed(schm, th = 4)
SEGtrees  <-  segment_trees(las, algo)
aitorhazi commented 3 months ago

Hi, I am attaching a screenshot with the workflow where the error appears. I am also attaching below a link where you can download the .laz file used. https://we.tl/t-D0iILYRaJN Thank you so much. lidR_issue

Jean-Romain commented 2 months ago

The link expired

aitorhazi commented 2 months ago

I attach it again.

https://we.tl/t-GwKzZzidKK

The link expires in a week.

Thanks.

Jean-Romain commented 2 months ago

Ok I get the files thanks.

Jean-Romain commented 2 months ago

I ran

library(lidR)

nlas <- readLAS("~/Téléchargements/issue 760/5075-47595.laz",  filter = "-set_withheld_flag 0")
las <- normalize_height(nlas, knnidw())
col <- height.colors(50)
chm <- rasterize_canopy(las = las, res = 0.5, algorithm = p2r(0.15))
kernel <- matrix(1, 3, 3)
schm <- terra::focal(x = chm, w = kernel, fun = median, na.rm = TRUE)
algo <-  watershed(schm, th = 4)
SEGtrees  <-  segment_trees(las, algo)

I worked. Please report more information.