Announcement : On
November 13, 2024 , all users will need to
use a Cloud project in order to access Earth Engine. After this date, continued individual access without a Cloud project will require
an exception .
Send feedback
Gradients
Stay organized with collections
Save and categorize content based on your preferences.
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:
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 ');
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.
Figure 1. Panchromatic gradient magnitude for the Landsat 8 imagery over the
San Francisco Bay area, California, USA.
Send feedback
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.
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Missing the information I need"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Too complicated / too many steps"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Out of date"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Samples / code issue"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]
Need to tell us more?
{"lastModified": "Last updated 2023-10-06 UTC."}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-10-06 UTC."]]