Edge detection is a fundamental image processing technique used to identify boundaries between objects or regions in an image. The Canny Edge Detector
Edge detection is a fundamental image processing technique used to identify boundaries between objects or regions in an image. The Canny Edge Detector is a popular and effective algorithm for this purpose. This article will guide you through a Google Earth Engine (GEE) script that demonstrates how to use the Canny Edge Detector to extract edges from a Landsat image.
This Google Earth Engine script performs Canny edge detection on a Landsat 5
image. First, it calculates the Normalized Difference Vegetation Index (NDVI)
from the Landsat bands. Then, it applies the
ee.Algorithms.CannyEdgeDetector
algorithm to the NDVI image to
identify edges. Finally, it masks the resulting edge image to remove areas
where no edges were detected, and displays both the NDVI and the detected
edges on the map.
GEE Code Sample
// Canny Edge Detector example.
// Load an image and compute NDVI from it.
var image = ee.Image('LANDSAT/LT05/C02/T1_TOA/LT05_031034_20110619');
var ndvi = image.normalizedDifference(['B4','B3']);
// Detect edges in the composite.
var canny = ee.Algorithms.CannyEdgeDetector(ndvi, 0.7);
// Mask the image with itself to get rid of areas with no edges.
canny = canny.updateMask(canny);
Map.setCenter(-101.05259, 37.93418, 13);
Map.addLayer(ndvi, {min: 0, max: 1}, 'Landsat NDVI');
Map.addLayer(canny, {min: 0, max: 1, palette: 'FF0000'}, 'Canny Edges');
Step-by-Step Explanation:
-
Load Landsat 5 image:
var image = ee.Image('LANDSAT/LT05/C02/T1_TOA/LT05_031034_20110619');
This line loads a Landsat 5 Top of Atmosphere (TOA) reflectance image into a variable named
image
. The specific image is identified by its path, row, and date. -
Compute NDVI:
var ndvi = image.normalizedDifference(['B4','B3']);
This line calculates the Normalized Difference Vegetation Index (NDVI) from the Landsat image. It uses the
normalizedDifference()
method, which calculates (B4 - B3) / (B4 + B3). For Landsat 5, Band 4 is the near-infrared (NIR) band, and Band 3 is the red band. NDVI is often used in edge detection to highlight boundaries between areas with different vegetation densities. -
Detect edges:
var canny = ee.Algorithms.CannyEdgeDetector(ndvi, 0.7);
This line applies the Canny Edge Detector algorithm to the NDVI image.
-
ee.Algorithms.CannyEdgeDetector()
is the GEE function that performs the edge detection. - The first argument is the input image (in this case, the NDVI image).
-
The second argument,
0.7
, is a threshold value. This threshold determines the sensitivity of the edge detection. Lower values will detect more edges, while higher values will detect fewer, but stronger, edges.
-
-
Mask the edges:
canny = canny.updateMask(canny);
The Canny Edge Detector produces an image where pixels with edges have values close to 1, and pixels without edges have values close to 0. This line uses the edge image itself as a mask. The
updateMask()
method sets the mask of the image, so that only pixels with values greater than 0 in the canny image are kept. This effectively removes any pixels that were not identified as edges by the algorithm, resulting in a cleaner edge representation. -
Display the results:
Map.setCenter(-101.05259, 37.93418, 13);
This line sets the center and zoom level of the map.
Map.addLayer(ndvi, {min: 0, max: 1}, 'Landsat NDVI');
This line adds the NDVI image to the map, specifying a color stretch from 0 to 1 for visualization.
Map.addLayer(canny, {min: 0, max: 1, palette: 'FF0000'}, 'Canny Edges');
This line adds the Canny edge image to the map, displaying the edges in red.
Datasets Used
-
Landsat 5 TOA Reflectance: (LANDSAT/LT05/C02/T1\_TOA)
- This dataset provides top-of-atmosphere reflectance data from the Landsat 5 satellite, used here as the input for edge detection.
Applications
Edge detection has numerous applications in remote sensing and image processing, including:
- Feature Extraction: Identifying linear features like roads, rivers, and geological faults.
- Land Cover Mapping: Delineating boundaries between different land cover types, such as forests, water bodies, and agricultural fields.
- Change Detection: Detecting changes in land cover or urban areas by comparing edges in images from different dates.
- Object Recognition: Identifying the outlines of specific objects in an image.
- Image Segmentation: Dividing an image into regions based on edge information.
Visualization Example
The script displays the original Landsat NDVI image and the resulting Canny edge image. The edges are highlighted in red, showing the boundaries between areas with differing NDVI values. You should be able to see how the Canny Edge Detector effectively identifies significant linear features and boundaries in the image.
Detecting Edges with Canny Edge Detector in Google Earth Engine
Notes
-
The threshold value in the
CannyEdgeDetector
function can significantly affect the results. Experiment with different values. - The Canny Edge Detector is generally robust to noise.
- NDVI was used here, but you can apply the Canny Edge Detector to other spectral bands or indices.
COMMENTS