公告:凡是在
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 環境頁面,瞭解 Python API 和如何使用 geemap
進行互動式開發。
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
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間: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```"]]