In this class we will try 2 different types of image classification: 1) Supervised Classification, 2) Indices Based Classification.
We will first select the data, and then perform these 2 different types of image classification.
If you have any problem selecting the data, you can check the Practice 1
In this tutorial, we are going to use USGS Landsat 9 Level 2 Collection Tier 1 product. Please check the data before you start (https://developers.google.com/earth-engine/datasets/catalog/LANDSAT_LC09_C02_T1_L2).
For this practice, let's draw the AOI around Sapporo City - Hokkaido, Japan.
The dates we are going to use are '2022-09-08' and '2022-09-10'
Attention to the variable names!
Please, remember the workflow of Supervised Classification and follow the steps shown on the orange boxes below:
Now that you already have an image, we need to create training samples. Training samples are necessary to teach the classifier how to classify the image, in simple words, the classifier groups together pixels with similar attributes.
In this tutorial, please make a simple classification map to focus on the GEE programming. Please use/refer to the class ID and category in the following Table 1. If necessary, modify it.
Table 1 - Landcover classes
For training samples, you can create points or polygons
To create training samples, you can select points or polygons. In this tutorial, we are going to use points.
> Go to Geometry Imports and click on + new layer
> Click on the setting button
> Change the name to Forest > select Feature Collection > change the color of the class (in this case Green)
> Click in + Property to add a new property and insert 'class' and value 1
This value must correspond to the class you determined before (Table 1). This is important because GEE uses the values to identify the classes
> Click in OK.
(If you want to remove the class, you can click on the trash can)
After that, go to the Map Window to create the training samples
> click on the Geometry Tools to draw points or polygons.
(if you choose polygons, use the left click to start drawing the polygon and double click to close it)
Two or three training samples are enough for this practice.
When drawing training samples, you can use the Google Satellite Imagery to support you
> Click in Satellite and uncheck/check the image in Layers
Please, be careful when comparing your image with Google Satellite Imagery, they might be taken on different periods of time.
Repeat the same steps for the other classes. Be aware to change the value of each class: Grassland (2), Bareland_Urban (3), and Water (4)
You can see the training samples on the import pane
Now, we need to prepare the training samples
> Add the code below on lines 11 and 13
The code on line 11 put the training samples inside one big package, which means that the variable classes has all training samples we draw on the map inside.
The code on line 13 extracts the pixel information from the image to teach each training sample. The number 30 means the spatial resolution of the sampling, in this case 30 meters.
We need to select and train a classifier to create a classification map. This time we are selecting the .libsvm() classifier and training this classifier with the training samples we created previously.
> Add the code below on lines 15 and 17
The code on line 15 selects which classifier we are going to use, in this case the Support Vector Machine classifier. On line 17 we are training the classifier with the training samples we created previously.
You can find more classifiers in the docs pane
> Go to Docs > type classifier
You can see the classifiers that the GEE offers, and how to use them (which arguments are necessary). You can try to use different ones, just change the code on line 15
After training the classifier (trained_classifier), you are able to classify your image
> Add the code below to apply the classifier on line 19
We are almost there, now we need to plot the classified map on the Map Window.
> Add the code below on line 21
The Map.addLayer() function adds the image to the Map Window.
The min: and max: shows the minimum and maximum values from the landcover classes, and inside the list, we define the colors to be shown.
You can use this website to define the colors to be displayed for each class
There are many ways to improve the classification, and one of them is to use more training samples. The other one is to use different classifiers as explained earlier.
The comment function (//) is very useful to try different classifiers since the GEE does not execute the comments:
You can create a list of classifier and select any code you want
Let's use the same code, we need to start from the beginning by cleaning the script area and selecting the data
Please remember the workflow for Indices-Based classification (blue boxes)
For this practice, we are going to use the Normalized Difference Vegetation Index, which is commonly know as NDVI. The NDVI is an indicator of the vegetation greenness and it is defined by the following formula
Let's calculate NDVI inside GEE
> Add the code below from line 11
This is a try and error method, in this case we use three classes defined as follow:
-1 to 0.05 : 1 water
0.05 to 0.33 : 2 Bareland etc.
0.33 to 1.00 : 3 Vegetation
Let's reclassify the NDVI
> Add the code below after line 15
> Add the following code on line 20 to see the classified map on the Map Window
You can change the number of classes like below
Please don't forget to change line 21, otherwise you will not see any difference on the classified map
var bands = ['SR_B7','SR_B6','SR_B5','SR_B4','SR_B3','SR_B2'];
var image = landsat9sr
.filterDate('2022-09-08', '2022-09-10')
.filterBounds(AOI)
.select(bands)
.median()
.clip(AOI);
Map.addLayer(image, {bands:['SR_B4','SR_B3','SR_B2'], min:7000, max:19000, gamma:2}, 'image');
var classes = Forest.merge(Grassland).merge(Bareland_Urban).merge(Water);
var training_samples = image.sampleRegions(classes, ['class'], 30);
var classifier = ee.Classifier.libsvm();
//var classifier = ee.Classifier.minimumDistance();
//var classifier = ee.Classifier.smileCart();
//var classifier = ee.Classifier.smileRandomForest({numberOfTrees:10});
var trained_classifier = classifier.train(training_samples, 'class');
var classified = image.classify(trained_classifier);
Map.addLayer(classified, {min:1, max:4, palette:['green','lime','orange', 'blue']});
var bands = ['SR_B7','SR_B6','SR_B5','SR_B4','SR_B3','SR_B2'];
var image = landsat9sr
.filterDate('2022-09-08', '2022-09-10')
.filterBounds(AOI)
.select(bands)
.median()
.clip(AOI);
Map.addLayer(image, {bands:['SR_B4','SR_B3','SR_B2'], min:7000, max:19000, gamma:2}, 'image');
var Nir = image.select('SR_B5');
var Red = image.select('SR_B4');
var NDVI = Nir.subtract(Red).divide(Nir.add(Red)).rename('NDVI');
var NDVI_classification = ee.Image(1)
.where(NDVI.gt(0.05).and(NDVI.lte(0.33)),2)
.where(NDVI.gt(0.33).and(NDVI.lte(0.40)),3)
.where(NDVI.gt(0.40).and(NDVI.lte(1.00)),4)
.clip(AOI);
Map.addLayer(NDVI_classification, {min:1, max:4, palette:['blue','orange',' lime',' green']},'NDVI Class');