ee.Image.normalizedDifference
Computes the normalized difference between two bands. If the bands to use are not specified, uses the first two bands. The normalized difference is computed as (first − second) / (first + second). Note that the returned image band name is 'nd', the input image properties are not retained in the output image, and a negative pixel value in either input band will cause the output pixel to be masked. To avoid masking negative input values, use
ee.Image.expression()
to compute normalized difference.
Usage | Returns |
---|
Image.normalizedDifference(bandNames) | Image |
Argument | Type | Details |
---|
this: input | Image | The input image. |
bandNames | List, default: null | A list of names specifying the bands to use. If not specified, the first and second bands are used. |
Examples
// A Landsat 8 surface reflectance image.
var img = ee.Image('LANDSAT/LC08/C02/T1_L2/LC08_044034_20210508');
// Calculate normalized difference vegetation index: (NIR - Red) / (NIR + Red).
var nirBand = 'SR_B5';
var redBand = 'SR_B4';
var ndvi = img.normalizedDifference([nirBand, redBand]);
// Display NDVI result on the map.
Map.setCenter(-122.148, 37.377, 11);
Map.addLayer(ndvi, {min: 0, max: 0.5}, 'NDVI');
Python setup
See the
Python Environment page for information on the Python API and using
geemap
for interactive development.
import ee
import geemap.core as geemap
# A Landsat 8 surface reflectance image.
img = ee.Image('LANDSAT/LC08/C02/T1_L2/LC08_044034_20210508')
# Calculate normalized difference vegetation index: (NIR - Red) / (NIR + Red).
nir_band = 'SR_B5'
red_band = 'SR_B4'
ndvi = img.normalizedDifference([nir_band, red_band])
# Display NDVI result on the map.
m = geemap.Map()
m.set_center(-122.148, 37.377, 11)
m.add_layer(ndvi, {'min': 0, 'max': 0.5}, 'NDVI')
m
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[[["Computes the normalized difference between two specified or default image bands using the formula (first - second) / (first + second)."],["Returns a single-band image named 'nd' representing the normalized difference."],["Input image properties are not preserved in the output, and negative input values in either band result in masked output pixels."],["`ee.Image.expression()` is recommended for handling negative input values and avoiding masking."]]],[]]