您可以使用 image.gradient()
计算图片的每个波段的梯度。
例如,以下代码会计算 Landsat 8 全色波段的梯度幅度和方向:
Code Editor (JavaScript)
// Load a Landsat 8 image and select the panchromatic band. var image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318').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');
请注意,gradient()
会输出两个带:X 方向的梯度和 Y 方向的梯度。如示例所示,这两个方向可以组合起来,以获取梯度幅度和方向。幅度应如下图所示。
