could not find function "segmentation_Terralib_Baatz"

41 views
Skip to first unread message

Ricardo Martínez Prentice

unread,
Nov 24, 2020, 5:01:18 AM11/24/20
to SegOptim user group
Dear João Gonçalves and group

First, thank you for this R package. I would like to set up this package in order to use it for my project. I am familiar with the multiresolution algorithm (Baatz&Schape,2000) and I see that the software which uses this is the Terralib (TISA) and the function is segmentation_Terralib_Baatz(). Why is not this function in the documentation (3.4 Image segmentation)?
I  have defined all the previous steps for installation but I get the following error: 
could not find function "segmentation_Terralib_Baatz"

Thank you in advance,
Regards,

João Gonçalves

unread,
Nov 24, 2020, 2:09:13 PM11/24/20
to SegOptim user group
Dear Ricardo,

Thanks for trying SegOptim and for pointing that the Baatz algorithm is not included in the documentation... definitely I need to add this one to the list!

The SegOptim package does provide a wrapper function called segmentation_Terralib_Baatz() to access the Baatz/Schape multi-resolution algorithm from a specific minimal build of TerraLib which includes the Baatz and the mean region growing segmenters (only for Windows!! sorry in advance...). I checked the last version of the package and the function is indeed available and working correctly in tests.

You can download TISA (TerraLib's Image Segmentation Algorithms) from here.

So you will need to install the SegOptim package in R (use the GitHub repo for this here) with :
remotes::install_github("joaofgoncalves/SegOptim")

then load the package using library(SegOptim) and the segmentation_Terralib_Baatz function will be available (in this function, the parameter TerraLib.path should point to the windows path where the TISA cmd tools are, for example: "C:/MyFiles/terralib-5.2.1-TISA/lib").

Have you managed to perform the above steps? Please make sure to install the package from GitHub. Also, please paste here the code and, the output from it to better check this issue... otherwise it is difficult to thoroughly diagnose what is happening.



Cheers
João Gonçalves
- - -

Ricardo Martínez Prentice

unread,
Nov 25, 2020, 5:23:53 AM11/25/20
to SegOptim user group
Thank you for your help . 

I have used the command  library(SegOptim)  and it worked. Everything is working but taking too long to process. 

Here is the code. Sorry I am still learning how to publish projects in GitHUB so at the moment I copy and paste here:

# ----- INSTALL THE PACKAGE SEGOPTIM---- ##


#PROTOCOL:  First, install remotes to conect to GitHUB
#Check if devtools package is installed and install it if not

if(!requireNamespace("remotes")){
  install.packages("remotes")
}

# Run the installation, from repository in GitHub . I have chosen ALL installation
#
remotes::install_github("joaofgoncalves/SegOptim")


## ----- END OF INSTALLATION OF PACKAGE SEGOPTIM---- ##

##Calling libraries

library(SegOptim)
library(tools)
library(raster)
library(rgdal)
library(rgeos)
library(sp)
library(RColorBrewer)
library(pillar)
library(cli)
library(vctrs)
library(rprojroot) 
library(rstudioapi)

#Remove existing variables
rm(list = ls())

# A function providing CLI access to TerraLib 5 Baatz-Shcape segmentation 
##and optimize its parameters using genetic algorithms.
#HELP: ??segmentation_Terralib_Baatz

##Create multi layered Raster (input bands)

allBands <- list.files("Route to rasters",pattern = "tif$",full.names = TRUE)
#1. stack bands and save them
stackBands <- stack(allBands)
writeRaster(stackBands, filename = "Route to stack bands/stack.tif")

ImgSegment <- segmentation_Terralib_Baatz(
c(5,3,20,500),
" Route to stack bands/stack.tif  ",
outputSegmRst = "Output/segmentation.tif",
CompactnessWeight = NULL,
SpectralWeight = NULL,
Threshold = NULL,
MinSize = NULL, # Minimum size of 8 because our training samples have 8 pixels (Pol -> Ras) 
verbose = TRUE, # I want to see messages
TerraLib.path = "C:/terralib-5.2.1-TISA-win_x64/terralib-5.2.1-TISA/lib" #input for argument path

# Check out the result
rstSegm <- raster(ImgSegment$segm)

print(rstSegm)

plot(rstSegm)


I am doing a first teste with those arguments of x. Perhaps it is why it is going so slow. 

Thank you.

João Gonçalves

unread,
Nov 25, 2020, 7:19:16 AM11/25/20
to SegOptim user group
Hi,

Thanks for your feedback. Depending on your computer resources, image size, spatial resolution, number of raster bands and the parameters used, in fact, the segmentation in TISA can take a while to process the whole image (or even exhaust the memory in some cases...).
However, you may consider the following to increase speed:
(a) using a small portion of your image to test the segmentation process and tune the parameters,
(b) resampling the whole image to a larger pixel size (i.e. upsampling) as to reduce the overall raster size and/or
(c) set up different parameters to modify the segmentation complexity, which generally requires some trials (parameter optimization through genetic algorithms is also available in SegOptim ;-). Check also the Supporting Information - Appendix S2 of the 'SegOptim paper' available here, with examples of segmentation parameters used during tests.

Cheers
João

Ricardo Martínez Prentice

unread,
Nov 25, 2020, 11:06:14 AM11/25/20
to SegOptim user group
Thank you , that was very helpful. 

I am already testing the segmentation. Next steps will be to carry out a classification. I will write again in case of having more questions. 

I am also reading the resources of TerraLib software in order to understand better the multiresolution algorithm as it is a little bit different from eCognition implementation, which I am more used to work with. 

Regards,



Reply all
Reply to author
Forward
0 new messages