GEE Tutorials

Google Earth Engine Tutorial: Beginners Guide 25 Edge Detection with Landsat 8

Credit: Youtube Channel “Terra Spatial, Tutorial on applying edge detection algorithms to Landsat 8 imagery for feature extraction.”

You can see all the tutorials from here: Techgeo Academy.

Google Earth Engine Tutorial: Beginners Guide to Edge Detection with Landsat 8

Edge detection is a fundamental technique in image processing and remote sensing used to identify boundaries between different features in an image. In this tutorial, you’ll learn how to perform edge detection using Landsat 8 imagery in Google Earth Engine (GEE). This method can help identify features like roads, water bodies, and land use changes.

1. Setting Up the Environment

Before starting, ensure you have a Google Earth Engine account and access to the Code Editor. Launch the GEE Code Editor at https://code.earthengine.google.com. Load the appropriate libraries and define the geometry of the area you want to analyze.

2. Loading and Preprocessing Landsat 8 Data

Start by loading the Landsat 8 dataset and selecting the relevant bands. You can filter the images by date, location, and quality. For example, the following code loads the Landsat 8 collection and selects the blue (Band 2), green (Band 3), and red (Band 4) bands:


var dataset = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2")
.filterDate('2023-01-01', '2023-12-31')
.filter(ee.Filter.eq('WRS_PATH', 133))
.filter(ee.Filter.eq('WRS_ROW', 42))
.filterMetadata('CLOUD_COVER', 'less_than', 10);
var image = dataset.select(['SR_B2', 'SR_B3', 'SR_B4']).median();

Replace the WRS_PATH and WRS_ROW values with your target study area. The median function reduces noise and improves clarity.

3. Applying Edge Detection

To perform edge detection, use the Sobel filter, a common 3×3 kernel for detecting horizontal and vertical edges. In GEE, create custom kernels and convolve them with your image. Here’s the code for the Sobel kernel:


var sobelX = ee.Kernel.convolution({
dimensions: [3, 3],
values: [ [ -1, 0, 1 ],
[ -2, 0, 2 ],
[ -1, 0, 1 ] ]
});

var sobelY = ee.Kernel.convolution({
dimensions: [3, 3],
values: [ [ -1, -2, -1 ],
[ 0, 0, 0 ],
[ 1, 2, 1 ] ]
});

var edgesX = image.convolve(sobelX);
var edgesY = image.convolve(sobelY);
var edgeMagnitude = edgesX.sqrt().add(edgesY.sqrt()).sqrt();

This creates two kernels (horizontal and vertical) to highlight edges and calculates the magnitude of the gradient for the final edge map.

4. Visualizing the Edges

Display the edge detection result in the GEE Map. Use the visualization parameters to highlight the detected edges:


Map.setCenter(-119.61, 36.01, 10);
Map.addLayer(edgeMagnitude, {min: 0, max: 1000}, 'Edge Detection');

Adjust the min and max values based on your dataset to ensure visibility of edges.

5. Saving the Output

Export the edge detection results as a GeoTIFF file for further analysis. Replace the file path and export parameters as needed:


Export.image.toDrive({
image: edgeMagnitude,
description: 'edge_detection_export',
folder: 'GEE_Exports',
fileNamePrefix: 'edges',
region: geometry,
crs: 'EPSG:4326',
scale: 30,
maxPixels: 1e10
});

Use the Export API to save your results directly to Google Drive.

FAQ

What is the best band for edge detection in Landsat 8?

There is no universal “best” band. For vegetation or land cover, consider using the near-infrared (Band 5). For visible features, use Band 4 (red) or Band 3 (green). Adjust based on your specific analysis goals.

How do I handle cloud cover in edge detection?

Use the QA band (Band QA) to mask clouds. Add a cloud masking step by filtering the image collection based on the CLOUD_COVER metadata. Replace filterMetadata('CLOUD_COVER', 'less_than', 10) with your desired threshold.

Can I use other edge detection algorithms in GEE?

Yes, GEE supports custom kernels. Implement algorithms like the Canny edge detector or Laplacian of Gaussian (LoG) manually by creating their respective kernels. These techniques require more complex workflows but offer additional flexibility.

Why are my edges not visible in the visualization?

Ensure the pixel range is appropriate. Use the Inspector tool in the GEE Code Editor to check the minimum and maximum values. Adjust the visualization parameters to improve contrast.

How do I save the edge detection results?

Use the Export.image.toDrive() function. Make sure your export region matches the study area and adjust the scale and crs (coordinate reference system) as required.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *