GEE: Use of common masking functions in remote sensing image processing [updateMask]

For learning to be just and constant, for learning to be not just Sui Dynasty, and for learning to be constant, to retreat. Song. Feng Zixian


There are three masking-related functions in GEE, mask, updateMask and unmask. It is necessary to distinguish and master the usage of three masking functions.

1. mask() function

  • ee.Image.mask()

Gets or sets the mask of the image. The output image preserves the metadata and footprint of the input image. Pixels whose mask changes from zero to another value will be filled with zero or the nearest zero value within the range of pixel types.

Mask image. If specified, the input image is copied to the output, but the mask is given by the value of the image. If this is a single band, it is used for all the bands in the input image. If not specified, the image created from the mask of the input image is returned, zoomed to the extent 0:1.

Note: The version that sets the mask will be discarded. To set the image mask on previously unmasked pixels, use Image.updateMask. To unmask previously masked pixels, use Image.unmask.

Usage: image.mask(), which distinguishes the masked and non-masked areas of the image with 0 and 1, where 1 represents the mask and 0 represents the unmasked area. Within the bounding area of the image, black represents the masked area and white represents the unmasked area.

2. updateMask()


Update the image mask in all locations where the existing mask is not zero. The output image preserves the metadata and footprint of the input image.

New mask for image, as 0,1 Floating point values in range. If the image has one band, it is used for all the bands in the input image. Otherwise, you must have the same number of bands as the input image.

Usage: image.updateMask(), updateMask will mask the area of 0 on the image. If you want one image to have the same mask as another, you can use image2. UpdateMask (image1.mask()). Not ()).

3. unmask()


Replace the mask and value of the input image with the mask and value of another image at all locations where the input mask is zero. The output image retains the metadata of the input image. By default, the output image retains the input footprint, but setting sameFootprint to false allows the footprint to be extended.

Use method, image. The function of unmask (number) is that it can replace the masked area image value with any value, while the unmasked area remains the original value. For example, image.unmask(-9999) replaces the value in the masked area with -9999, which makes the image seamlessly compatible with arcgis.

4. Case Practice

The python code is as follows:

(1) Image data

image = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')
trueColor = {
    'bands':['B4', 'B3', 'B2'], 
    'min':0, 'max':2800,

# Loading Images
Map = geemap.Map()
Map.addLayer(image, trueColor, 's2 image')

(2) corresponding mask processing

# ************Replace all mask areas with a value, using unmask**********
# # ************Mask off unqualified pixels and use updateMask********
# The Boolean band of land is created using SWIR1, where water is 0 and land is 1
landMask ='B11').gt(100)

# Applying a mask band to update the original image will invalidate any cells on the image that have a mask equal to 0
imageMasked = image.updateMask(landMask) # Only land is retained

# Loading two images, the landMask image clearly shows two colors, 0 in blue and 1 in green
Map.addLayer(landMask, {'palette': ['blue', 'lightgreen']}, 'Land mask')
Map.addLayer(imageMasked, trueColor, 'imageMasked')

# ************Replace all mask areas with a value, using unmask**********
# Set invalid mask value, replace non-mask area with 32767 null value
imageUnmasked = imageMasked.unmask(32767)
Map.addLayer(imageUnmasked, trueColor, 'image unmasked')

# Reset mask pixels to valid, fill with default value of 0, enter footprint
maskResetFootprint = imageMasked.unmask()
Map.addLayer(maskResetFootprint, trueColor, 'maskResetFootprint')

# Replace with another image cell value
fill = ee.Image('COPERNICUS/S2_SR/20200618T184919_20200618T190341_T10SEG')
imgFill = imageMasked.unmask(fill)
Map.addLayer(fill, trueColor, 'new s2')
Map.addLayer(imgFill, trueColor, 'image filled')

The results are illustrated as follows:


V. Summary

  1. There are three common masking functions in GEE, mask, updateMask, and unmask. Mask is not recommended as a function to be deprecated. Data processing and analysis can be achieved by using updateMask and Mask flexibly.
  2. It is interesting to use masking analysis to achieve the magical function of moving flowers and joints.

Reference resources:


Keywords: Python Big Data GEE

Added by FortMyersDrew on Fri, 11 Feb 2022 23:15:19 +0200