Supervised classification (Tutorial)

From AWF-Wiki
(Difference between revisions)
Jump to: navigation, search
(Classification of the LandSat-8 imagery)
 
(87 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{construction}}
+
=Per pixel classification with QGIS and OTB processing plugin=
''In this article, you will learn how to conduct a supervised image classification in [[QGIS]]''
+
In '''OTB Version 7.0.0''' the requirements for the input training vector data are very restrictive because of some bugs.
 +
** File format needs to be Shapefile (GPKG does not work).
 +
** The column containing the class label needs to integer (whole number). '''Data type Integer 64bit does not work!'''
 +
** Any other columns with other data types (real or text (string) with umlauts or blank are not correctly imported which leads to failure of the modul TrainImagesClassifier.
 +
A work around is to make a copy of your training data and delete all other columns than the class label column
  
== Classification with Orfeo Toolbox ==
+
All these issues have been fixed in '''OTB Version 7.1.0'''!
=== Image statistics ===
+
# Add the LandSat-8 imagery ''SUB_LC81950242013188LGN00_MUL.tif'' into a QGIS project. It should be available in the [[Course data|course data]].
+
# Calculate mean and standard error for each band of the LandSat-8 imagery.
+
## Open {{mitem|text=Orfeo Toolbox --> Compute images second order statistics}} (see figure '''A''').
+
## Set ''SUB_LC81950242013188LGN00_MUL.tif'' as {{button|text=Input images}}.
+
## Save the {{button|text=Output XML file}} as ''SUB_LC81950242013188LGN00_mul.tif.xml''.
+
  
=== Train image classifier ===
+
==Training phase==
# Add the training areas ''training_manual_poly.shp'' into QGIS. It should be available in the course data.
+
* In the search engine of Processing Toolbox, type {{typed|text=TrainImages}} and open '''TrainImagesClassifer'''.
# Open {{mitem|text=Orfeo Toolbox --> TrainImageClassifier (libsvm)}} to use the Support Vector Machine SVM algorithm (see figure '''B''').
+
* In the {{button|text=Input Image List}} select a (or optional: several) multispectral images: '''mosaic_S2A_MSIL2A_20200521T102031_N0214_R065_T32UNC_UNB_UPC.tif'''.
# Set ''SUB_LC81950242013188LGN00_MUL.tif'' as {{button|text=Input image list}}.
+
* In the {{button|text=Input Vector Data List}} choose a vector polygon file with training areas: '''lucc_training_2020-05-21.shp'''.
# Set ''training_manual_poly.shp'' as {{button|text=Input vector list}}.
+
* In the {{button|text=Validation Vector Data List}} and choose a vector polygon file with an independent sample of validation areas: leave empty.
# Set ''SUB_LC81950242013188LGN00_mul.tif.xml'' as {{button|text=Input XML image statistics file}}.
+
* Training and validation sample ratio: {{typed|text=0.25}}. This is a 4-fold cross-validation with split 0.75 per cent  for training and 0.25 per cent for testing.
# Set {{button|text=Name of discrimination field}} to ''C_ID'' (C_ID refers to the column that contain the LUC classes code).
+
* Type {{typed|text=Code}} in the {{button|text=Field Name}} text field.
# Save the {{button|text=Output confusion matrix}} as ''ConfusionMatrixSVM.csv''.
+
* Choose Support Vector Machine Classifer {{button|text=libsvm}} from the drop down list.
# Save the {{button|text=Output model}} as ''SVM.model''.
+
* SVM Model Type is {{button|text=csvc}}
# Calculation of accuracies :<br/> Open ''ConfusionMatrixSVM.csv'' in LibreOffice or MS Excel and calculate overall, producer’s and user’s accuracies.
+
* User defined input centroids: Type any character, no need for a filename. This field is required and but ignored (a bug in the OTB plugin!)
 +
* The SVM Kernel Type is {{button|text=Linear}}.
 +
* Switch checkbox {{button|text=Parameters optimization}} '''off'''. The optimization results in a higher accuracy but takes much time (> 1 hour computation).
 +
* In the {{button|text=Output model}} specify an model file: e.g. '''svm.model'''
 +
* Define an output file for {{button|text=Output confusion matrix or contingency table}} (e.g.'''svm_matrix.csv''').
 +
* Click {{button|text=Run}}.
 +
* Click on the {{button|text=Log}} tab and inspect the '''model quality''' measures: Precision, Recall, F-score and Kappa index.
 +
[[File:qgis_otb_trainimages.png|400px]]
  
=== Classification===
+
==Classification phase==
# Add the cloud mask ''cloud_shadow_mask.tif'' into QGIS. It should be available in the course data.
+
* In the search engine of Processing Toolbox, type {{typed|text=ImageClassifier}} and double click '''ImageClassifier'''.
# Open {{mitem|text=Orfeo Toolbox --> Image Classification}} (see figure '''C''').
+
* Set ''mosaic_S2A_MSIL2A_20200521T102031_N0214_R065_T32UNC_UNB_UPC.tif'' as {{button|text=Input image}}.
# Set ''SUB_LC81950242013188LGN00_MUL.tif'' as {{button|text=Input image}}.
+
* Set {{button|text=Input _mask}} to blank (top of drop-down list).
# Set ''cloud_shadow_mask.tif'' as {{button|text=Input mask}}.
+
* Set '''lucc_svm.model''' as {{button|text=Model file}}.
# Set ''SVM.model'' as {{button|text=Model file}}.
+
* Set the number of classes in your model: this is the number of unique classes in your training file.
# Set ''SUB_LC81950242013188LGN00_MUL.tif.xml'' as {{button|text=Statistical file}}.
+
* Set {{button|text=Output pixel type}} to '''uint16'''
# Save the {{button|text=Output image}} as ''su_svm.tif''.
+
* Save the {{button|text=Output image}} as '''svm_classification.tif'''.
# Evaluate classification results.
+
* Uncheck Confidence map: ''Open output file after running algorithm''.
## Add the classification result ''su_svm.tif'' to QGIS.
+
* Uncheck Probability map: ''Open output file after running algorithm''.
## Right click ''su_svm.tif'' in the [[TOC]] and select {{mitem|text=Properties --> Style --> Style --> Load Style}}.
+
* Add ''svm_classification.tif'' to QGIS canvas.
## Load ''classifcation.qml'', which you should be available in the course data.  
+
* Find the prepared style file '''rast_classifcation.qml''' in the Tutorial data of Workshop 05.
## Open the Google Maps Layer under {{mitem|text=Web --> ObenLayers plugin --> Add Google Satellite layer}} and look for misclassification.
+
* Right click ''svm_classification.tif'' in the [[TOC]] and select {{mitem|text=Properties --> Symbology --> Style --> Load Style}}.
 +
* Select the style file '''svm_classification.tif'''. {{button|text=OK}}.
 +
[[File:qgis_otb_imageclassifier.png|400px]]
  
=== Compute a confusion matrix with independent reference data ===
+
==Postprocessing==
# Open {{mitem|text=Orfeo Toolbox --> ComputeConfusionMatrix (Vector)}} (see figure '''D''').
+
This application uses a majority filter with a circular structure element.
# Set ''SUB_LC81950242013188LGN00_MUL.tif'' as {{button|text=Input image}}.
+
* In the search engine of Processing Toolbox, type {{typed|text=regularization}} and double click '''ClassificationMapRegularization'''.
# Set ''train_systematic_seg.shp'' as {{button|text=Input reference vector data}}.
+
* Define {{button|text=Input classification image}} as an classification output raster.
# Set {{button|text=Field name}} to ''C_ID''.
+
* Set {{button|text=Structuring element radius}} to {{typed|text=2}} pixels.
 +
* Set {{button|text=Output pixel type}} to {{button|text=uint16}}.
 +
* Save the {{button|text=Output regularizd image}} as '''svm_classification_majority.tif'''.
 +
* Click {{button|text=Run}}.
 +
[[File:qgis_otb_majority.png|400px]]
  
 +
=Per pixel classification with OTB standalone=
 +
==Training phase==
 +
* Type into the search box of the Windows taskbar: {{typed|text=mapla.bat}}. Click on mapla.bat to open  Monteverdi Application Launcher.
 +
* In the search engine of mapla, type {{typed|text=TrainImages}} and double click '''TrainImagesClassifer'''.
 +
* In the {{button|text=Input Image List}} click on {{button|text=+}} and select a (or optional: several) multispectral images: '''Subset_S2A_MSIL2A_20170619T_MUL.tif '''.
 +
* In the {{button|text=Input Vector Data List}} choose a vector polygon file with training areas: '''lucc_training_input.shp'''.
 +
* Activate the checkbox {{button|text=Validation Vector Data List}} and choose a vector polygon file with an independent sample of validation areas: '''lucc_validation.shp'''
 +
* In the {{button|text=Output model}} specify an output model file: e.g. '''lucc_svm.model'''
 +
* Activate the checkbox and save the {{button|text=Output confusion matrix or contingency table}} as '''lucc_svm_confusion.csv'''.
 +
* In the {{button|text=Bound sample number by minimum}} field type {{typed|text=1}}.
 +
* Set the {{button|text=training and validation sample ratio}} to {{typed|text=0 }}. (0 = all training data).
 +
* Mark  ''C_ID''  in the {{button|text=Field containing the class integer label}} (C_ID refers to the column that contains the LUC code in the training and validation vector file).
 +
* Choose {{button|text=LibSVM classifier}} from the drop down list as Classifier to use for the training.
 +
* The SVM Kernel Type is {{button|text=Linear}}.
 +
* The SVM Model Type is {{button|text=C support vector classification}}.
 +
* Switch the Parameters optimization to {{button|text=on}}.
 +
* Check user defined seed and enter an integer value.
 +
* Click on {{button|text=Execute}}.
 +
* Click on the {{button|text=Log}} tab and inspect the quality measures: Precision, Recall, F-score and Kappa index.
 +
[[File:otb_trainimages.png|500px]]
  
 +
==Classification phase==
 +
* In the search engine of mapla, type {{typed|text=ImageClassifier}} and double click '''ClassifcationMapRegularization'''
 +
* Set ''Subset_S2A_MSIL2A_20170619T_MUL.tif'' as {{button|text=Input image}}.
 +
* Set '''SVM.model''' as {{button|text=Model file}}.
 +
* Save the {{button|text=Output image}} as '''svm_classification.tif'''.
 +
[[File:otb_imageclassifier.png|500px]]
 +
* Evaluate classification results:
 +
* Add ''svm_classification.tif'' to QGIS canvas.
 +
* Download the style file '''classifcation.qml''' from Stud.IP.
 +
* Right click ''svm_classification.tif'' in the [[TOC]] and select {{mitem|text=Properties --> Style --> Style --> Load Style}}.
 +
* Select the style file '''\lucc\classification.qml'''. {{button|text=OK}}.
 +
* Open the text file '''lucc_svm_confusion.csv''' with LibreOffice Calc or MS Excel and calculate overall, user, producer accuracy and kappa index.
  
 
+
==Postprocessing==
=== Figures ===
+
This application uses a majority with a circular structure element.
{|class="wikitable" style="border:0pt"
+
* In the search engine of mapla, type, {{typed|text=regular}} and double click '''ClassifcationMapRegularization'''.
|style="border:0pt"|[[Image:ComputeImage.png|thumb|450px|'''Figure A''': Dialogues of the ''Compute images second order statistics'']]
+
* Define {{button|text=Input classification image}} as ans classification output raster.
|style="border:0pt"|[[Image:TrainImageClassifier.png|thumb|450px|'''Figure B''': Dialogues of the ''TrainImageClassifier (libsvm)'']]
+
* Save the {{button|text=Output regularizd image}} as '''svm_classification_majority.tif'''.
|-
+
* Set {{button|text=Structuring element radius}} to {{typed|text=3}} pixels.
|style="border:0pt"|[[Image:ImageClassification.png|thumb|450px|'''Figure C''': Dialogues of the ''Image Classification'']]
+
* Set {{button|text=Output pixel type}} to {{button|text=uint8}}.
|style="border:0pt"|[[Image:ComputeConfusionMatrix.png|thumb|450px|'''Figure D''': Dialogues of the ''ComputeConfusionMatrix (Vector)'']]
+
* Click {{button|text=Execute}}.
|}
+
[[File:otb_majority.png|400px]]
 
+
 
+
 
+
 
+
== Classification with Semi-Automatic Classification Plugin ==
+
=== Plugin installation and testing ===
+
# Installing the plugin.
+
## Click {{button|text=Manage and install plugins}}.
+
## In the appearing menu, select {{button|text=Add more}}.
+
## Browse for the {{button|text=Semi-Automatic Classification Plugin}} (it's most comfortable to use the search-field).
+
## Click {{button|text=Install}}. If installation was successful, two additional windows should add to the [[QGIS GUI]] ({{button|text=Classification}} and {{button|text=ROI creation}}, see figure '''A''').
+
# Testing the plugin
+
## For the plugin to work, you need to make sure that your installations of GRASS GIS, ORFEO Toolbox and SAGA GIS work correctly.
+
## With the plugin installed, select {{mitem|text=Raster --> Semi Automatic Class Plugin}}.
+
## In the appearing menu, select the {{button|text=Settings}} tab (figure '''B'''). Click {{button|text=Test Grass GIS}} and wait until the message {{button|text=Test OK}} appears. Do the same the Orfeo Toolbox, Saga and Qgis geoalgorithms.
+
 
+
=== Classification ===
+
# Add the raster map ''188_pca_indices.tif'' from the [[course data]] (see previous exercises).
+
# Also add the vector layer ''Training_manual_poly.shp''.
+
 
+
==== ROI digitalization ====
+
For better understanding of the workflow, try digitizing some
+
'''R'''egions '''O'''f '''I'''nterest yourself.
+
# First, you need to create a new shapefile to store the ROIs.
+
#* In the {{button|text=Input}} section of the {{button|text=ROI creation}} menu, click the {{button|text=New shapefile}} button. Just select the directory and file for the new shapefile and add it to the [[QGIS GUI|canvas]]. Click the {{button|text=Refresh list of shapefiles}} button and select your shapefile from the pulldown menu.
+
# In the {{button|text=ROI Definition}} section you can add define the attributes of each ROI. The {{button|text=Macroclass ID}}, which may be used to code sub-classes of the main classes (like e.g. ''Coniferous forest'' as a macroclass of ''Forest''), the {{button|text=Class ID}}  and the name of the class in the {{button|text=Information}} column.  
+
# As an example, enter {{typed|text=Urban area}} in the {{button|text=Information}} column and leave the ID's as they are.
+
# Looking at the {{button|text=ROI creation}} section, you see two options for generating the ROI:
+
#* By clicking {{button|text=+}}, you can select nondimensional points at the map. Simply place them in pixel clusters you have identified as belonging to the respective class. The regions will later be generated by ''region growing''.
+
#* A more precise way is to delineate ROI polygons using the {{button|text=Create a ROI polygon}} tool. You can re-draw the shape of a specific pixel cluster on the map and finish with a right-click.
+
# After having digitized a certain number of points or polygons for a ROI, click {{button|text=Save ROI}}. Wait until the computation is finished. You will then see the ROI being added to the {{button|text=ROI list}} in the {{button|text=Classification}} window.  
+
# Repeat ROI generation for a few classes, like e.g.
+
#* Forest
+
#* Agricultural area
+
#* Water
+
#* Clouds
+
# After having added a few classe, select {{button|text=Minimum distance}} in the {{button|text=Classification algorithm}} section. Click {{button|text=+}} in the {{button|text=Classification preview}} section. By clicking somewhere on the map, a small preview for a section of the map will be generated.
+
 
+
==== Using predefined ROIs ====
+
Just adding a few classes will not generate a satisfying classification result. For a high quality classification, ROIs have to be very well defined, which requires a lot of time. For a supervised classification closer to real-life, you can use some predefined ROIs.
+
# Load the shapefile ''training_manual_poly.shp'' into the project and select it as training shapefile. You'll see how the ROI list is automatically updated.
+
# For {{button|text=Classification style}} click {{button|text=Select qml}} and browse for the file ''classification.qml'' from the [[course data]].  
+
# In the {{button|text=Classification output}} section, check the {{button|text=Classificatin report}} and {{button|text=Calculate accuracy}} boxes.
+
# Click {{button|text=Perform classification}}. Enter name and path for the output map (e.g. ''ml_classification.tif'') and wait for the classification to finish (figure '''C'''). This may take a long time! Plan to do some different work (or just have lunch) while the algorithm is running.
+
 
+
==== Alternative algorithms and accuracy assessment ====
+
To get an impression of results depending on the alorithm selected, try the {{button|text=Maximum likelihood}} classifier as {{button|text=Classification algorithm}}.
+
In the {{button|text=Semi Automatic classification}} dialogue (figure '''D'''), select the {{button|text=Post processing}} tab to view the error matrix. If desired, it can be saved for future reference.
+
 
+
 
+
 
+
 
+
=== Figures ===
+
{| class="wikitable" style="border:0pt"
+
|style="border:0pt"|[[Image:RemSens_Exercise08_02.png|thumb|450px|'''Figure A''': Dialogues of the ''Semi-Automatic Classification Plugin]]
+
|style="border:0pt"|[[Image:RemSens_Exercise08_01.png|thumb|450px|'''Figure B''': The ''Semi-Automatic Classification Plugin'' menu]]
+
|-
+
|style="border:0pt"|[[Image:RemSens_Exercise08_04.png|thumb|450px|'''Figure C''': The classified landcover map]]
+
|style="border:0pt"|[[Image:RemSens_Exercise08_05.png|thumb|450px|'''Figure D''': The ''Accuracy'' menu with error matrix]]
+
|}
+
 
+
[[Category:Image classification]]
+

Latest revision as of 14:12, 1 December 2020

Contents

[edit] Per pixel classification with QGIS and OTB processing plugin

In OTB Version 7.0.0 the requirements for the input training vector data are very restrictive because of some bugs.

    • File format needs to be Shapefile (GPKG does not work).
    • The column containing the class label needs to integer (whole number). Data type Integer 64bit does not work!
    • Any other columns with other data types (real or text (string) with umlauts or blank are not correctly imported which leads to failure of the modul TrainImagesClassifier.

A work around is to make a copy of your training data and delete all other columns than the class label column

All these issues have been fixed in OTB Version 7.1.0!

[edit] Training phase

  • In the search engine of Processing Toolbox, type TrainImages and open TrainImagesClassifer.
  • In the Input Image List select a (or optional: several) multispectral images: mosaic_S2A_MSIL2A_20200521T102031_N0214_R065_T32UNC_UNB_UPC.tif.
  • In the Input Vector Data List choose a vector polygon file with training areas: lucc_training_2020-05-21.shp.
  • In the Validation Vector Data List and choose a vector polygon file with an independent sample of validation areas: leave empty.
  • Training and validation sample ratio: 0.25. This is a 4-fold cross-validation with split 0.75 per cent for training and 0.25 per cent for testing.
  • Type Code in the Field Name text field.
  • Choose Support Vector Machine Classifer libsvm from the drop down list.
  • SVM Model Type is csvc
  • User defined input centroids: Type any character, no need for a filename. This field is required and but ignored (a bug in the OTB plugin!)
  • The SVM Kernel Type is Linear.
  • Switch checkbox Parameters optimization off. The optimization results in a higher accuracy but takes much time (> 1 hour computation).
  • In the Output model specify an model file: e.g. svm.model
  • Define an output file for Output confusion matrix or contingency table (e.g.svm_matrix.csv).
  • Click Run.
  • Click on the Log tab and inspect the model quality measures: Precision, Recall, F-score and Kappa index.

Qgis otb trainimages.png

[edit] Classification phase

  • In the search engine of Processing Toolbox, type ImageClassifier and double click ImageClassifier.
  • Set mosaic_S2A_MSIL2A_20200521T102031_N0214_R065_T32UNC_UNB_UPC.tif as Input image.
  • Set Input _mask to blank (top of drop-down list).
  • Set lucc_svm.model as Model file.
  • Set the number of classes in your model: this is the number of unique classes in your training file.
  • Set Output pixel type to uint16
  • Save the Output image as svm_classification.tif.
  • Uncheck Confidence map: Open output file after running algorithm.
  • Uncheck Probability map: Open output file after running algorithm.
  • Add svm_classification.tif to QGIS canvas.
  • Find the prepared style file rast_classifcation.qml in the Tutorial data of Workshop 05.
  • Right click svm_classification.tif in the TOC and select Properties --> Symbology --> Style --> Load Style.
  • Select the style file svm_classification.tif. OK.

Qgis otb imageclassifier.png

[edit] Postprocessing

This application uses a majority filter with a circular structure element.

  • In the search engine of Processing Toolbox, type regularization and double click ClassificationMapRegularization.
  • Define Input classification image as an classification output raster.
  • Set Structuring element radius to 2 pixels.
  • Set Output pixel type to uint16.
  • Save the Output regularizd image as svm_classification_majority.tif.
  • Click Run.

Qgis otb majority.png

[edit] Per pixel classification with OTB standalone

[edit] Training phase

  • Type into the search box of the Windows taskbar: mapla.bat. Click on mapla.bat to open Monteverdi Application Launcher.
  • In the search engine of mapla, type TrainImages and double click TrainImagesClassifer.
  • In the Input Image List click on + and select a (or optional: several) multispectral images: Subset_S2A_MSIL2A_20170619T_MUL.tif .
  • In the Input Vector Data List choose a vector polygon file with training areas: lucc_training_input.shp.
  • Activate the checkbox Validation Vector Data List and choose a vector polygon file with an independent sample of validation areas: lucc_validation.shp
  • In the Output model specify an output model file: e.g. lucc_svm.model
  • Activate the checkbox and save the Output confusion matrix or contingency table as lucc_svm_confusion.csv.
  • In the Bound sample number by minimum field type 1.
  • Set the training and validation sample ratio to 0. (0 = all training data).
  • Mark C_ID in the Field containing the class integer label (C_ID refers to the column that contains the LUC code in the training and validation vector file).
  • Choose LibSVM classifier from the drop down list as Classifier to use for the training.
  • The SVM Kernel Type is Linear.
  • The SVM Model Type is C support vector classification.
  • Switch the Parameters optimization to on.
  • Check user defined seed and enter an integer value.
  • Click on Execute.
  • Click on the Log tab and inspect the quality measures: Precision, Recall, F-score and Kappa index.

Otb trainimages.png

[edit] Classification phase

  • In the search engine of mapla, type ImageClassifier and double click ClassifcationMapRegularization
  • Set Subset_S2A_MSIL2A_20170619T_MUL.tif as Input image.
  • Set SVM.model as Model file.
  • Save the Output image as svm_classification.tif.

Otb imageclassifier.png

  • Evaluate classification results:
  • Add svm_classification.tif to QGIS canvas.
  • Download the style file classifcation.qml from Stud.IP.
  • Right click svm_classification.tif in the TOC and select Properties --> Style --> Style --> Load Style.
  • Select the style file \lucc\classification.qml. OK.
  • Open the text file lucc_svm_confusion.csv with LibreOffice Calc or MS Excel and calculate overall, user, producer accuracy and kappa index.

[edit] Postprocessing

This application uses a majority with a circular structure element.

  • In the search engine of mapla, type, regular and double click ClassifcationMapRegularization.
  • Define Input classification image as ans classification output raster.
  • Save the Output regularizd image as svm_classification_majority.tif.
  • Set Structuring element radius to 3 pixels.
  • Set Output pixel type to uint8.
  • Click Execute.

Otb majority.png

Personal tools
Namespaces

Variants
Actions
Navigation
Development
Toolbox
Print/export