Gradients

You can compute the gradient of each band of an image with image.gradient(). For example, the following code computes the gradient magnitude and direction of the Landsat 8 panchromatic band:

// Load a Landsat 8 image and select the panchromatic band.
var image = ee.Image('LANDSAT/LC8_L1T/LC80440342014077LGN00').select('B8');

// Compute the image gradient in the X and Y directions.
var xyGrad = image.gradient();

// Compute the magnitude of the gradient.
var gradient = xyGrad.select('x').pow(2)
          .add(xyGrad.select('y').pow(2)).sqrt();

// Compute the direction of the gradient.
var direction = xyGrad.select('y').atan2(xyGrad.select('x'));

// Display the results.
Map.setCenter(-122.054, 37.7295, 10);
Map.addLayer(direction, {min: -2, max: 2, format: 'png'}, 'direction');
Map.addLayer(gradient, {min: -7, max: 7, format: 'png'}, 'gradient');
    

Note that gradient() outputs two bands: the gradient in the X-direction and the gradient in the Y-direction. As shown in the example, the two directions can be combined to get gradient magnitude and direction. The magnitude should look something like Figure 1.

gradient_sf
Figure 1. Panchromatic gradient magnitude for the Landsat 8 imagery over the San Francisco Bay area, California, USA.

发送以下问题的反馈:

此网页
Google Earth Engine API