Tuesday, 7 February 2017

Normalization in Google 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.



Answer



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


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 the 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)

link


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...