Thursday 28 July 2016

Use Orfeo Toolbox in R


I was wondering if it is possible to use the algorithm of Orfeo Toolbox in RStudio. I have been looking for some information but didn't find anything relevant. From the Orfeo toolbox I see that it can be access through command line so that may be an option?


I am working with Ubuntu Xenial 16.04



Answer



OTB algorithms are in OTB_installation_path/bin. So, as you can use them in command line (see more in documentation), the same way could be used trough R by system function.


You can create your owns functions to work with OTB in R. An example for Mean Shift Image Segmentation:



meanshift.segm <- function(otb.path = "", raster.in = "", out.path = "", name ="", filter.meanshift.spatialr = "5",
filter.meanshift.ranger = "15", filter.meanshift.thres = "0.1",
filter.meanshift.maxiter = "100", filter.meanshift.minsize = "100",
mode.vector.outmode = "ovw", mode.vector.inmask = "", mode.vector.neighbor = "false",
mode.vector.stitch = "true", mode.vector.minsize = 1, mode.vector.simplify = 0.1,
mode.vector.layername = "layer", mode.vector.fieldname = "DN", mode.vector.tilesize = 1024,
mode.vector.startlabel = 1){
# Set configuration
conf <- paste("-in",raster.in,"-filter meanshift","-filter.meanshift.spatialr",filter.meanshift.spatialr,
"-filter.meanshift.ranger",filter.meanshift.ranger,"-filter.meanshift.thres",filter.meanshift.thres,

"-filter.meanshift.maxiter",filter.meanshift.maxiter,"-filter.meanshift.minsize",filter.meanshift.minsize,
"-mode vector","-mode.vector.out",paste(out.path,"/",name,".shp",sep=""),"-mode.vector.outmode",mode.vector.outmode,
ifelse(missingArg(mode.vector.inmask),"",paste("-mode.vector.inmask",mode.vector.inmask)),
"-mode.vector.neighbor", mode.vector.neighbor,
"-mode.vector.stitch",mode.vector.stitch,
"-mode.vector.minsize",mode.vector.minsize,
"-mode.vector.simplify",mode.vector.simplify,
"-mode.vector.layername",mode.vector.layername,
"-mode.vector.fieldname",mode.vector.fieldname,
"-mode.vector.tilesize",mode.vector.tilesize,

"-mode.vector.startlabel",mode.vector.startlabel)
# apply function in command line
system(paste(otb.path,"/otbcli_Segmentation"," ",conf,sep=""))
# save configuration for futher use
write.table(x = conf,file = paste(out.path,"/",name,"_conf.txt",sep=""),row.names = F, col.names = F)
}

# usage, you can set any option listed above
meanshift.segm(otb.path = "OTB-5.8.0-Darwin64/bin", raster.in = "path to/rater_in.tif", out.path="/out/path", name = "test")


# or, to read the output into R
out_path = '/out/path'
lyr_name = 'test'

# usage
meanshift.segm(otb.path = "OTB-5.8.0-Darwin64/bin", raster.in = "path to/rater_in.tif", out.path=out_path, name = lyr_name)

shp <- readOGR(dsn=out_path, layer = lyr_name, driver = 'ESRI Shapefile')

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...