# 5.2. Tutorial 2¶

The following is a basic tutorial about the Semi-Automatic Classification Plugin (SCP). It is assumed that you have a basic knowledge of QGIS.

## 5.2.1. Tutorial 2: Unsupervised Classification¶

This is a basic tutorial about the use of SCP for the unsupervised classification of a multispectral image. It is recommended to read the Brief Introduction to Remote Sensing before this tutorial, and in particular the part Clustering.

Clustering can be used for unsupervised classification, which means that no training input is required, producing classes (i.e. clusters) that have no definition and consequently the user must assign a land cover label to each class.

The purpose of the classification is to identify the land cover classes with the corresponding ID codes defined in the following table.

Classes
Class name Class ID
Water 1
Built-up 2
Vegetation 3
Soil 4

Following the video of this tutorial.

### 5.2.1.1. Input Data¶

Any multispectral image can be used for unsupervised classification. In this tutorial, we are going to use a subset of a Sentinel-2 Satellite image (Copernicus land monitoring services) and use the bands illustrated in the following table. You can download the image from this archive (about 30 MB, © Copernicus Sentinel data 2020 downloaded from https://scihub.copernicus.eu/), and then unzip the downloaded file.

Sentinel-2 Bands Central Wavelength [micrometers] Resolution [meters]
Band 2 - Blue 0.490 10
Band 3 - Green 0.560 10
Band 4 - Red 0.665 10
Band 5 - Vegetation Red Edge 0.705 20
Band 6 - Vegetation Red Edge 0.740 20
Band 7 - Vegetation Red Edge 0.783 20
Band 8 - NIR 0.842 10
Band 8A - Vegetation Red Edge 0.865 20
Band 11 - SWIR 1.610 20
Band 12 - SWIR 2.190 20

Start QGIS and the SCP.

The downloaded product is a Sentinel-2 Level 2A image, which is already atmospherically corrected (Level 1C images require atmospherical correction as described in Image conversion to reflectance). Nevertheless, preprocessing is required to convert pixel values to a decimal value of reflectance.

Conversion to reflectance (see Radiance and Reflectance) can be performed automatically. In order to convert bands to reflectance, open the Preprocessing clicking the button in the SCP menu or the SCP dock, and select the tab Sentinel-2.

Click the button Directory containing Sentinel-2 bands and select the directory of Sentinel-2 bands. The list of bands is automatically loaded in the table Metadata.

You can ignore Select metadata file because this conversion doesn’t require additional information.

Check the option Create Band set and use Band set tools in order to create automatically the input Band set that will be used for clustering.

To start the conversion process, click the button RUN and select the directory where converted bands are saved. After a few minutes, converted bands are loaded and displayed (file name beginning with RT_).

Converted bands

Open the tab Band set clicking the button in the SCP menu or the SCP dock. The bands are already listed in the first Band set.

Band set

### 5.2.1.2. Clustering¶

Clustering requires the user to define the number of clusters (i.e. classes) expected in the image, which correspond to as many spectral signatures (i.e. seeds). During the first iteration clusters are produced calculating the pixel spectral distance with initial spectral signatures. Pixels are assigned according to the most similar spectral signature, therefore producing clusters. In this tutorial we are going to perform clustering using the ISODATA algorithm, which includes steps for merging clusters having similar spectral signatures and splitting clusters having too high variability. We are going to select randomly the starting spectral signatures and use the algorithm Euclidean Distance for distance calculation.

Open the tab Clustering clicking the button in the SCP menu or the SCP dock. In Select input band set we set 1 because we are going to classify the first Band set.

In Method check ISODATA. In Distance threshold set 0.01, which is the distance required for merging clusters.

In Number of classes set 10; we set a number higher than the land cover classes defined at the beginning of this tutorial because we try to account for spectral variability of classes. In Max number of iterations set 10; in general, the more the iterations the better the result.

In ISODATA max standard deviation we set the maximum standard deviation considered for splitting a class; we can set the value 0.2 here. In ISODATA minimum class size in pixels we can set 10.

We check the option Use random seed signatures to randomly selects the spectral signatures of seed pixels from Band set. Now click the button RUN and define the path of the classification output.

Clustering tool

After the process, the result is displayed in the map. The result of the unsupervised classification is a raster having values that don’t correspond to the class IDs defined at the beginning of this tutorial. The number of classes can vary depending on the various iterations.

Result of clustering

The mean spectral signature of output classes is displayed in the panel. We could also save these spectral signatures in a previously created Training input if we wanted to use these in other supervised classifications.

### 5.2.1.3. Reclassification of the output¶

We must identify the correspondence between raster classes and the land cover class IDs defined at the beginning of this tutorial.

We can perform this task through photointerpretation. We can display a Color Composite of bands: Near-Infrared, Red, and Green: in the Working toolbar, click the list RGB= and select the item 7-3-2 (corresponding to the band numbers in Band set). You can see that image colors in the map change according to the selected bands, and vegetation is highlighted in red (if the item 3-2-1 was selected, natural colors would be displayed).

Color composite RGB=7-3-2

We should take note of the corresponding values, similarly to the following table as example. Please note that you should define different values according to the result of your classification output.

Class name Cluster ID New class ID
Vegetation 1 3
Built-up 2 2
Vegetation 3 3
Vegetation 4 3
Vegetation 5 3
Soil 6 4
Soil 7 4
Water 8 1
Vegetation 9 3
Soil 10 4

Now we can reclassify the output raster according to out classification system.

Open the tool Reclassification clicking the button in the SCP menu or the SCP dock. Click Calculate unique values to automatically calculate unique values in the classification and fill the reclassification table. In New value set the corresponding New class ID.

Reclassification

When the table is complete, click the button RUN and define the path of the classification output. After the process, the raster is added to the map. We can define a symbology according to our classes.

Reclassified raster

Of course, the result of an unsupervised classification can have many classification errors. In this example, several pixels of soil have been classified as built-up. For reducing those errors we could repeat the clustering step increasing the Number of classes, reducing the value ISODATA max standard deviation, and increasing the value Distance threshold.

### 5.2.1.4. Refinement of the output¶

The classification could contain small patches of pixels that we would like to remove from the classification result.

We can use the tool Classification sieve to remove small patches of isolated pixel replacing them with the value of the largest neighbour patch. Open the tool Classification sieve clicking the button in the SCP menu or the SCP dock.

In Select the classification select the raster reclassified in the previous step (click to refresh the layer list if the classification is not listed). In Size threshold enter 2 to set the maximum size of the patch to be replaced (in pixel unit); all patches smaller than the selected number of pixels will be replaced by the value of the largest neighbour patch. Setting the value 2 we are going to remove isolated single pixels.

Sieve tool

In Pixel connection : select 8 as the type of pixel connection in a 3x3 window (i.e. diagonal pixels are considered connected). Now click the button RUN and define the path of the classification output. After the process, the raster is added to the map. We should see that single pixels have been replaced by the largest neighbour patch, resulting in a cleaner classification.

Refined classification

Well done! We have performed an unsupervised classification of a remote sensing image. We can improve the classification using some of the tools that will be described in other tutorials.