error in tutorial

65 views
Skip to first unread message

Fowad Ahmed

unread,
May 29, 2019, 12:44:50 PM5/29/19
to SegOptim user group
Dear All,

I'm writing to ask for your help and input on an issue I am facing. Currently, I am following the tutorial provided in the documentation of the SegOptim. So in section 4, everything is working fine but in section 4.5 Run OTB image segmentation, I am receiving an error. I have attached the error image and code in word file. Please help me in this regard and much appreciated. 

With kind regards
Fowad
1.JPG
code.docx

João Gonçalves

unread,
May 29, 2019, 2:02:12 PM5/29/19
to SegOptim user group
Dear Fowad,

Thanks for your feedback and for trying out SegOptim.

According to your print screen it seems that you were able to run the OTB segmenter correctly but it is throwing an error when trying to load the output segmented file into R. After checking your script more closely it seems that there a couple of issues with inputs and outputs:

The first one is this:
inputSegFeat.path <- 'G:\\THESIS_Landkilf\\test\\TRAIN_AREAS'
This needs to be changed because SegOptim expects here an input raster file containing features for segmentation (usually a multiband file with selected spectral or other features). [Check the complete script below with corrections]

And the second is this:
outSegmRst.path <- "G:\\THESIS_Landkilf\\test\\SEGM_FEAT"
This variable defines the output segmented raster created by OTB and so SegOptim expects this to be a raster file [Also check the complete script below with corrections]

Also, generally it is preferable to use the forward slash (/) instead of the double back slash (\\).

So here goes the complete workflow for running the segmentation and classification steps - check the comments in detail for more info:

library(sp)
library
(raster)
library
(randomForest)
library
(SegOptim)


### ----- Define the inputs and outputs ----- ###

# Input raster file to use in the OTB segmenter (usually a multi-band file)
# In this case Worldview-2 bands 5, 3 and 2 (Red, Green and Blue)
#
inputSegFeat
.path <- 'G:/THESIS_Landkilf/test/SEGM_FEAT/SegmFeat_WV2_b532.tif"'

# The output segmented raster (usually .tif format works nicely)
#
outSegmRst
.path <- "G:/THESIS_Landkilf/test/SEGM_FEAT/segmentedRaster.tif"

# A raster file containing train areas (with 0's and 1's)
#
trainData
.path <- "G:/THESIS_Landkilf/test/TRAIN_AREAS/TrainAreas.tif"

# Input raster files used as features for classification (used to make a RasterStack object)
# In the example this includes all combinations of Normalized Difference Indices and Spectral Bands
#
classificationFeatures
.path <- c("G:/THESIS_Landkilf/test/CLASSIF_FEAT/ClassifFeat_WV2_NDIs.tif",
                                 
"G:/THESIS_Landkilf/test/CLASSIF_FEAT/ClassifFeat_WV2_SpectralBands.tif")

# The final output classified raster
#
outClassRst
.path <- "G:/THESIS_Landkilf/test/WV2_AcaciaDealbata.tif"

# Path to OTB binaries
#
otbPath
<- "C:/OTB-6.6.1-Win64/bin"


### ----- 1) Run the segmentation step ----- ###

outSegmRst
<- segmentation_OTB_LSMS(
  inputRstPath  
= inputSegFeat.path, # Input raster with features/bands to segment
 
SpectralRange = 3.1,
 
SpatialRange  = 4.5,
 
MinSize       = 21,
  outputSegmRst
= outSegmRst.path, # Output segmented raster
  verbose      
= TRUE,
  otbBinPath    
= otbPath,
  lsms_maxiter  
= 50)

# Load the segmented raster file
segmRst
<- raster(outSegmRst$segm)



After performing the segmentation step you can do the rest of the workflow for classification:



### ----- 2) Run the data preparation step before classification ----- ###

# Load train data into a raster object
trainDataRst
<- raster(trainData.path)

# Stack classification features
classificationFeatures
<- stack(classificationFeatures.path)

# Change the names for each feature layer
names
(classificationFeatures) <- c(paste("NDI_",1:28,sep=""),paste("SpecBand_",1:8,sep=""))


# Populate the segments with data from each feature using the mean (other functions can be used...)
#
calData
<- prepareCalData(rstSegm      = segmRst,
                          trainData    
= trainDataRst,
                          rstFeatures  
= classificationFeatures,
                          thresh      
= 0.5,  # At least 50% covered by a train area then is a positive example
                          funs        
= "mean",
                          minImgSegm  
= 30,
                          verbose      
= TRUE)

                         
### ----- 3) Run the classification step ----- ###

# This will use the Random Forest classifier and 10-fold cross-validation
#
classifObj
<- calibrateClassifier( calData                    = calData,
                                   classificationMethod      
= "RF",
                                   balanceTrainData          
= FALSE,
                                   balanceMethod              
= "ubOver",
                                   evalMethod                
= "10FCV",
                                   evalMetric                
= "Kappa",
                                   minTrainCases              
= 30,
                                   minCasesByClassTrain      
= 10,
                                   minCasesByClassTest        
= 5,
                                   runFullCalibration        
= TRUE)

# Confusion matrix for the full dataset
classifObj$ConfMat$FULL                                  

# Average performance for evalMetric and using test data only (i.e., data not used for training)
classifObj$AvgPerf

# Performance stats for evalMetric across test rounds
classifObj$PerfStats
                       
# Get more evaluation measures
evalMatrix
<- evalPerformanceClassifier(classifObj)
print(evalMatrix)


### ----- 4) Run the final prediction step ----- ###
             
# This will predict the class labels for the entire image (i.e., outside the training set)
# and also save the final classified image
rstPredSegmRF
<- predictSegments(classifierObj = classifObj,
                                 calData      
= calData,
                                 rstSegm      
= segmRst,
                                 predictFor    
= "all",
                                 filename      
= outClassRst.path)
print(rstPredSegmRF)                      


Let me know if this works for you.

Best regards,
João
- - -
Reply all
Reply to author
Forward
0 new messages