公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.Image.selfMask
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
使用图片的值作为新的遮罩值,更新现有遮罩不为零的所有位置的图片遮罩。输出图片会保留输入图片的元数据和覆盖区。
参数 | 类型 | 详细信息 |
---|
此:image | 图片 | 要使用自身遮罩的图片。 |
示例
代码编辑器 (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');
Python 设置
如需了解 Python API 和如何使用 geemap
进行交互式开发,请参阅
Python 环境页面。
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
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe \u003ccode\u003eselfMask()\u003c/code\u003e method updates an image's mask using its own pixel values, where existing mask values of zero remain masked.\u003c/p\u003e\n"],["\u003cp\u003eThis effectively masks out areas where the image has a value of zero, making those pixels invalid.\u003c/p\u003e\n"],["\u003cp\u003eThe output image retains the original metadata and footprint.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eselfMask()\u003c/code\u003e is commonly used with Boolean masks to isolate areas of interest based on specific pixel values.\u003c/p\u003e\n"]]],["The `selfMask()` method updates an image's mask. It uses the image's pixel values as the new mask, keeping only areas where the existing mask is non-zero. Areas where the image value is zero are removed, becoming invalid. The output image retains the input's metadata and footprint. The example demonstrates creating a land/water mask and then applying `selfMask()` to remove areas with a value of zero, representing water.\n"],null,["# ee.Image.selfMask\n\nUpdates an image's mask at all positions where the existing mask is not zero using the value of the image as the new mask value. The output image retains the metadata and footprint of the input image.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|--------------------|---------|\n| Image.selfMask`()` | Image |\n\n| Argument | Type | Details |\n|---------------|-------|--------------------------------|\n| this: `image` | Image | The image to mask with itself. |\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// Mask the land mask by itself; pixel values equal to 0 (water) become invalid.\nvar landMaskMasked = landMask.selfMask();\nprint('Land mask, masked', landMaskMasked);\nMap.addLayer(landMaskMasked, {palette: ['gold']}, 'Land mask, masked');\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# Mask the land mask by itself pixel values equal to 0 (water) become invalid.\nland_mask_masked = land_mask.selfMask()\ndisplay('Land mask, masked', land_mask_masked)\nm.add_layer(land_mask_masked, {'palette': ['gold']}, 'Land mask, masked')\nm\n```"]]