[GIS] Normalization in Google Earth Engine

googlegoogle-earth-engine

How can I normalize pixel values from 0 to 1 or 0 to 100?
Is there any function or command related to it? I'm using JavaScript.

Best Answer

If you want that done on a known or preset min and max values, apply unitScale(low, high) directly on the image you have.

If you want to calculate the min and max value on a per image, per band value, use the following code:

// get one image
var image = ee.ImageCollection("LANDSAT/LC08/C01/T1_RT").first();
// calculate the min and max value of an image
var minMax = image.reduceRegion({
  reducer: ee.Reducer.minMax(),
  geometry: image.geometry(),
  scale: 30,
  maxPixels: 10e9,
  // tileScale: 16
}); 
// use unit scale to normalize the pixel values
var unitScale = ee.ImageCollection.fromImages(
  image.bandNames().map(function(name){
    name = ee.String(name);
    var band = image.select(name);
    return band.unitScale(ee.Number(minMax.get(name.cat('_min'))), ee.Number(minMax.get(name.cat('_max'))))
                // eventually multiply by 100 to get range 0-100
                //.multiply(100);
})).toBands().rename(image.bandNames());

// add to the map
Map.addLayer(image, {min: 0, max: 35000, bands: ['B4', 'B3', 'B2']}, 'original')
Map.addLayer(unitScale, {min: 0, max: 1, bands: ['B4', 'B3', 'B2']}, 'unitscaled')
Map.centerObject(unitScale)

Code link