Google Earth Engine – Applying Thresholds in Image Segmentation Using JavaScript

google-earth-engineimage segmentationjavascript

I am trying to segment the images based on the threshold values. For instance, I want to put a threshold range between > 800 and < 1200, but with my existing code in Google Earth Engine, I can put the only one function at a time that is greater than, below is my code, how to define greater than and less than at the same time.

 // Threshold the thermal band to set hot pixels as value 1, mask all else.
var hotspots = s2a.gt(3500) // i want put here both grater than and less than function
  .selfMask()
  .rename('hotspots');

// Display the thermal hotspots on the Map.
Map.addLayer(hotspots, {palette: 'FF0000'}, 'Hotspots');

Best Answer

Use the updateMask() function:

var hotspots = s2a.updateMask(s2a.gt(800)).updateMask(s2a.lt(1200))
        // set all values to 1 (instead of keeping the original values)
        .gt(800).selfMask()
        // rename the band
        .rename('hotspots');