AI-generated Key Takeaways
-
The
selfMask()
method updates an image's mask using its own pixel values, where existing mask values of zero remain masked. -
This effectively masks out areas where the image has a value of zero, making those pixels invalid.
-
The output image retains the original metadata and footprint.
-
selfMask()
is commonly used with Boolean masks to isolate areas of interest based on specific pixel values.
Usage | Returns |
---|---|
Image.selfMask() | Image |
Argument | Type | Details |
---|---|---|
this: image | Image | The image to mask with itself. |
Examples
Code Editor (JavaScript)
// A Sentinel-2 surface reflectance image. var img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG'); var trueColorViz = { bands: ['B4', 'B3', 'B2'], min: 0, max: 2700, gamma: 1.3 }; print('Sentinel-2 image', img); Map.setCenter(-122.36, 37.47, 10); Map.addLayer(img, trueColorViz, 'Sentinel-2 image'); // Create a Boolean land mask from the SWIR1 band; water is value 0, land is 1. var landMask = img.select('B11').gt(100); print('Land mask', landMask); Map.addLayer(landMask, {palette: ['blue', 'lightgreen']}, 'Land mask'); // Mask the land mask by itself; pixel values equal to 0 (water) become invalid. var landMaskMasked = landMask.selfMask(); print('Land mask, masked', landMaskMasked); Map.addLayer(landMaskMasked, {palette: ['gold']}, 'Land mask, masked');
import ee import geemap.core as geemap
Colab (Python)
# A Sentinel-2 surface reflectance image. img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG') true_color_viz = { 'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 2700, 'gamma': 1.3, } display('Sentinel-2 image', img) m = geemap.Map() m.set_center(-122.36, 37.47, 10) m.add_layer(img, true_color_viz, 'Sentinel-2 image') # Create a Boolean land mask from the SWIR1 band water is value 0, land is 1. land_mask = img.select('B11').gt(100) display('Land mask', land_mask) m.add_layer(land_mask, {'palette': ['blue', 'lightgreen']}, 'Land mask') # Mask the land mask by itself pixel values equal to 0 (water) become invalid. land_mask_masked = land_mask.selfMask() display('Land mask, masked', land_mask_masked) m.add_layer(land_mask_masked, {'palette': ['gold']}, 'Land mask, masked') m