Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/tutorials/Image/11_texture.ipynb
2313 views
Kernel: Python 3

Texture

Earth Engine has several special methods for estimating spatial texture. When the image is discrete valued (not floating point), you can use image.entropy() to compute the entropy in a neighborhood:

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

# Installs geemap package import subprocess try: import geemap except ImportError: print("geemap package not installed. Installing ...") subprocess.check_call(["python", "-m", "pip", "install", "geemap"]) # Checks whether this notebook is running on Google Colab try: import google.colab import geemap.foliumap as emap except: import geemap as emap # Authenticates and initializes Earth Engine import ee try: ee.Initialize() except Exception as e: ee.Authenticate() ee.Initialize()

Create an interactive map

The default basemap is Google Satellite. Additional basemaps can be added using the Map.add_basemap() function.

Map = geemap.Map(center=[40, -100], zoom=4) Map.add_basemap("ROADMAP") # Add Google Map Map

Add Earth Engine Python script

import math # Load a high-resolution NAIP image. image = ee.Image("USDA/NAIP/DOQQ/m_3712213_sw_10_1_20140613") # Zoom to San Francisco, display. Map.setCenter(-122.466123, 37.769833, 17) Map.addLayer(image, {"max": 255}, "image") # Get the NIR band. nir = image.select("N") # Define a neighborhood with a kernel. square = ee.Kernel.square(**{"radius": 4}) # Compute entropy and display. entropy = nir.entropy(square) Map.addLayer(entropy, {"min": 1, "max": 5, "palette": ["0000CC", "CC0000"]}, "entropy")

Note that the NIR band is scaled to 8-bits prior to calling entropy() since the entropy computation takes discrete valued inputs. The non-zero elements in the kernel specify the neighborhood.

Another way to measure texture is with a gray-level co-occurrence matrix (GLCM). Using the image and kernel from the previous example, compute the GLCM-based contrast as follows:

# Compute the gray-level co-occurrence matrix (GLCM), get contrast. glcm = nir.glcmTexture(**{"size": 4}) contrast = glcm.select("N_contrast") Map.addLayer( contrast, {"min": 0, "max": 1500, "palette": ["0000CC", "CC0000"]}, "contrast" )

Many measures of texture are output by image.glcm(). For a complete reference on the outputs, see Haralick et al. (1973) and Conners et al. (1984).

Local measures of spatial association such as Geary’s C (Anselin 1995) can be computed in Earth Engine using image.neighborhoodToBands(). Using the image from the previous example:

# Create a list of weights for a 9x9 kernel. list = [1, 1, 1, 1, 1, 1, 1, 1, 1] # The center of the kernel is zero. centerList = [1, 1, 1, 1, 0, 1, 1, 1, 1] # Assemble a list of lists: the 9x9 kernel weights as a 2-D matrix. lists = [list, list, list, list, centerList, list, list, list, list] # Create the kernel from the weights. # Non-zero weights represent the spatial neighborhood. kernel = ee.Kernel.fixed(9, 9, lists, -4, -4, False) # Convert the neighborhood into multiple bands. neighs = nir.neighborhoodToBands(kernel) # Compute local Geary's C, a measure of spatial association. gearys = nir.subtract(neighs).pow(2).reduce(ee.Reducer.sum()).divide(math.pow(9, 2)) Map.addLayer( gearys, {"min": 20, "max": 2500, "palette": ["0000CC", "CC0000"]}, "Geary's C" ) Map.addLayerControl() Map

For an example of using neighborhood standard deviation to compute image texture, see the Statistics of Image Neighborhoods page.