Create probability maps

using the random forest classifier

step 1: add the image and training data

var image = ee.Image("users/servirmekong/Vietnam/Sent2_2017_Forest001")
var referenceData = ee.FeatureCollection("users/servirmekong/Vietnam/ReferenceDataForestTraining");
var chl = ee.FeatureCollection("users/servirmekong/Vietnam/CHL_Boundary").geometry();

step 2: select the training data

var water = referenceData.filter(ee.Filter.eq("land_type","water"));
var crop = referenceData.filter(ee.Filter.eq("land_type","crop"));
var urban = referenceData.filter(ee.Filter.eq("land_type","urban"));
var plantations = referenceData.filter(ee.Filter.eq("land_type","plantations"));
var forest = referenceData.filter(ee.Filter.eq("land_type","forest"));

step 3: Set the class to 1 and all others to 0

var setClass = function(feat){
  return feat.set("land_class",1)
}

var setOther = function(feat){
  return feat.set("land_class",0)
}

var primi = crop.map(setClass)
var other = water.map(setOther).merge(urban.map(setOther)).merge(plantations.map(setOther)).merge(forest.map(setOther))

step 4: Filter the other class to have about an equal number of data points in both categories.

other = other.randomColumn("random")
other = other.filter(ee.Filter.lt("random",0.25))

step 5: Display the data points

Map.addLayer(primi.draw("white"),{},"primitives")
Map.addLayer(other.draw("black"),{},"other")

step 6: Merge the data

var referenceData = other.merge(primi)

step 7: Create the training sample and train the classifier

var trainingSample = image.sampleRegions(referenceData,["land_class"],10);

var bandNames = image.bandNames();
var classifier = ee.Classifier.randomForest(100,0).setOutputMode('PROBABILITY').train(trainingSample,"land_class",bandNames);

step 8: classify the image and add it to the map.

var classification = image.classify(classifier).multiply(100)

Map.addLayer(classification,{min:20,max:80,palette:"white,gray,black"},"primitive")

example

One comment

Leave a Reply