Error on image segmentation steps

70 views
Skip to first unread message

laura.alonso.martinez@¶uvigo.es

unread,
Apr 22, 2020, 11:40:14 AM4/22/20
to SegOptim user group

Dear Joao,

 

I have being performing pixel based classifications in R but recently I realised that in some ocasions it could be more interesting performing object based ones. I have found your package really usefull to non experts users as it allows to apply OBIA without having to learn to use new programing interfaces. However following the tutorial I'm not able to run a single segmentation approach, I'm obtaining different errors. 

I'm performing the classification on a small crop of a raster brick of 4 Sentinel 2 bands (B2,B3,B4 and B8A) as a test dataset.

 

Using the OTB toolbox, no mather which numbers I set as SpectralRange, SpatialRange and MinSize it always appear this error:

 

> outSegmRst <- segmentation_OTB_LSMS(

+   

+   inputRstPath = "D:/Mapa_forestal/WorldView/multilayer2.tif", 

+   SpectralRange=1,

+   SpatialRange=5, 

+   MinSize=20,

+   outputSegmRst = "D:/Mapa_forestal/WorldView/OBIA_test/test.tif",

+   verbose = T,

+   otbBinPath = otbBinPath)

SpectralRange=1

SpatialRange=5

MinSize=20

> 

 

Using the GRASS it appears a error in the first step of creating a grass database: On my understanding the GRASS.GISDBASE parameter is already a character vector.

 

> lista3<-list(B02crop,B03crop,B04crop,B8Acrop)

> importToGRASS(fileList=lista3, 

+               GRASS.path = GRASS.path,

+               GRASS.GISDBASE = "base.db",

+               GRASS.LOCATION_NAME="D:/Mapa_forestal/WorldView/OBIA_test", 

+               GRASS.MAPSET = "PERMANENT",

+               addParams = "",

+               verbose = TRUE)

Error in basename(fin) : a character vector argument expected

> 

 

Using the SAGA segmentation:

> segmObj <- segmentation_SAGA_SRG(rstList=lista2,

+                                  outputSegmRst="D:/Mapa_forestal/WorldView/OBIA_test/test.sgrd",

+                                  Bandwidth=2,

+                                GaussianWeightingBW=5,

+                                  VarFeatSpace=3,

+                                  VarPosSpace=2,

+                                  seedType=0,

+                                  method1=0,

+                                  DWeighting=3,

+                                  normalize=0,

+                                  neighbour=0,

+                                  method2=0,

+                                  thresh=0,

+                                  leafSize=1024,

+                                  SAGApath=env,

+                                  verbose=TRUE)

Bandwidth=2

GaussianWeightingBW=5

VarFeatSpace=3

VarPosSpace=2

Error in system(command, as.integer(flag), f, stdout, stderr, timeout) :

  character string expected as first argument

>

Using the RSGISLib segmentation:

> segmObj <- segmentation_RSGISLib_Shep(inputRstPath="D:/Mapa_forestal/WorldView/multilayer2.tif",

+                                       outputSegmRst="D:/Mapa_forestal/WorldView/OBIA_test/test.tif",

+                                       NumClust=10,

+                                       MinSize=20,

+                                       SpectralThresh=10,

+                                       pythonPath=pythonPath,

+                                       verbose=TRUE)

NumClust=10

MinSize=20

SpectralThresh=10

Warning message:

In shell(tmpBatchFile, ignore.stdout = TRUE, ignore.stderr = TRUE,  :

  'C:/Users/Laura aM/Documents/RSGISLib_Shep_Segm_mo4pol91.bat' execution failed with error code 1

>

 Thanks in advance. Best regards,


Laura


Full script, following, just in case it is needed:

library(SegOptim)

library(raster)

library(sp)

library(devtools)

library(sf)



GRASS.path <-"C:/Program Files/GRASS GIS 7.2.0/grass72.bat"

otbBinPath <-"C:/Program Files/OTB/OTB-7.1.0-Win64/OTB-7.1.0-Win64/bin"

pythonPath <-"C:/Users/Laura aM/anaconda3/envs/py35"

SAGApath <- "C:/Program Files/saga-7.6.2_x64/saga-7.6.2_x64"

TerraLib.path <- "C:/Program Files/TerraLib/terralib-5.2.1-TISA-win_x64/terralib-5.2.1-TISA/lib"


#cargar datos 


B02 <- raster("D:/Mapa_forestal/TMH/Sentinel/S2B_MSIL2A_20190212T113319_N0211_R080_T29TMH_20190212T155715.SAFE/GRANULE/L2A_T29TMH_A010116_20190212T113420/IMG_DATA/R20m/B02.jp2")


B03 <- raster("D:/Mapa_forestal/TMH/Sentinel/S2B_MSIL2A_20190212T113319_N0211_R080_T29TMH_20190212T155715.SAFE/GRANULE/L2A_T29TMH_A010116_20190212T113420/IMG_DATA/R20m/B03.jp2")


B04 <- raster("D:/Mapa_forestal/TMH/Sentinel/S2B_MSIL2A_20190212T113319_N0211_R080_T29TMH_20190212T155715.SAFE/GRANULE/L2A_T29TMH_A010116_20190212T113420/IMG_DATA/R20m/B04.jp2")


B8A <- raster("D:/Mapa_forestal/TMH/Sentinel/S2B_MSIL2A_20190212T113319_N0211_R080_T29TMH_20190212T155715.SAFE/GRANULE/L2A_T29TMH_A010116_20190212T113420/IMG_DATA/R20m/B8A.jp2")


multiple<- stack(B02,B03,B04,B8A)

datos<-brick(multiple)

myExtent <- st_read("D:/Mapa_forestal/WorldView/OBIA_test/polygon.shp")

myExtent2<-st_transform(myExtent, "+proj=utm +zone=29 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")


datos1<- crop(datos, myExtent2)


writeRaster(datos1, filename="D:/Mapa_forestal/WorldView/multilayer2.tif", options="INTERLEAVE=BAND", overwrite=TRUE)


B02crop<- crop(B02,myExtent2)

writeRaster(datos1, filename="D:/Mapa_forestal/WorldView/B02crop.tif", options="INTERLEAVE=BAND", overwrite=TRUE)

B03crop<- crop(B03,myExtent2)

writeRaster(datos1, filename="D:/Mapa_forestal/WorldView/B03crop.tif", options="INTERLEAVE=BAND", overwrite=TRUE)

B04crop<- crop(B04, myExtent2)

writeRaster(datos1, filename="D:/Mapa_forestal/WorldView/B04crop.tif", options="INTERLEAVE=BAND", overwrite=TRUE)

B8Acrop<- crop(B8A, myExtent2)

writeRaster(datos1, filename="D:/Mapa_forestal/WorldView/B8Acrop.tif", options="INTERLEAVE=BAND", overwrite=TRUE)


########################################################################



# Check the manual for more info on this function parameters

?segmentation_OTB_LSMS


outSegmRst <- segmentation_OTB_LSMS(

  

  inputRstPath = "D:/Mapa_forestal/WorldView/multilayer2.tif", 

  SpectralRange=1,

  SpatialRange=5, 

  MinSize=20,

  outputSegmRst = "D:/Mapa_forestal/WorldView/OBIA_test/test.tif",

  verbose = T,

  otbBinPath = otbBinPath)


# Check the manual for more info on this function parameters

?segmentation_GRASS_RG


lista<-list(B02crop,B03crop,B04crop,B8Acrop)


importToGRASS(fileList=lista3, 

              GRASS.path = GRASS.path,

              GRASS.GISDBASE = "base.db",

              GRASS.LOCATION_NAME="D:/Mapa_forestal/WorldView/OBIA_test", 

              GRASS.MAPSET = "PERMANENT",

              addParams = "",

              verbose = TRUE)


segmObj <- segmentation_GRASS_RG(GRASS.path="grass", 

                                 GRASS.inputRstName, 

                                 GRASS.GISDBASE, 

                                 GRASS.LOCATION_NAME="demolocation",

                                 GRASS.MAPSET="PERMANENT", 

                                 outputSegmRst=NULL, 

                                 Threshold=NULL, 

                                 MinSize=NULL, 

                                 memory=1024, 

                                 iterations=20,

                                 verbose=TRUE)


#######################################################

# Check the manual for more info on this function parameters

?segmentation_SAGA_SRG


library(RSAGA)

env <- rsaga.env()

rsaga.import.gdal(B02crop, "B02crop1.sgrd", env = env)

rsaga.import.gdal(B03crop, "B03crop1.sgrd", env = env)


lista2<-list("B02crop1.sgrd","B03crop1.sgrd")

lista4<-list("D:/Mapa_forestal/WorldView/B4segoptim.sgdr","D:/Mapa_forestal/WorldView/B3segoptim.sgdr")


segmObj <- segmentation_SAGA_SRG(rstList=lista4, 

                                 outputSegmRst="D:/Mapa_forestal/WorldView/OBIA_test/test.sgrd", 

                                 Bandwidth=2, 

                               GaussianWeightingBW=5, 

                                 VarFeatSpace=3, 

                                 VarPosSpace=2, 

                                 seedType=0, 

                                 method1=0, 

                                 DWeighting=3, 

                                 normalize=0, 

                                 neighbour=0, 

                                 method2=0, 

                                 thresh=0, 

                                 leafSize=1024, 

                                 SAGApath=env, 

                                 verbose=TRUE)


########################################################

# Check the manual for more info on this function parameters

?segmentation_RSGISLib_Shep


segmObj <- segmentation_RSGISLib_Shep(inputRstPath="D:/Mapa_forestal/WorldView/multilayer2.tif", 

                                      outputSegmRst="D:/Mapa_forestal/WorldView/OBIA_test/test.tif", 

                                      NumClust=10, 

                                      MinSize=20, 

                                      SpectralThresh=10, 

                                      pythonPath=pythonPath, 

                                      verbose=TRUE)

João Gonçalves

unread,
Apr 23, 2020, 7:27:57 PM4/23/20
to SegOptim user group
Dear Laura,

Thanks for using the SegOptim package. As you may understand it is hard to figure exactly what is happening without a fully reproducible example to test and debug.
My advice to proceed is the following:

1) Please separate a script and the error message for each segmentation algorithm to check one by one what is happening and submit it here in attachment for further inspection;

2) Make sure that you have full read/write permissions in the R working directory in setwd(). Often people encounter this problem. Also try to open R/RStudio with admin permissions to check if the behaviour/errors change;

3) In GRASS.path you can remove the .bat extension from there. The other paths to 3rd-party software for image segmentation seem all OK and should work;

4) OTB seems to be working nicely but the parameters are not. Check the Supporting Info of the SegOptim paper here to see parameters used during tests. It may help;

5) In GRASS data importation not sure what is happening but the inputs for the database seem off. You may use the software directly to create a db and import the data to it directly through GRASS command line or GUI instead of using R;

6) In RSGISLib I am not sure what is happening because the error message is too generic. But as before check if you have the right version needed by SegOptim (this link) and check the working folder permissions.


Let me know if this works out.

Cheers
João
- - -



Reply all
Reply to author
Forward
0 new messages