Classifying a raster

Image classification is one of the most complex aspects of remote sensing. While QGIS is able to color pixels based on values for visualization, it stops short of doing much classification. It does provide a Raster Calculator tool where you can perform arbitrary math formulas on an image, however it does not attempt to implement any common algorithms. The Orfeo Toolbox is dedicated purely to remote sensing and includes an automated classification algorithm called K-means clustering, which groups pixels into an arbitrary number of similar classes to create a new image. We can do a nice demonstration of image classification using this algorithm.

Getting ready

For this recipe, we will use a false color image which you can download here:

https://geospatialpython.googlecode.com/files/FalseColor.zip

Unzip this TIFF file and place it in your /qgis_data/rasters directory.

How to do it...

All we need to do is run the algorithm on our input image. The important parameters are the second, third, sixth, and tenth parameters. They define the input image name, the amount of RAM to dedicate to the task, the number of classes, and the output name respectively.

  1. First, import the processing module in the QGIS Python Console:
    import processing
    
  2. Next, run the otb algorithm using the processing.runandload() method to display the output in QGIS:
    processing.runandload("otb:unsupervisedkmeansimageclassification","/qgis_data/rasters/FalseColor.tif",768,None,10000,3,1000,0.95,"/qgis_data/rasters/classify.tif",None)
    
  3. When the image loads in QGIS, double click the layer name in the Table of Contents.
  4. In the Layer Properties dialog, choose Style.
  5. Change the Render type menu to Singleband pseudocolor.
  6. Change the color map menu on the right to Spectral.
  7. Click the Classify button.
  8. Choose the Ok button at the bottom of the window.
  9. Verify your image looks similar to the following image, except without the class labels:
    How to do it...

How it works...

Keeping the class number low allows the automated classification algorithm to focus on the major features in the image and helps when us to achieve a very high level of accuracy determining overall land use. Additional automated classification would require supervised analysis with training data sets and more in-depth preparation. But the overall concept would remain the same. QGIS has a nice plugin for semi-automatic classification. You can learn more about it at the following URL:

https://plugins.qgis.org/plugins/SemiAutomaticClassificationPlugin/

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset