公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.Image.unmask
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在输入蒙版为零的所有位置,将输入图像的蒙版和值替换为另一图像的蒙版和值。输出图片会保留输入图片的元数据。默认情况下,输出图片也会保留输入的覆盖区,但将 sameFootprint 设置为 false 可扩展覆盖区。
用法 | 返回 |
---|
Image.unmask(value, sameFootprint) | 图片 |
参数 | 类型 | 详细信息 |
---|
此:input | 图片 | 输入图片。 |
value | 图片,默认值:null | 输入图像的蒙版像素的新值和蒙版。如果未指定,则默认为在任何地方都有效的常量零值图像。 |
sameFootprint | 布尔值,默认值:true | 如果为 true(或未指定),则输出会保留输入图片的占地面积。如果为 false,则输出的覆盖区是输入覆盖区与值图像覆盖区的并集。 |
示例
代码编辑器 (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');
// Set invalid masked pixels to a new value, e.g. a constant nodata value
// when exporting an image as GeoTIFF.
var imgUnmasked = imgMasked.unmask(32767);
print('Image, unmasked', imgMasked);
Map.addLayer(imgUnmasked, trueColorViz, 'Image, unmasked');
// Reset masked pixels to valid, fill with default value 0, input footprint.
var maskResetFootprint = imgMasked.unmask();
print('Image mask reset, footprint', maskResetFootprint);
Map.addLayer(maskResetFootprint, trueColorViz, 'Image mask reset, footprint');
// Reset masked pixels to valid, fill with default value 0, everywhere.
var maskResetEverywhere = imgMasked.unmask({sameFootprint: false});
print('Image mask reset, everywhere', maskResetEverywhere);
Map.addLayer(maskResetEverywhere, trueColorViz, 'Image mask reset, everywhere');
// Fill masked pixels with pixels from a different image.
var fill = ee.Image('COPERNICUS/S2_SR/20200618T184919_20200618T190341_T10SEG');
var imgFilled = imgMasked.unmask(fill);
print('Image, filled', imgFilled);
Map.addLayer(imgFilled, trueColorViz, 'Image, filled');
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')
# 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')
# Set invalid masked pixels to a new value, e.g. a constant nodata value
# when exporting an image as GeoTIFF.
img_unmasked = img_masked.unmask(32767)
display('Image, unmasked', img_masked)
m.add_layer(img_unmasked, true_color_viz, 'Image, unmasked')
# Reset masked pixels to valid, fill with default value 0, input footprint.
mask_reset_footprint = img_masked.unmask()
display('Image mask reset, footprint', mask_reset_footprint)
m.add_layer(mask_reset_footprint, true_color_viz, 'Image mask reset, footprint')
# Reset masked pixels to valid, fill with default value 0, everywhere.
mask_reset_everywhere = img_masked.unmask(sameFootprint=False)
display('Image mask reset, everywhere', mask_reset_everywhere)
m.add_layer(
mask_reset_everywhere, true_color_viz, 'Image mask reset, everywhere'
)
# Fill masked pixels with pixels from a different image.
fill = ee.Image('COPERNICUS/S2_SR/20200618T184919_20200618T190341_T10SEG')
img_filled = img_masked.unmask(fill)
display('Image, filled', img_filled)
m.add_layer(img_filled, true_color_viz, 'Image, filled')
m
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe \u003ccode\u003eunmask()\u003c/code\u003e method replaces masked pixel values in an image with specified values or data from another image.\u003c/p\u003e\n"],["\u003cp\u003eIt's used to fill in areas where the input image has invalid or missing data, often represented by a mask with zero values.\u003c/p\u003e\n"],["\u003cp\u003eBy default, the output image retains the original image's footprint, but you can change this behavior using the \u003ccode\u003esameFootprint\u003c/code\u003e parameter.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eunmask()\u003c/code\u003e can be useful for tasks like setting nodata values for export, merging images, or filling gaps in data.\u003c/p\u003e\n"]]],[],null,["# ee.Image.unmask\n\nReplaces mask and value of the input image with the mask and value of another image at all positions where the input mask is zero. The output image retains the metadata of the input image. By default, the output image also retains the footprint of the input, but setting sameFootprint to false allows to extend the footprint.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|-----------------------------------------------|---------|\n| Image.unmask`(`*value* `, `*sameFootprint*`)` | Image |\n\n| Argument | Type | Details |\n|-----------------|------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| this: `input` | Image | Input image. |\n| `value` | Image, default: null | New value and mask for the masked pixels of the input image. If not specified, defaults to constant zero image which is valid everywhere. |\n| `sameFootprint` | Boolean, default: true | If true (or unspecified), the output retains the footprint of the input image. If false, the footprint of the output is the union of the input footprint with the footprint of the value 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// Set invalid masked pixels to a new value, e.g. a constant nodata value\n// when exporting an image as GeoTIFF.\nvar imgUnmasked = imgMasked.unmask(32767);\nprint('Image, unmasked', imgMasked);\nMap.addLayer(imgUnmasked, trueColorViz, 'Image, unmasked');\n\n// Reset masked pixels to valid, fill with default value 0, input footprint.\nvar maskResetFootprint = imgMasked.unmask();\nprint('Image mask reset, footprint', maskResetFootprint);\nMap.addLayer(maskResetFootprint, trueColorViz, 'Image mask reset, footprint');\n\n// Reset masked pixels to valid, fill with default value 0, everywhere.\nvar maskResetEverywhere = imgMasked.unmask({sameFootprint: false});\nprint('Image mask reset, everywhere', maskResetEverywhere);\nMap.addLayer(maskResetEverywhere, trueColorViz, 'Image mask reset, everywhere');\n\n// Fill masked pixels with pixels from a different image.\nvar fill = ee.Image('COPERNICUS/S2_SR/20200618T184919_20200618T190341_T10SEG');\nvar imgFilled = imgMasked.unmask(fill);\nprint('Image, filled', imgFilled);\nMap.addLayer(imgFilled, trueColorViz, 'Image, filled');\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# Set invalid masked pixels to a new value, e.g. a constant nodata value\n# when exporting an image as GeoTIFF.\nimg_unmasked = img_masked.unmask(32767)\ndisplay('Image, unmasked', img_masked)\nm.add_layer(img_unmasked, true_color_viz, 'Image, unmasked')\n\n# Reset masked pixels to valid, fill with default value 0, input footprint.\nmask_reset_footprint = img_masked.unmask()\ndisplay('Image mask reset, footprint', mask_reset_footprint)\nm.add_layer(mask_reset_footprint, true_color_viz, 'Image mask reset, footprint')\n\n# Reset masked pixels to valid, fill with default value 0, everywhere.\nmask_reset_everywhere = img_masked.unmask(sameFootprint=False)\ndisplay('Image mask reset, everywhere', mask_reset_everywhere)\nm.add_layer(\n mask_reset_everywhere, true_color_viz, 'Image mask reset, everywhere'\n)\n\n# Fill masked pixels with pixels from a different image.\nfill = ee.Image('COPERNICUS/S2_SR/20200618T184919_20200618T190341_T10SEG')\nimg_filled = img_masked.unmask(fill)\ndisplay('Image, filled', img_filled)\nm.add_layer(img_filled, true_color_viz, 'Image, filled')\nm\n```"]]