0

I have classified an image with 50 clusters (k-means). The next step is to reclassify the image into several meaningful classes (e.g. vegetation, urban, water etc...). I want to take an automated approach as I will scale this up later to a much larger region. I intend to use some indices to do this. For this example, I have calculated the NDVI for this region and would like to use a representative range of values from the NDVI to reclassify the image.

I have calculated the zonal statistics (mean NDVI) for each of the 50 clusters using the reduceRegion.group() code. For example, in the image below, the light-blue, dark-blue and maroon clusters represent water, and each have a mean NDVI of <0 (as outputted by the zonal statistics). My question is, how could I use this information to combine those clusters into a new class called 'water' using the mean NDVI value from output of the zonal statistics, and an NDVI threshold of <0 as the condition?

link to my script: https://code.earthengine.google.com/79116167bb45b4d24f33abc42c8d99bb

enter image description here

here is my code:

    // load ref data
var mangrove_ref = ee.ImageCollection("LANDSAT/MANGROVE_FORESTS"); {
var srtm = ee.Image("USGS/SRTMGL1_003");
var country = ee.FeatureCollection("users/Madsamjp/eez_country").filterMetadata('NAME_0', 'equals', 'Bangladesh')}
Map.setCenter(89.52, 22.15)

// clip and mask study region
var mangrove_clip = mangrove_ref.map(function(image) {
  return image.clip(country); }); {  // this function clips the image collection to the variable 'country'
var water_mask = mangrove_clip.map(function mask_NDVI(image) {
  var water = bangladesh.lt(0.4);
  var select = water.select(['NDVI']);
  var binary = select.eq(0);
  return image.updateMask(binary)}); // creates a mask
var reduce = water_mask.reduce(ee.Reducer.sum()); // reduces mask to single image
var clip = bangladesh.mask(reduce)} // masks area for stats

// k-means cluster algorithym
var selected_bands = bangladesh.select(['B1', 'B2','B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11']); {
  var training = selected_bands.sample({
  scale: 30,
  factor: 0.001,
});
var clusterer = ee.Clusterer.wekaKMeans(50).train(training);
var result = selected_bands.cluster(clusterer);}

// add NDVI band to cluster image
var select_ndvi = bangladesh.select(['NDVI']);
var add_ndvi = (select_ndvi).addBands(result, ['cluster']);

// zonal statistics (mean NDVI for each class of clusters)
var means = add_ndvi.reduceRegion({
  reducer: ee.Reducer.mean().group({
    groupField: 1,
    groupName: 'class',
  }),
  geometry: bangladesh.geometry(),
  scale: 30,
  maxPixels: 1e12
}); print(means, 'mean ndvi for cluster classes');// ----->

// how to reclassify the clusterer output using the mean NDVI values for each of the cluster classes by setting a threshold.
// e.g. all clusters with a mean NDVI <0 should be reclassified to '1' - water, between 0.4 - to 0.7 to '2' - vegetation etc. 

Map.addLayer(add_ndvi, {min: -0.4, max: 0.8, bands: ['NDVI']}, 'NDVI');
Map.addLayer(result.randomVisualizer(), {}, 'cluster');

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.