Path: blob/master/tutorials/Image/09_edge_detection.ipynb
2313 views
Edge detection
Edge detection is applicable to a wide range of image processing tasks. In addition to the edge detection kernels described in the convolutions section, there are several specialized edge detection algorithms in Earth Engine. The Canny edge detection algorithm (Canny 1986) uses four separate filters to identify the diagonal, vertical, and horizontal edges. The calculation extracts the first derivative value for the horizontal and vertical directions and computes the gradient magnitude. Gradients of smaller magnitude are suppressed. To eliminate high-frequency noise, optionally pre-filter the image with a Gaussian kernel. For example:
Install Earth Engine API and geemap
Install the Earth Engine Python API and geemap. The geemap Python package is built upon the ipyleaflet and folium packages and implements several methods for interacting with Earth Engine data layers, such as Map.addLayer(), Map.setCenter(), and Map.centerObject(). The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its dependencies, including earthengine-api, folium, and ipyleaflet.
Important note: A key difference between folium and ipyleaflet is that ipyleaflet is built upon ipywidgets and allows bidirectional communication between the front-end and the backend enabling the use of the map to capture user input, while folium is meant for displaying static data only (source). Note that Google Colab currently does not support ipyleaflet (source). Therefore, if you are using geemap with Google Colab, you should use import geemap.foliumap. If you are using geemap with binder or a local Jupyter notebook server, you can use import geemap, which provides more functionalities for capturing user input (e.g., mouse-clicking and moving).
Create an interactive map
The default basemap is Google Satellite. Additional basemaps can be added using the Map.add_basemap() function.
Add Earth Engine Python script
Note that the threshold parameter determines the minimum gradient magnitude and the sigma parameter is the standard deviation (SD) of a Gaussian pre-filter to remove high-frequency noise. For line extraction from an edge detector, Earth Engine implements the Hough transform (Duda and Hart 1972). Continuing the previous example, extract lines from the Canny detector with:
Another specialized algorithm in Earth Engine is zeroCrossing(). A zero-crossing is defined as any pixel where the right, bottom, or diagonal bottom-right pixel has the opposite sign. If any of these pixels is of opposite sign, the current pixel is set to 1 (zero-crossing); otherwise it's set to zero. To detect edges, the zero-crossings algorithm can be applied to an estimate of the image second derivative. The following demonstrates using zeroCrossing() for edge detection:
The zero-crossings output for an area near the San Francisco, CA airport should look something like Figure 1.
View source on GitHub
Run in Google Colab