ee.Image.updateMask
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Aktualisiert die Maske eines Bildes an allen Positionen, an denen die vorhandene Maske nicht null ist. Das Ausgabebild behält die Metadaten und den Footprint des Eingabebilds bei.
Nutzung | Ausgabe |
---|
Image.updateMask(mask) | Bild |
Argument | Typ | Details |
---|
So gehts: image | Bild | Eingabebild. |
mask | Bild | Neue Maske für das Bild als Gleitkommawert im Bereich [0, 1] (ungültig = 0, gültig = 1). Wenn dieses Bild nur ein Band hat, wird es für alle Bänder im Eingabebild verwendet. Andernfalls muss es dieselbe Anzahl von Bändern wie das Eingabebild haben. |
Beispiele
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');
// Apply the single-band land mask to all image bands; pixel values equal to 0
// in the mask become invalid in the image.
var imgMasked = img.updateMask(landMask);
print('Image, land only', imgMasked);
Map.addLayer(imgMasked, trueColorViz, 'Image, land only');
// Masks are band-specific. Here, a multi-band mask image is used to update
// corresponding input image band masks.
var imgBandSubset = img.select(['B4', 'B3', 'B2']);
var bandSpecificMasks = imgBandSubset.gt(200);
var imgBandSubsetMasked = imgBandSubset.updateMask(bandSpecificMasks);
print('Multi-band mask image', bandSpecificMasks);
print('Image, variable band masks', imgBandSubsetMasked);
Map.addLayer(bandSpecificMasks, null, 'Multi-band mask image');
Map.addLayer(imgBandSubsetMasked, trueColorViz, 'Image, variable band masks');
// Note that there is only a single alpha channel for visualization, so when
// the ee.Image is rendered as an RGB image or map tiles, a masked pixel in any
// band will result in transparency for all bands.
// Floating point mask values between 0 and 1 will be used to define opacity
// in visualization via Map.addLayer and ee.Image.visualize.
var landMaskFloat = landMask.add(0.65);
var imgMaskedFloat = img.updateMask(landMaskFloat);
print('Image, partially transparent', imgMaskedFloat);
Map.addLayer(imgMaskedFloat, trueColorViz, 'Image, partially transparent');
Python einrichten
Informationen zur Python API und zur Verwendung von geemap
für die interaktive Entwicklung finden Sie auf der Seite
Python-Umgebung.
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')
# Apply the single-band land mask to all image bands pixel values equal to 0
# in the mask become invalid in the image.
img_masked = img.updateMask(land_mask)
display('Image, land only', img_masked)
m.add_layer(img_masked, true_color_viz, 'Image, land only')
# Masks are band-specific. Here, a multi-band mask image is used to update
# corresponding input image band masks.
img_band_subset = img.select(['B4', 'B3', 'B2'])
band_specific_masks = img_band_subset.gt(200)
img_band_subset_masked = img_band_subset.updateMask(band_specific_masks)
display('Multi-band mask image', band_specific_masks)
display('Image, variable band masks', img_band_subset_masked)
m.add_layer(band_specific_masks, None, 'Multi-band mask image')
m.add_layer(
img_band_subset_masked, true_color_viz, 'Image, variable band masks'
)
# Note that there is only a single alpha channel for visualization, so when
# the ee.Image is rendered as an RGB image or map tiles, a masked pixel in any
# band will result in transparency for all bands.
# Floating point mask values between 0 and 1 will be used to define opacity
# in visualization via m.add_ee_layer and ee.Image.visualize.
land_mask_float = land_mask.add(0.65)
img_masked_float = img.updateMask(land_mask_float)
display('Image, partially transparent', img_masked_float)
m.add_layer(img_masked_float, true_color_viz, 'Image, partially transparent')
m
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
Zuletzt aktualisiert: 2025-07-26 (UTC).
[null,null,["Zuletzt aktualisiert: 2025-07-26 (UTC)."],[[["\u003cp\u003e\u003ccode\u003eupdateMask()\u003c/code\u003e modifies an image's mask, making pixels with a mask value of 0 invalid.\u003c/p\u003e\n"],["\u003cp\u003eIt accepts a single or multi-band image as a mask, applying it to corresponding bands in the input image.\u003c/p\u003e\n"],["\u003cp\u003eThe input image's metadata and footprint are preserved in the output.\u003c/p\u003e\n"],["\u003cp\u003eFloating point mask values (0-1) control pixel opacity during visualization.\u003c/p\u003e\n"]]],["The `updateMask()` function modifies an image's existing mask. It takes a new mask as input, a floating-point image where 0 denotes invalid and 1 denotes valid pixels. When applied, the function updates all image pixels where the current mask is non-zero. The new mask can be single-band, affecting all input image bands, or multi-band, updating bands individually. Mask values between 0 and 1 indicate partial transparency. The output image keeps the input image's metadata and footprint.\n"],null,["# ee.Image.updateMask\n\nUpdates an image's mask at all positions where the existing mask is not zero. The output image retains the metadata and footprint of the input image.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|--------------------------|---------|\n| Image.updateMask`(mask)` | Image |\n\n| Argument | Type | Details |\n|---------------|-------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| this: `image` | Image | Input image. |\n| `mask` | Image | New mask for the image, as a floating-point value in the range \\[0, 1\\] (invalid = 0, valid = 1). If this image has a single band, it is used for all bands in the input image; otherwise, must have the same number of bands as the input image. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// A Sentinel-2 surface reflectance image.\nvar img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG');\nvar trueColorViz = {\n bands: ['B4', 'B3', 'B2'],\n min: 0,\n max: 2700,\n gamma: 1.3\n};\nprint('Sentinel-2 image', img);\nMap.setCenter(-122.36, 37.47, 10);\nMap.addLayer(img, trueColorViz, 'Sentinel-2 image');\n\n// Create a Boolean land mask from the SWIR1 band; water is value 0, land is 1.\nvar landMask = img.select('B11').gt(100);\nprint('Land mask', landMask);\nMap.addLayer(landMask, {palette: ['blue', 'lightgreen']}, 'Land mask');\n\n// Apply the single-band land mask to all image bands; pixel values equal to 0\n// in the mask become invalid in the image.\nvar imgMasked = img.updateMask(landMask);\nprint('Image, land only', imgMasked);\nMap.addLayer(imgMasked, trueColorViz, 'Image, land only');\n\n// Masks are band-specific. Here, a multi-band mask image is used to update\n// corresponding input image band masks.\nvar imgBandSubset = img.select(['B4', 'B3', 'B2']);\nvar bandSpecificMasks = imgBandSubset.gt(200);\nvar imgBandSubsetMasked = imgBandSubset.updateMask(bandSpecificMasks);\nprint('Multi-band mask image', bandSpecificMasks);\nprint('Image, variable band masks', imgBandSubsetMasked);\nMap.addLayer(bandSpecificMasks, null, 'Multi-band mask image');\nMap.addLayer(imgBandSubsetMasked, trueColorViz, 'Image, variable band masks');\n// Note that there is only a single alpha channel for visualization, so when\n// the ee.Image is rendered as an RGB image or map tiles, a masked pixel in any\n// band will result in transparency for all bands.\n\n// Floating point mask values between 0 and 1 will be used to define opacity\n// in visualization via Map.addLayer and ee.Image.visualize.\nvar landMaskFloat = landMask.add(0.65);\nvar imgMaskedFloat = img.updateMask(landMaskFloat);\nprint('Image, partially transparent', imgMaskedFloat);\nMap.addLayer(imgMaskedFloat, trueColorViz, 'Image, partially transparent');\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# A Sentinel-2 surface reflectance image.\nimg = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')\ntrue_color_viz = {\n 'bands': ['B4', 'B3', 'B2'],\n 'min': 0,\n 'max': 2700,\n 'gamma': 1.3,\n}\ndisplay('Sentinel-2 image', img)\nm = geemap.Map()\nm.set_center(-122.36, 37.47, 10)\nm.add_layer(img, true_color_viz, 'Sentinel-2 image')\n\n# Create a Boolean land mask from the SWIR1 band water is value 0, land is 1.\nland_mask = img.select('B11').gt(100)\ndisplay('Land mask', land_mask)\nm.add_layer(land_mask, {'palette': ['blue', 'lightgreen']}, 'Land mask')\n\n# Apply the single-band land mask to all image bands pixel values equal to 0\n# in the mask become invalid in the image.\nimg_masked = img.updateMask(land_mask)\ndisplay('Image, land only', img_masked)\nm.add_layer(img_masked, true_color_viz, 'Image, land only')\n\n# Masks are band-specific. Here, a multi-band mask image is used to update\n# corresponding input image band masks.\nimg_band_subset = img.select(['B4', 'B3', 'B2'])\nband_specific_masks = img_band_subset.gt(200)\nimg_band_subset_masked = img_band_subset.updateMask(band_specific_masks)\ndisplay('Multi-band mask image', band_specific_masks)\ndisplay('Image, variable band masks', img_band_subset_masked)\nm.add_layer(band_specific_masks, None, 'Multi-band mask image')\nm.add_layer(\n img_band_subset_masked, true_color_viz, 'Image, variable band masks'\n)\n# Note that there is only a single alpha channel for visualization, so when\n# the ee.Image is rendered as an RGB image or map tiles, a masked pixel in any\n# band will result in transparency for all bands.\n\n# Floating point mask values between 0 and 1 will be used to define opacity\n# in visualization via m.add_ee_layer and ee.Image.visualize.\nland_mask_float = land_mask.add(0.65)\nimg_masked_float = img.updateMask(land_mask_float)\ndisplay('Image, partially transparent', img_masked_float)\nm.add_layer(img_masked_float, true_color_viz, 'Image, partially transparent')\nm\n```"]]