3

I am very new at GEE, and I’m trying to classify an image based on the EVI range of various vegetation classes. For example, Bamboo may have a minimum threshold of 0.6712 and maximum of 0.776 EVI. This way I have 10 classes. I obtained the EVI band from my Landsat image.

How do I classify the pixels and assign them values from 1-10 for the 10 classes?

I tried using ee.Algorithms.If method:

    var EVIband = ee.Image(CFL598.select('EVI'));
    print("evi", EVIband);
    Map.addLayer(EVIband, {min:-0.10, max:0.8, bands:['EVI']}, 'EVI Band');

    var ten = ee.Image(ee.Algorithms.If((EVIband.gte(0.6712).and(EVIband.lte(0.776))), EVIband.mask(ee.Number(10))));
    print("bamboo", ten);
    Map.addLayer(ten);

I was planning to do this for the rest of the 9 classes and use addBands to create a whole image, but this did not work.

0

1 Answer 1

6

EVIband.gte(0.6712).and(EVIband.lte(0.776) evaluates each pixel and returns 1 if the pixel value meets the conditions and 0 if not. If you want to give the pixels that meet this condition some identifiable number, easiest is to multiply this image by a constant ee.Image. Example:

var ten = EVIband.gte(0.6712).and(EVIband.lte(0.776).multiply(ee.Image(10))

I agree, I would sum over all the binary images to get a final class band. If you define a list of thresholds beforehand, it is possible to map a function over this list, rather than adopt a brute-force apprpoach. Example segmenting an EVI image (EVIband) into three classes:

var T1 = ee.List([0,0.3,0.6]) // define lower thresholds 
var T2 = ee.List([0.3,0.6,0.9]) // define upper thresholds 
var key = ee.List([1,2,3]) // unique "key" to assign to each class 

var classband = ee.ImageCollection.fromImages(
  
  // map over list of indices 
  ee.List.sequence(0,T1.length().subtract(1)).map(function(index){

    // return binary image 
    return EVIband.gte(ee.Number(T1.get(index)))
      .and(EVIband.lt(ee.Number(T2.get(index))))
      .multiply(ee.Image.constant(key.get(index)))
      .toUint16()
    })
  )

   // sum over all images 
  .sum() 

EDIT for completeness, example of brute-force approach:

var one = EVIband.gte(0).and(EVIband.lt(0.3)).toUint16()
var two = EVIband.gte(0.3).and(EVIband.lt(0.6)).multiply(ee.Image(2)).toUint16()
var three = EVIband.gte(0.6).and(EVIband.lt(0.9)).multiply(ee.Image(3)).toUint16()
var classband = ee.ImageCollection.fromImages([one,two,three]).sum()

which is actually less code than the previous example but, maybe not as elegant for the case of having 10 classes.

1
  • Thanks a lot for your answer! Both the methods worked great:)
    – Kavya
    Feb 14, 2021 at 19:34

Your Answer

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

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