2

I am trying to generate histogram of pixel values for my sample polygons. I used the code suggested on Histogram Matching in Google Earth Engine. Its working but I am not able to control the bin size so I want to extract the raw data (that the code is using to create the histogram) in csv so that I can plot the histogram in excel with more control of histogram display. Here is what I have tried. Is it possible to get the pixel data that GEE is using to plot histogram.

var s2 = ee.ImageCollection("COPERNICUS/S2");
var admin1 = ee.FeatureCollection("FAO/GAUL_SIMPLIFIED_500m/2015/level1");
  
var MP = admin1.filter(ee.Filter.eq('ADM1_NAME', 'Madhya Pradesh'));
var geometry = MP.geometry();
var rgbVis = {min: 0.0, max: 3000, bands: ['B4', 'B3', 'B2']};
Map.centerObject(geometry,9.5);
var filtered1 = s2.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))
  .filter(ee.Filter.date('2020-03-15', '2020-03-21'))
  .filter(ee.Filter.bounds(geometry));
  function addIndices(image) {
  var ndvi = image.normalizedDifference(['B8', 'B4']).rename('ndvi');
  var ndwi = image.normalizedDifference(['B3', 'B8']).rename('ndwi');
  return image.addBands(ndvi).addBands(ndwi);
}
var withIndices1 = filtered1.map(addIndices);
var compositeA = withIndices1.median().clip(geometry);
var palette = [
  'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',
  '74A901', '66A000', '529400', '3E8601', '207401', '056201',
  '004C00', '023B01', '012E01', '011D01', '011301'];
  var ndviVis = {min:0, max:0.5, palette: palette };
  var NDVI = compositeA.select('ndvi');
  Map.addLayer(compositeA, rgbVis, 'Karnataka ');
  Map.addLayer(NDVI,ndviVis,'NDVI');
  
  
  ////////// Define a function to display histogram of a band////////
//////////NDVI:Unburned
var showHistogram = function(image, name, band) {
  var options = {
  title: 'Histogram of ' + name,
  fontSize: 20,
  hAxis: {title: 'DN'},
  vAxis: {title: 'count of DN'},
  };
    
  var histogram = ui.Chart.image.histogram({
  image: image.select(band),
  region: Unburned,
  maxBuckets: Math.pow(2, 8),
  scale: 20,
  });
  
  var histogram = histogram.setSeriesNames([band])
    .setOptions(options);
  print(histogram);
};

// Show histograms
showHistogram(compositeA, 'NDVI Unburned', 'ndvi');

//////////NDVI:Burned
var showHistogram = function(image, name, band) {
  var options = {
  title: 'Histogram of ' + name,
  fontSize: 20,
  hAxis: {title: 'DN'},
  vAxis: {title: 'count of DN'},
  };
    
  var histogram = ui.Chart.image.histogram({
  image: image.select(band),
  region: Burned,
  maxBuckets: Math.pow(2, 8),
  scale: 20,
  });
  
  var histogram = histogram.setSeriesNames([band])
    .setOptions(options);
  print(histogram);
};

// Show histograms
showHistogram(compositeA, 'NDVI Burned', 'ndvi');

1 Answer 1

2

You can use ee.Image.sampleRegions() (see here) on your created composite image to extract all pixel values within your sample polygons, and then export it as a CSV using Export.table.toDrive() (see here).

Example code:

var sampled_pixels = compositeA.sampleRegions(MP)
Export.table.toDrive(sampled_pixels) // CSV by default

Your Answer

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

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