I have created 20 clusters from unsupervised classification of a specific region in Google Earth Engine. Now I want to reclass them. For example, let's say class values 10, 4, and 16 are forest, I want to combine these class values into a class that represents forest. Similarly, I want to do this for water, agricultural land, etc. How can I do that?

link code

var poi=ee.Geometry.Point([78.032188,30.316496]).buffer(35000);

var dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
                  .filterDate('2015-01-01', '2015-12-31');
var trueColor321 = dataset.select(['B3', 'B2', 'B1']);
var tru=trueColor321.reduce(ee.Reducer.mean());
var truclip= tru.clip(poi);
var trueColor321Vis = {
  min: 0.0,
  max: 0.4,
  gamma: 1.2,
Map.setCenter(78.032188,30.316496, 9);
Map.addLayer(truclip, trueColor321Vis, 'True Color (321)');

var dataset1=ee.ImageCollection('LANDSAT/LC08/C01/T1_32DAY_NDVI').filterDate('2015-01-01','2015-12-31');
var NDVI= dataset1.select('NDVI');
var ndvimean=NDVI.reduce(ee.Reducer.mean());
var ndviclip=ndvimean.clip(poi);
var visparam={
  min: 0.0,
  max: 1.0,
  palette: [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'

var training=ndviclip.sample({
  region : poi,
  scale : 30,
  numPixels : 5000
var clusterer=ee.Clusterer.wekaKMeans(20).train(training);

var result = ndviclip.cluster(clusterer);


var subset= result.select("cluster").eq(4).selfMask();
Map.addLayer(subset,{},'only 19');

Use the remap function. Just to give you an example, here I remapped your band values to ones and other values to zeros.

// merge several clusters together

  // Define your from and to values
  var fromForest = [10,4,16]; // original values
  var toForest = [1,1,1]; // values to give to the pixels

  var fromAgricultural = [1,2,3]; // original values
  var toAgricultural = [1,1,1]; // values to give to the pixels

  var fromBuildup = [5,6,13]; // original values
  var toBuildup = [1,1,1]; // values to give to the pixels

  // remap the pixel values and rename the bands accordingly
  var Forest = result.remap(fromForest, toForest, 0, 'cluster').rename('Forest');
  var Agricultural = result.remap(fromAgricultural, toAgricultural, 0, 'cluster').rename('Agricultural');
  var Buildup = result.remap(fromBuildup, toBuildup, 0, 'cluster').rename('Buildup');

  // concatenate to one image
  var image = ee.Image.cat([Buildup, Forest, Agricultural]);

Map.addLayer(image.clip(poi),{},"merged clusters");

Link to code

  • 2
    To expand on Kuik's answer a bit... this creates a 3-band image (1 band per class). If you want all final classes in the same band, you can use the described .remap() method, just make two corresponding lists: "fromValuesAll" and "toValuesAll" that include corresponding values for all of the changes you want to make. Mar 11 '20 at 20:36
  • Yes you are right, I just choose to store values in different bands so you can conveniently rename them to a user friendly name. If you want to continue you analysis, it's probably better store each class with a different integer value in a single band image.
    – Kuik
    Mar 12 '20 at 19:19

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.