I have classified an image into 20 classes using unsupervised classification for a specific region in Google Earth Engine. I want to check a specific class (say class 9) and visualize only that specific class.

How can I do that?

//loading the true image
var poi=ee.Geometry.Point([78.032188,30.316496]).buffer(35000);

var dataset = ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA')
                  .filterDate('2010-01-01', '2010-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)');
//ndvi calculation
var dataset1=ee.ImageCollection('LANDSAT/LT05/C01/T1_32DAY_NDVI').filterDate('2010-01-01','2010-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(100).train(training);

var result = ndviclip.cluster(clusterer);


Easily done. Add this to the bottom of your script. This checks every pixel, and if it equals 9, then it passes the test the new pixel is set to 1. otherwise, 0.

var subset = result.select("cluster").eq(9).selfMask();

Map.addLayer(subset,{},"only 9");

==== Edit ===

.selfMask() will hide all the pixels that don't match the criteria.

| improve this answer | |
  • Add .selfMask() to the end of the subset definition line to mask out all of the 0's (non-9-value pixels). – Justin Braaten Mar 9 at 16:44
  • Thanks Justin. Updated the answer. – Sean Roulet Mar 9 at 17:10

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.