Detecting Edges with Canny Edge Detector in Google Earth Engine

SHARE:

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.

Detecting Edge in Google Earth Engine GEE


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:

  1. 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.

  2. 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.

  3. 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.
  4. 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.

  5. 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.

Edge Detection in Google Earth Engine GEE

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.
Open All Collapse All Refresh Feeds
RSS Feeds & Updates
Geospatial Tools

GIS & Remote Sensing

Workflows and tutorials on spatial data and imagery analysis.

gis.geojamal.com

Remote Sensing Indices

Explore NDVI, SAVI, NDWI and many spectral indicators.

rs.geojamal.com

GeoAI & ML

Machine learning tools and spatial intelligence applications.

geoai.geojamal.com
Mapping & Visualization

Map Resources

Shapefiles, basemaps and vector tiles for GIS projects.

maps.geojamal.com

Earth Tools & Maps

Explore satellite viewers, terrain tools and visual apps.

earth.geojamal.com

Coordinate Tools

View, transform and clean coordinates on a live map.

coordinates.geojamal.com
AI & Smart Processing

GeoAI & ML

AI tools for automated analysis of Earth data.

geoai.geojamal.com

GEE Scripts & Apps

Google Earth Engine apps and tutorials.

gee.geojamal.com
Coordinate & File Converters

Geo Format Converter

Convert CSV, KML, GPX, GeoJSON, Excel, and more.

convert.geojamal.com

Coordinate Tools

Live projection and geocoding tools.

coordinates.geojamal.com
Learning Platforms

TV: English Tutorials

Video content for GEE, GIS, remote sensing in English.

tv.geojamal.com

TV: Arabic Tutorials

Arabic video lessons on mapping and geospatial analysis.

ar.tv.geojamal.com

How-To Guides

Written tutorials and step-by-step guides.

howto.geojamal.com

GeoJamal بالعربية

الموقع الرسمي للمحتوى العربي في نظم المعلومات الجغرافية، الاستشعار عن بعد، والخرائط الذكية.

ar.geojamal.com
Downloads & Resources

Downloads Center

Free GIS tools, satellite data, software and add-ons.

downloads.geojamal.com

SASPlanet Center

Offline satellite downloading and map exploration tools.

sasplanet.geojamal.com
GPS & Field Tools

GPS Utilities

Geotagged photos, live location, export to KML or GPX.

gps.geojamal.com

Coordinate Tools

Field-ready conversion and projection mapping support.

coordinates.geojamal.com
Multilingual Access

GeoJamal بالعربية

الموقع الرسمي للمحتوى العربي في نظم المعلومات الجغرافية، الاستشعار عن بعد، الذكاء الاصطناعي الجغرافي، وتحليل الخرائط.

ar.geojamal.com
Name

CHIRPS,1,Climate,1,Cloud masking,1,Crop Impact,1,DEM,1,Disaster Monitoring,1,EVI,3,Export,2,Fire forest,1,Flood Mapping,1,Forest,1,GEE,1,GEE Academy,3,GEE script,1,GEE Tutorial,1,GeoJamal,1,Google Earth Engine,1,Image analysis,6,JRC,1,Land Cover,1,Landsat,1,LST,1,MODIS,4,NDVI,5,NOAA,1,remote sensing,3,SAR Analysis,1,Sentinel,2,Sentinel-1,1,Sentinel-2,1,Snow,1,SRTM,3,temperature,1,time series,1,vegetation index,3,VIIRS,1,
ltr
item
GEE Academy: Detecting Edges with Canny Edge Detector in Google Earth Engine
Detecting Edges with Canny Edge Detector in Google Earth Engine
Edge detection is a fundamental image processing technique used to identify boundaries between objects or regions in an image. The Canny Edge Detector
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwyJNzAMJzHtL0e387Ip8zZyrLwlqa5WPlwIzA9liGy5-zRAiNUkA0VHvgmkleLXZgnVbur3RLFjkSD4ZPASkza9iRKLn1-oSmirwfZeudoEcSGhX6ksydD-DaABhUgLcfCXCwPw0q6MHJU5sHZPa0YoGsTvCR_DLhU8HEkwTPdQvndqmxoegADPI-O8Q/w640-h348/EdgeDetector%20Earth%20Engine%20Code%20Editor.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwyJNzAMJzHtL0e387Ip8zZyrLwlqa5WPlwIzA9liGy5-zRAiNUkA0VHvgmkleLXZgnVbur3RLFjkSD4ZPASkza9iRKLn1-oSmirwfZeudoEcSGhX6ksydD-DaABhUgLcfCXCwPw0q6MHJU5sHZPa0YoGsTvCR_DLhU8HEkwTPdQvndqmxoegADPI-O8Q/s72-w640-c-h348/EdgeDetector%20Earth%20Engine%20Code%20Editor.png
GEE Academy
https://gee.geojamal.com/2025/04/detecting-edges-with-canny-edge.html
https://gee.geojamal.com/
https://gee.geojamal.com/
https://gee.geojamal.com/2025/04/detecting-edges-with-canny-edge.html
true
3421025227311197355
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content