SegOptim with SAGA

37 views
Skip to first unread message

Marcelo Alves

unread,
Jul 16, 2020, 1:59:36 PM7/16/20
to SegOptim user group
Prezad@s,
Estou tentando realizar uma segmentação supervisionada em bandas Landsat8 no R usando o pacote SegOptmin. Está ocorrendo o seguinte erro ao tentar realizar a segmentação:

Error in system(command, as.integer(flag), f, stdout, stderr, timeout) : 
  character string expected as first argument

Usei os seguintes comandos para transformar as imagens em formato SAGA e tentar iniciar a segmentação:

library(SegOptim)
library(raster)
library(RSAGA)
library(sf)
library(rgdal)


# Check the manual for more info on this function parameters
?segmentation_SAGA_SRG

 # Configurar diretório de localização de instalação do SAGA GIS
env <- rsaga.env('G:/Downloads/saga-7.3.0_x64/saga-7.3.0_x64/')

# Importar imagem empilhada
lsat_funil_crop <- stack("G:/sr/c5/lsat_funil_crop.tif") 

# Renomear bandas
names(lsat_funil_crop) <- c('costal', 'blue', 'green', 'red', 'NIR', 'SWIR2', 'SWIR3')

lsat_funil_crop


plot(lsat_funil_crop$NIR)

# Exportar bandas para o formato SAGA GIS
writeRaster(lsat_funil_crop$costal, filename="G:/sr/C9/b1.sgrd", format='SAGA', overwrite=TRUE)
writeRaster(lsat_funil_crop$blue, filename="G:/sr/C9/b2.sgrd", format='SAGA', overwrite=TRUE)
writeRaster(lsat_funil_crop$green, filename="G:/sr/C9/b3.sgrd", format='SAGA', overwrite=TRUE)
writeRaster(lsat_funil_crop$red, filename="G:/sr/C9/b4.sgrd", format='SAGA', overwrite=TRUE)
writeRaster(lsat_funil_crop$NIR, filename="G:/sr/C9/b5.sgrd", format='SAGA', overwrite=TRUE)
writeRaster(lsat_funil_crop$SWIR2, filename="G:/sr/C9/b6.sgrd", format='SAGA', overwrite=TRUE)
writeRaster(lsat_funil_crop$SWIR3, filename="G:/sr/C9/b7.sgrd", format='SAGA', overwrite=TRUE)

b <- list.files("G:/sr/C9", pattern=".sgrd")

# Segmentação de imagem
segmObj <- segmentation_SAGA_SRG(rstList=b, 
                                 outputSegmRst= "G:/sr/C9/seg.sgrd", 
                                 Bandwidth=10, 
                                 GaussianWeightingBW=5, 
                                 VarFeatSpace=1, 
                                 VarPosSpace=1, 
                                 seedType=0, 
                                 method1=0, 
                                 DWeighting=3, 
                                 normalize=0, 
                                 neighbour=0, 
                                 method2=0, 
                                 thresh=0, 
                                 leafSize=1024, 
                                 SAGApath=env, 
                                 verbose=TRUE)

Muito grato a quem puder ajudar.
att.
Marcelo de Carvalho Alves

Marcelo Alves

unread,
Jul 17, 2020, 7:15:11 AM7/17/20
to SegOptim user group
This problem was solved including x vector in the code:

x<-c(10,5,1,1)
x

# Segmentação de imagem
segmObj <- segmentation_SAGA_SRG(x, rstList= lsat, 
                                 outputSegmRst= "G:/sr/C9/seg.sgrd", 
                                 Bandwidth=10, 
                                 GaussianWeightingBW=5, 
                                 VarFeatSpace=1, 
                                 VarPosSpace=1, 
                                 seedType=0, 
                                 method1=0, 
                                 DWeighting=3, 
                                 normalize=0, 
                                 neighbour=0, 
                                 method2=0, 
                                 thresh=0, 
                                 leafSize=1024, 
                                 SAGApath='G:/Downloads/saga-7.3.0_x64/saga-7.3.0_x64/',
                                 verbose=TRUE)

João Gonçalves

unread,
Jul 17, 2020, 10:01:23 AM7/17/20
to SegOptim user group
Hi Marcelo,

Thanks for using SegOptim.

Glad that you managed to solve this issue. The "x" is only necessary if the input parameters are not passed directly (and more declaratively) using: Bandwidth, GaussianWeightingBW, VarFeatSpace, and VarPosSpace. Also, the "x" is mainly necessary for optimization using genetic algorithms (which is not the case).

Could you please try to re-run the function and check if it is working without the "x", like:

segmObj <- segmentation_SAGA_SRG(rstList= lsat,
outputSegmRst= "G:/sr/C9/seg.sgrd",
Bandwidth = 10,
GaussianWeightingBW = 5,
VarFeatSpace = 1,
VarPosSpace = 1,
seedType = 0,
method1 = 0,
DWeighting = 3,
normalize = 0,
neighbour = 0,
method2 = 0,
thresh = 0,
leafSize = 1024,
SAGApath = 'G:/Downloads/saga-7.3.0_x64/saga-7.3.0_x64/',
verbose = TRUE)

Please let me have your feedback after testing this.

Thanks!!
João

This problem was solved including x vector in the code:

x<-c(10,5,1,1)
x

# Segmentação de imagem
segmObj <- segmentation_SAGA_SRG(x, rstList= lsat, 
                                 outputSegmRst= "G:/sr/C9/seg.sgrd", 
                                 Bandwidth=10, 
                                 GaussianWeightingBW=5, 
                                 VarFeatSpace=1, 
                                 VarPosSpace=1, 
                                 seedType=0, 
                                 method1=0, 
                                 DWeighting=3, 
                                 normalize=0, 
                                 neighbour=0, 
                                 method2=0, 
                                 thresh=0, 
                                 leafSize=1024, 
                                 SAGApath='G:/Downloads/saga-7.3.0_x64/saga-7.3.0_x64/',
                                 verbose=TRUE)

Marcelo Alves

unread,
Jul 17, 2020, 3:28:05 PM7/17/20
to SegOptim user group
Dear João,
You are correct. It was possible to run SAGA segmentation without x. Thus, I think my error was in the vector file of landsat bands. In this case I did this:

b1<-"G:/sr/C9/b1.sgrd"
b2<-"G:/sr/C9/b2.sgrd"
b3<-"G:/sr/C9/b3.sgrd"
b4<-"G:/sr/C9/b4.sgrd"
b5<-"G:/sr/C9/b5.sgrd"
b6<-"G:/sr/C9/b6.sgrd"
b7<-"G:/sr/C9/b7.sgrd"

lsat<-c(b1,b2,b3,b4,b5,b6,b7)

I had a problem with the number of training samples.   It was necessary to use a pixel by pixel classification as a training sample to predict the segmentation in the entire image. With few samples it was not possible to get good results. For genetic algorithm classification with python (conda, etc), I have tried installation here but with no success. I am using windows 7 and I do not know if this was the problem. The SAGA option was good for me considering I was using RSAGA for terrain analysis.

Thank you, 
kind regards,
Marcelo


On Thursday, July 16, 2020 at 2:59:36 PM UTC-3, Marcelo Alves wrote:

Marcelo Alves

unread,
Nov 2, 2021, 12:22:19 PM11/2/21
to SegOptim user group
Good afternoon everyone,

Could someone please help me with how to save a "SOptim.Classifier" class file in R? We did a random forest classification and considering the time need for this task we would like to save the model to use it again. Is there a possibility for this?

Thank you,
Kind regards,

Marcelo Alves

João F Gonçalves

unread,
Nov 3, 2021, 4:18:44 PM11/3/21
to Marcelo Alves, SegOptim user group

Hi Marcelo,

Thanks for using SegOptim

You can write the object with the calibrated/trained classifier (of class "SOptim.Classifier") by using any of R's base functions such as saveRDS (which writes a single R object to a file, and makes it possible to restore it) or by using the save function which writes an external representation of R objects to file. The objects can be read back from the file at a later date by using the function load


saveRDS(clObj, "myTrainedClassifier.rds") # save the SOptim.Classifier object

clObj <- readRDS("./myTrainedClassifier.rds") # restore the object back in a different session


Hope this helps

Cheers

João

- - -

Marcelo Alves

unread,
Nov 9, 2021, 5:43:22 AM11/9/21
to João F Gonçalves, SegOptim user group
Thank you very much!
----
Marcelo de Carvalho Alves
Professor Associado III
Universidade Federal de Lavras (UFLA)
Departamento de Engenharia Agrícola (DEA)
Telefone: (+55)(35) 3829-1027

Reply all
Reply to author
Forward
0 new messages