1

https://code.earthengine.google.com/e430e2a79aecb65e3efbce23779a0abc

I have written this code for supervised classification using the cart classifier. Now I want to extract the no. of pixels in each class i.e. water, urban, vegetation etc.

var rgb_vis = {min: 0, max: 0.3, bands: ['B4', 'B3', 'B2']};
var image =  ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA')
  .filterBounds(ganga)
  .filterDate('2009-10-01', '2009-10-31')
  .median();
var region = image.clipToCollection(ganga);
// Map.addLayer(region, rgb_vis,'map');
//Data
// print(region);
var fc = water.merge(vegetation).merge(urban).merge(fallow);
var bands = ['B2','B3','B4','B5','B6','B7'];
//Train
var training = region.select(bands).sampleRegions({
  collection:fc,properties: ['landcover'],scale : 30});
var classifier = ee.Classifier.cart().train({features: training, classProperty: 'landcover', inputProperties: bands});
//Classification
var classified = region.select(bands).classify(classifier);
// print(classified);
//Display
Map.centerObject(fc, 11);
// Map.addLayer(region, rgb_vis, 'Landsat image');
// Map.addLayer(classified, {min: 0, max: 3, palette: ['0FD1F8','0E8205', 'EAF10B','EE0BF1']}, 'classification');
var output = classified.visualize({min:0,max:3,palette: ['0FD1F8','0E8205', 'EAF10B','EE0BF1'],forceRgbOutput:true});
Export.image.toDrive({image:output, region:geometry,description:'2009', scale: 30,crs:'EPSG:4326'});
Map.addLayer(output)
  • you need to make your assets public, or else no one can debug. Also, you might want to explain your question a bit more clearly in terms of the code you have written and where/how you got stuck. – John Powell Dec 13 '18 at 8:43
1

You can calculate the number of pixels per class by applying the frequencyHistogram reducer to your study region:

https://code.earthengine.google.com/f8e3d94f3b1af7f859f8203f135b3f0a

// calculate number of pixels per class
var classCount = classified.reduceRegion({
  reducer: ee.Reducer.frequencyHistogram(),
  geometry: ganga,
  scale: 30,
  maxPixels: 10e9})
  • In the above code, I have checked the accuracy of my training data, how I can validate the final classified image. I have tried the one on developers.google.com/earth-engine/classification . This method is not working, might be the case of classifier difference. – Shivam Dec 14 '18 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.