您可以使用 image.gradient()
計算圖片各頻帶的梯度。舉例來說,以下程式碼會計算 Landsat 8 全色帶的漸層大小和方向:
程式碼編輯器 (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 方向的漸層。如範例所示,這兩個方向可以結合,以取得漸層大小和方向。幅度應如下圖 1 所示。
