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')
  .filterDate('2009-10-01', '2009-10-31')
var region = image.clipToCollection(ganga);
// Map.addLayer(region, rgb_vis,'map');
// print(region);
var fc = water.merge(vegetation).merge(urban).merge(fallow);
var bands = ['B2','B3','B4','B5','B6','B7'];
var training = region.select(bands).sampleRegions({
  collection:fc,properties: ['landcover'],scale : 30});
var classifier = ee.Classifier.cart().train({features: training, classProperty: 'landcover', inputProperties: bands});
var classified = region.select(bands).classify(classifier);
// print(classified);
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'});
  • 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. Dec 13, 2018 at 8:43

1 Answer 1


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


// 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, 2018 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.