OTB example code for DUMMIES

#1

Hi everyone
I am new to OTB, I have background on Machine Learning and computer vision and now try to use OTB to classify geospatial images. (as for a university thesis)

I use OTB inside python notebook, I have tried the example on ‘smoothing’ over Sentinel1 products and it works but then I got frustrated to make a very simple example of ‘TrainVectorClassifier’ or ‘TrainImagesClassifier’:

As far as I understood if I have a tif image and a shapefile containing the polygons (which overlap with the tif file) with associated class to each polygon, I can run TrainImagesClassifier , right?
OR
if I have a sqlite file, I can use TrainVectorClassifier and the related tif (on which I extracted the polygons and their classes) could be used later to ImageClassifier,

Could anyone kindly provide me just a tiff file and its related shapefile or sqlite file?

there are files on the orfeo git but I do not succeed to create an A-to-Z example for myself.

I got ‘QB_1_ortho.tif’ and ‘VectorData_QB1.shp’ and it is not working, then I tried on ‘cupriteSubHsi.tif’ and ‘cuprite_samples.sqlite’ and still I got nowhere.
I tried to install QGIS and use orfeo imported inside the platform but even there I could not make it work.

For the moment if anyone as a response to this S.O.S message :smiley: just could give me an example in python notebook or even only the correct files , he/she would save me from smashing my head to the wall :-)))
Best
Panteha

#2

Hi Panteha,

We have OTB training material (slides, exercises guide and data) that contain some quite simple example (sources of all documentation is available here : https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb-documents/tree/master/Courses/org)

You can download the dataset here : https://www.orfeo-toolbox.org/packages/WorkshopData/
Once unzipped, you will find a classification folder with several Sentinel2 images and vector files :

  • Five classification/images/<date>_<S2_tile>.tif : this is a small time serie
  • classification/references/training/training.shp contain several homogenous polygons with an attribute (CODE) that indicates the class of the pixel.

You can train a classifier with an otbcli application :
otbcli_TrainImagesClassifier -io.il <your path>/WorkshopData/classification/images/20160607_T31TCJ_ROI_20m.tif -io.vd <your path>/WorkshopData/classification/references/training/training.shp -io.out <your path>/WorkshopTest/model_2016-06-07.rf -sample.vfn CODE -ram 128 -classifier rf -classifier.rf.max 20 -classifier.rf.cat 13 -classifier.rf.nbtrees 50 -classifier.rf.acc 0

Of course, you can use it through your Python script if you set all the parameters properly. Beware that they depend on the classifier you use (Random Forest -rf- in my example).
app = otb.Registry.CreateApplication("TrainImagesClassifier")
app.SetParameterValue("io.il", "path to your image.tif")
[ ... ]
app.ExecuteAndWriteOutput()

Once you are familiar to TrainImagesClassifier, you can use the TrainVectorClassifier.
OTB applications can use different file formats (shp, sqlite, geopackage, etc.) for their vector data.
Difference between the two apps if that you either train a classifier that will work on pixel values or on values associated within each polygon. You can use the Segmentation framework to divide your input image in polygons, and then train a classifier.
You will also find a lot of information in OTB Cookbook https://www.orfeo-toolbox.org/CookBook/recipes/pbclassif.html

Hope that it helps !

Best regards

1 Like
#3

Hi Yannick
Thank you for the expressive reply, I have put some time to study the materials you mentioned.
I have realised my first unsupervised classifier and it worked out successfully, yet I am still stuck with the supervised classifier:
I would kindly invite you to have a look on my post where I have explained the error:

https://forum.orfeo-toolbox.org/t/itk-error-shapefile-field-not-find/116

I would be grateful for any help.
Best regards
Panteha

#4

Hi Panteha,

I am a master student, and one of my research interests is land cover classification using SAR images. I have finished the cases with Sentinel-1 SAR data using classifiers like SVM and RF.

Recently, I want to try some other Machine Learning methods. OTB seems to be a helpful tool, but I am stuck at the first step, installation. If there any chance, we can discuss more details about classification and help each other?

I would be grateful for your reply. My e-mail is zybbbbbox@gmail.com.
Best regards
Yi Zhao

#5

Hi Yi Zhao
sorry to reply so late.
I use OTB in python with following script, on a VM that already had it installed. (for installation part, I am sorry I can not help that much) :
import sys
sys.path.append(’/opt/anaconda/bin/’)
sys.path.append(’/opt/OTB-6.6.1/lib/python’)
sys.path.append(’/opt/OTB-6.6.1/lib/libfftw3.so.3’)

os.environ[‘OTB_APPLICATION_PATH’] = ‘/opt/OTB-6.6.1/lib/otb/applications’
os.environ[‘LD_LIBRARY_PATH’] = ‘/opt/OTB-6.6.1/lib’
os.environ[‘ITK_AUTOLOAD_PATH’] = ‘/opt/OTB-6.6.1/lib/otb/applications’
os.environ[‘GDAL_DATA’] = ‘/opt/anaconda/share/gdal/’
import otbApplication

I hope you somehow get it done and I will be happy to share ideas on OTB classifier algorithms.
Best
PP

#6

Hi Panteha
Thanks for your help, I have figured out the issue was caused by my python version and my environment variable setting. I can succeed importing otbApplication in command line but fail in Spyder. That is weird.
Anyway, thanks for your solutions again. If you have any questions about Sentinel data or LULC, I will try my best.

Best regards
Yi Zhao