Thanks for creating SegOptim! an excellent free product for remote sensing.
I have been running segmentations and classifications using segoptim over the past few months, but just recently, I have been running into an error with my data. When I downloaded the data from the tutorial (to make sure it wasn't a problem with my data), I am getting the same error.
The error relates to the prepareCalData function.
The segmentation seemed to work, and the three rasters used in the preparecaldata function appear to have the same extent and resolution, and look sensible when plotted.
But whenever I run prepareCalData, I get the following error:
Error : Column 2 is length 0 which differs from length of column 1 (2843) Error in getTrainData_(x = x, rstSegm = rstSegm, useThresh = useThresh, : An error occurred while calculating training data stats! Warning message: In prepareCalData(rstSegm = segmRst, trainData = trainDataRst, rstFeatures = classificationFeatures.path, : An error occurred while generating train data! Check segmentation parameter ranges? Perhaps input train data?
Changing the threshold and minimum image segment doesnt seem to fix the issue.
This did coincide with updating R and all packages, so I fear a rollback may be necessary. But then again, I wouldnt rule out a simple code error..
Any help would be hugely appreciated. I have copied the code and session info below.
Thanks in advance.
Mitch
#####code and session info.
library(raster)
library(randomForest)
library(SegOptim)
# Change this!!!
setwd("...../Desktop/test_data")
# Path to raster data used for image segmentation
# In SEGM_FEAT directory
inputSegFeat.path <- "./SEGM_FEAT/SegmFeat_WV2_b532.tif"
# Path to training raster data
# [0] Non-invaded areas [1] Acacia dealbata invaded areas
# In TRAIN_AREAS directory
trainData.path <- "./TRAIN_AREAS/TrainAreas.tif"
# Path to raster data used as classification features
# In CLASSIF_FEAT directory
classificationFeatures.path <- c("./CLASSIF_FEAT/ClassifFeat_WV2_NDIs.tif",
"./CLASSIF_FEAT/ClassifFeat_WV2_SpectralBands.tif")
# Path to Orfeo Toolbox binaries
otbPath <- "C:/qgis-plugins/OTB/bin/"
# Check if the files and folders exist!
if(!file.exists(inputSegFeat.path))
print("Could not find data for image segmentation!")
if(!file.exists(trainData.path))
print("Could not find training data!")
if(!(file.exists(classificationFeatures.path[1]) | file.exists(classificationFeatures.path[2])))
print("Could not find data for classification!")
if(!dir.exists(otbPath))
print("Could not find Orfeo Toolbox binaries!")
outSegmRst.path <- "./segmRaster.tif"
# The final output file containing the distribution of the target species
outClassRst.path <- "./WV2_VilarVeiga_AcaciaDealbata_v1.tif"
## To know more about the algorithm and its parameters use the help
?segmentation_OTB_LSMS
## Run the segmentation
outSegmRst <- segmentation_OTB_LSMS(
# Input raster with features/bands to segment
inputRstPath = inputSegFeat.path,
# Algorithm params
SpectralRange = 3.1,
SpatialRange = 4.5,
MinSize = 21,
# Output
outputSegmRst = outSegmRst.path,
verbose = TRUE,
otbBinPath = otbPath,
lsms_maxiter = 50)
# Check the file paths with outputs
print(outSegmRst)
# Load the segmented raster and plot it
segmRst <- raster(outSegmRst$segm)
# Train data
trainDataRst <- raster(trainData.path)
# Classification features
classificationFeatures <- stack(classificationFeatures.path)
# Change the names for each layer
names(classificationFeatures) <- c(paste("NDI_",1:28,sep=""),paste("SpecBand_",1:8,sep=""))
# Check help for details
?prepareCalData
trainDataRst
?prepareCalData()
calData <- prepareCalData(rstSegm = segmRst,
trainData = trainDataRst,
rstFeatures = classificationFeatures.path,
thresh = 0.5,
funs = "mean",
minImgSegm = 2,
verbose = TRUE)
###session info
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)
Matrix products: default
locale:
[1] LC_COLLATE=English_Australia.1252
[2] LC_CTYPE=English_Australia.1252
[3] LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] SegOptim_0.2.4 randomForest_4.7-1 raster_3.5-15
[4] sp_1.4-5
loaded via a namespace (and not attached):
[1] colorspace_2.0-2 ellipsis_0.3.2 class_7.3-19
[4] gdalUtils_2.0.3.2 clusterCrit_1.2.8 rgdal_1.5-28
[7] rprojroot_2.0.2 fs_1.5.0 rstudioapi_0.13
[10] proxy_0.4-26 listenv_0.8.0 remotes_2.4.2
[13] ParamHelpers_1.14 prodlim_2019.11.13 fansi_0.5.0
[16] lubridate_1.8.0 codetools_0.2-18 splines_4.1.2
[19] R.methodsS3_1.8.1 doParallel_1.0.17 mda_0.5-2
[22] pROC_1.18.0 caret_6.0-90 cluster_2.1.2
[25] R.oo_1.24.0 NLMR_1.1 compiler_4.1.2
[28] backports_1.4.1 assertthat_0.2.1 Matrix_1.4-0
[31] cli_3.1.1 prettyunits_1.1.1 tools_4.1.2
[34] gtable_0.3.0 glue_1.4.2 RANN_2.6.1
[37] reshape2_1.4.4 dplyr_1.0.7 doRNG_1.8.2
[40] fastmatch_1.1-3 Rcpp_1.0.7 parallelMap_1.5.1
[43] GA_3.2.2 vctrs_0.3.8 nlme_3.1-155
[46] iterators_1.0.14 timeDate_3043.102 gower_1.0.0
[49] mlr_2.19.0 librarian_1.8.1 stringr_1.4.0
[52] globals_0.14.0 ps_1.6.0 lifecycle_1.0.1
[55] rngtools_1.5.2 future_1.23.0 terra_1.5-17
[58] MASS_7.3-55 scales_1.1.1 ipred_0.9-12
[61] cclust_0.6-23 parallel_4.1.2 BBmisc_1.11
[64] curl_4.3.2 ggplot2_3.3.5 dtplyr_1.2.1
[67] rpart_4.1.16 stringi_1.7.6 foreach_1.5.2
[70] e1071_1.7-9 checkmate_2.0.0 pkgbuild_1.3.1
[73] lava_1.6.10 rlang_0.4.11 pkgconfig_2.0.3
[76] lattice_0.20-45 ROCR_1.0-11 lidR_3.2.3
[79] purrr_0.3.4 sf_1.0-6 recipes_0.1.17
[82] processx_3.5.2 tidyselect_1.1.1 parallelly_1.30.0
[85] gbm_2.1.8 plyr_1.8.6 magrittr_2.0.1
[88] R6_2.5.1 generics_0.1.2 DBI_1.1.2
[91] pillar_1.7.0 withr_2.4.3 units_0.8-0
[94] survival_3.2-13 nnet_7.3-17 tibble_3.1.3
[97] future.apply_1.8.1 crayon_1.5.0 unbalanced_2.0
[100] KernSmooth_2.23-20 utf8_1.2.2 grid_4.1.2
[103] data.table_1.14.2 FNN_1.1.3 callr_3.7.0
[106] ModelMetrics_1.2.2.2 digest_0.6.29 classInt_0.4-3
[109] R.utils_2.11.0 stats4_4.1.2 munsell_0.5.0