I am trying to create a canopy height map from LIDAR point cloud data (LAS format) but I get some jagged edges/holes in my results (see photo). Is there a parameter I can tweak to fill these in? I tried adding the subcircle parameter to pitfree but still have the holes/edges. Here is the code I am using:
library(lidR)
library(terra)
library(sf)
print("Calculating CHM")
las = readLAS(input)
nonnoise <- filter_poi(las, Classification != 18) # remove high noise
nlas <- normalize_height(nonnoise, knnidw())
chm <- rasterize_canopy(nlas, res = 1, algorithm = pitfree(subcircle = 0.15), pkg = "terra")
writeRaster(chm, sprintf('./out/%s_chm.tif', filename), overwrite=TRUE)
Edit: I tweaked my code to the following to fix this issue:
chm <- rasterize_canopy(nlas, res = 1, algorithm = pitfree(max_edge = c(3, 3), subcircle = 1.4))
fill.na <- function(x, i=5) { if (is.na(x)[i]) { return(mean(x, na.rm = TRUE)) } else { return(x[i]) }}
w <- matrix(9,9,9)
filled <- terra::focal(chm, w, fun = fill.na)
smoothed <- terra::focal(filled, w = matrix(1,3,3), fun = mean, na.rm = TRUE)