Improved Classification
Let us now try to improve the classification based on the thoughts of the last chapter.
To do so, we increase the number of samples per class from 20 to 50 and instead of point geometries we use polygon geometries. The code itself stays the same if you use the same classes and class names. Just like for the previous version, you can alternatively click here to see an example with already collected samples.
//Access the boundary geometry of Raqqa
var extent_aoi = ee.FeatureCollection("FAO/GAUL/2015/level1")
.filter(ee.Filter.eq('ADM1_NAME', 'Raqqa')); //filter for entry that equals GAUL Level 1 feature named 'Raqqa'
print(extent_aoi, 'Extent AOI');
Map.addLayer(extent_aoi, {},'Extent AOI');
Map.centerObject(extent_aoi, 8);
//Access the Sentinel-2 Imagery of June 2020 with less than 10% cloud coverage
var s2a = ee.ImageCollection('COPERNICUS/S2_SR')
.filterBounds(extent_aoi)
.filterDate('2020-06-01', '2020-06-30')
.select('B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9','B11','B12')
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10));
print(s2a, 'Image Collection Raqqa June 2020');
//Create a single Image by reducing by Median and clip it to the extent of the geometry
var s2a_median = s2a.median()
.clip(extent_aoi);
//Print your Image to your console tab to inspect it
print(s2a_median, 'Median reduced Image Raqqa June 2020');
//Add your Image as a map layer
var visParams = {'min': 400,'max': [4000,3000,3000], 'bands':'B8,B3,B2'};
Map.addLayer(s2a_median, visParams, 'S2 Raqqa June 2020 Median');
////////////////////////////////////////
//Start collecting Training Data here
///////////////////////////////////////
//Now that we defined our desired classes, we will need to merge them into one featureCollection for being able to use them as training data.
var classMerge = water.merge(vegetation).merge(urban).merge(baresoil);
print(classMerge, 'Merged Classes');
// Now, we can finally start collecting training data based on our defined classes.
// To do so, we will use .sampleRegions. Please check the documentation to find out more about how this works!
// Also, keep in mind that .sampleRegions only works for Images, not for ImageCollections!
var training = s2a_median.sampleRegions({
collection: classMerge,
properties: ['landcover'],
scale: 30
});
print(training.limit(5000), 'Training Data');
//Classification using CART Classifier:
var cartclassifier = ee.Classifier.smileCart().train({
features: training,
classProperty: 'landcover'
});
//Run the classification
var cartclassified = s2a_median.classify(cartclassifier);
//Display classification
Map.addLayer(cartclassified,
{min: 0, max: 3, palette: ['blue', 'green', 'orange','yellow']},
'CART classification');
//Classification using RandomForest Classifier:
var rfclassifier = ee.Classifier.smileRandomForest(50).train({
features: training,
classProperty: 'landcover'
});
//Run the classification
var rfclassified = s2a_median.classify(rfclassifier);
//Display classification
Map.addLayer(rfclassified,
{min: 0, max: 3, palette: ['blue', 'green', 'orange','yellow']},
'RF classification');
If we now compare the results of the previous and the improved classifications, we can see that the improved version has much finer gradations and does a better job differentiating the classes.