1

I have classified my study area into six landcover classes (water, urban, barrenland,plantation, nonmangroves forests and mangroves) using Random Forest. I used six bands for classification (red, blue, green, nir, NDVI, NDWI). I now wish to get the NDVI values of all pixels that have been classified as 'mangroves' by my classifier. Can someone show me how to do that? Here is my code:

//i) Merge hand-drawn polygons into one FeatureCollection: 

var FCmerged=water.merge(urban).merge(barrenland).merge(plantation).merge(nonMangrove).merge(mangrove); 
print(FCmerged.size()); 
Map.addLayer(FCmerged, {}, 'FCMerged'); 


//ii) Select bands to be used for classification: 
var bands = ['B2', 'B3', 'B4','B5','NDWI','NDVI'];
print(bands,'bands'); 


//iii) Using Stratified RandomSampling to generate training points within the polygons: 
var FCimage=ee.Image().byte().paint(FCmerged, "lc").rename("lc")
print(FCimage, 'image'); 

var stratifiedsample=FCimage.stratifiedSample({
  numPoints:1200, 
  classBand:"lc",
  region:ROI,
  scale:30, 
  classValues:[0,1,2,3,4,5], 
  classPoints:[200,200,200,200,200,200],
  geometries:true
}) 

// iv) Collect and map training points: 
print('Stratified samples',stratifiedsample); 
print(stratifiedsample.reduceColumns(ee.Reducer.frequencyHistogram(),['lc']).get('histogram','No of points')); 

//v) Extract pixel values of the training points:

var trainingPoints=l8.select(bands).sampleRegions({
  collection:stratifiedsample,
  properties:["lc"],
  geometries:true,
  scale:30,
})
print(trainingPoints,{},'trainingPoints'); 
Map.addLayer(trainingPoints, {},'trainingPoints',false);

 
// vi) Split the training points by 70%/30% 
var sample=trainingPoints.randomColumn();

var split = 0.7;  // Roughly 70% training, 30% testing.
var training = sample.filter(ee.Filter.lt('random', split));
print(training,'points for classifier'); 
var test = sample.filter(ee.Filter.gte('random', split));
print(test,'Testing points'); 


// vii) Build the Random Forest classifier: 
var RandomForest=ee.Classifier.smileRandomForest(1000).train({
  features:training,
  classProperty:'lc',
  inputProperties:bands,
}); 
print('Random forest, explained', RandomForest.explain());


//viii) Classify L8 using RandomForest:  
var L8Classified=l8.select(bands).classify(RandomForest);  //here l8 is an imagecomposite of landsat 8 images for 2020. 
print(L8Classified, 'L8 Classified'); // this output is an image


//ix) Visualisation parameters: 
var lcPalette =  [
  '0000FF', // water (0) (blue)
  'FF00FF', // urban (1) (fuschia)
  '008080',//barrenland (2) (teal)
  'FFFF00',//  plantation (3) (yellow)
  '008000',//nonmangroveforests (4) (green)
  'FF0000', // mangroveforest (5) (red)
];
//x) Add Classified landcover onto map: 
Map.addLayer(L8Classified, {palette:lcPalette, min:0,max:5}, 'LULCC_2020'); 

// xi) Check the acuracy of the classification using a confusion matrix: 

var confusionMatrix = RandomForest.confusionMatrix();
print('Classification accuracy: ', confusionMatrix.accuracy());

//xii) Validate the RandomForest Classification using test points: 
var testEvaluation = test.classify(RandomForest); 
print('Test Evaluation', testEvaluation);  //this output is a Feature Collection

// xiii) Get a confusion matrix representing expected accuracy.
var testAccuracy = testEvaluation.errorMatrix('lc', 'classification');
print('Validation error matrix: ', testAccuracy);
print('Validation overall accuracy: ', testAccuracy.accuracy());

//xiv) Extract NDVI of classified mangrove class to study mangrove health: 
var mangroveNDVI = L8Classified.updateMask(L8Classified.eq(5)); // mask it
print(mangroveNDVI); 
Map.addLayer(mangroveNDVI,{},'Crops');

1 Answer 1

0

I'd suggest reading these 1) how to get statistics of an image region https://developers.google.cn/earth-engine/guides/reducers_reduce_region?hl=zh-cn and 2) histogram charts https://developers.google.cn/earth-engine/guides/charts_image?hl=zh-cn#uichartimagehistogram

At the end of your code, you should mask NDVI with the mangrove class and do image reductions for an area of interest to get mean, min, and max, and a histogram chart to see the distribution. Play with the scale to get it to run in GEE.

It's tough to check if what I provided is right for the code you provided because we don't have the hand drawn polygons. I may have messed something up below. So here is a working example https://code.earthengine.google.com/67d338a8643a2069c80c7de4b7147c31

//mask non-mangrove pixels
var mangroveNDVIPixels = FCimage.select('NDVI').updateMask(FCimage.eq(5));


//get values min max mean
var meanDictionary = mangroveNDVIPixels.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: geometry,//area of interest
  scale: 480,
  maxPixels: 1e9
});
print(meanDictionary)

var minMaxDictionary = mangroveNDVIPixels.reduceRegion({
  reducer: ee.Reducer.minMax(),
  geometry: geometry,//area of interest
  scale: 480,
  maxPixels: 1e9
});
print(minMaxDictionary)

//chart NDVI values
var chart =
    ui.Chart.image.histogram({image: mangroveNDVIPixels, region: geometry, scale: 480})
        .setSeriesNames(['NDVI'])
        .setOptions({
          title: 'NDVI Histogram',
          hAxis: {
            title: 'NDVI',
            titleTextStyle: {italic: false, bold: true},
          },
          vAxis:
              {title: 'Count', titleTextStyle: {italic: false, bold: true}},
          colors: ['cf513e', '1d6b99', 'f0af07']
        });
print(chart);  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.