Image Classification

Google Earth Engine enables users to conduct a wide range of geospatial analysis, including Machine Learning.

Supervised image classification can be used for land cover analysis, for example. An example of this in action is here:

Step by Step:

Loading the Image:

var s2 = ee.ImageCollection("COPERNICUS/S2_SR")

// Perform supervised classification
// Find the feature id by adding the layer to the map and using Inspector.


var filtered = s2
    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30))
    .filter(ee.Filter.date('2022-01-01', '2022-01-28'))
    .filter(ee.Filter.bounds(geometry))

var composite = filtered.median().clip(geometry) 


var rgbVis = {min: 0.0, max: 12000, bands: ['B4', 'B3', 'B2']};
Map.addLayer(composite, rgbVis, 'image');

Create the Training Samples

Creating FeatureCollections is straightforward from the map panel- here, we have selected water features from the image and assigned them a Property (landcover) and Value (1):

This results in something like this:

Train the Classifier:

//Assign the 'landcover' property:

// ice: 0
// water: 1
// rock: 2

var gcps = ice.merge(ice).merge(water).merge(rock)

var training = composite.sampleRegions({
   collection: gcps, 
   properties: ['landcover'], 
   scale: 20  ,
   tileScale: 16
   });
 print(training)


// // Train the classifier.
 var classifier = ee.Classifier.smileRandomForest(50).train({
   features: training,  
   classProperty: 'landcover', 
   inputProperties: composite.bandNames()
 });
 

Run the Classifier:

// Classify the image.
 var classified = composite.classify(classifier);
 Map.addLayer(classified, {min: 0, max: 2, palette: ['white', 'blue', 'brown']}, 'Classified'); 

Results:

Last updated