公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.Image.arrayMask
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
创建一个数组图像,其中每个数组值像素都使用另一个数组值像素进行遮盖,仅保留遮盖值为非零的元素。如果遮罩图片只有一个波段,则会将其应用于“input”的所有波段;否则,它们必须具有相同数量的波段。
用法 | 返回 |
---|
Image.arrayMask(mask) | 图片 |
参数 | 类型 | 详细信息 |
---|
此:input | 图片 | 要遮盖的数组映像。 |
mask | 图片 | 要遮盖的数组图片。 |
示例
代码编辑器 (JavaScript)
// A function to print arrays for a selected pixel in the following examples.
function sampArrImg(arrImg) {
var point = ee.Geometry.Point([-121, 42]);
return arrImg.sample(point, 500).first().get('array');
}
// Create a 1D array image with length 6.
var arrayImg1D = ee.Image([0, 1, 2, 4, 0, 5]).toArray();
print('1D array image (pixel)', sampArrImg(arrayImg1D));
// [0, 1, 2, 4, 0, 5]
// Create a mask using a relational operator to mask values greater than 2.
var mask1D = arrayImg1D.lte(2);
print('1D mask for greater than value 2 (pixel)', sampArrImg(mask1D));
// [1, 1, 1, 0, 1, 0]
var arrayImg1DMask = arrayImg1D.arrayMask(mask1D);
print('1D array image mask (pixel)', sampArrImg(arrayImg1DMask));
// [0, 1, 2, 0]
// Self mask the 1D array image. Value zero will be masked out.
var arrayImg1DselfMask = arrayImg1D.arrayMask(arrayImg1D);
print('1D array image self mask (pixel)', sampArrImg(arrayImg1DselfMask));
// [1, 2, 4, 5]
// Create a 2D array image.
var arrayImg2D = arrayImg1D.arrayReshape(ee.Image([2, 3]).toArray(), 2);
print('2D 2x3 array image (pixel)', sampArrImg(arrayImg2D));
// [[0, 1, 2],
// [4, 0, 5]]
// Slice out a row to use as a column mask.
var rowAsMaskForCols = arrayImg2D.arraySlice(0, 1, 2);
print('2D mask for cols (pixel)', sampArrImg(rowAsMaskForCols));
// [[4, 0, 5]]
var arrayImg2DMaskCols = arrayImg2D.arrayMask(rowAsMaskForCols);
print('2D array image cols masked (pixel)', sampArrImg(arrayImg2DMaskCols));
// [[0, 2],
// [4, 5]]
// Slice out a column to use as a row mask.
var colAsMaskForRows = arrayImg2D.arraySlice(1, 1, 2);
print('2D mask for rows (pixel)', sampArrImg(colAsMaskForRows));
// [[1],
// [0]]
var arrayImg2DMaskRows = arrayImg2D.arrayMask(colAsMaskForRows);
print('2D array image rows masked (pixel)', sampArrImg(arrayImg2DMaskRows));
// [[0, 1, 2]]
Python 设置
如需了解 Python API 和如何使用 geemap
进行交互式开发,请参阅
Python 环境页面。
import ee
import geemap.core as geemap
Colab (Python)
# A function to print arrays for a selected pixel in the following examples.
def samp_arr_img(arr_img):
point = ee.Geometry.Point([-121, 42])
return arr_img.sample(point, 500).first().get('array')
# Create a 1D array image with length 6.
array_img_1d = ee.Image([0, 1, 2, 4, 0, 5]).toArray()
print('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())
# [0, 1, 2, 4, 0, 5]
# Create a mask using a relational operator to mask values greater than 2.
mask_1d = array_img_1d.lte(2)
print(
'1D mask for greater than value 2 (pixel):',
samp_arr_img(mask_1d).getInfo()
)
# [1, 1, 1, 0, 1, 0]
array_img1d_mask = array_img_1d.arrayMask(mask_1d)
print('1D array image mask (pixel):', samp_arr_img(array_img1d_mask).getInfo())
# [0, 1, 2, 0]
# Self mask the 1D array image. Value zero will be masked out.
array_img_1d_self_mask = array_img_1d.arrayMask(array_img_1d)
print(
'1D array image self mask (pixel):',
samp_arr_img(array_img_1d_self_mask).getInfo()
)
# [1, 2, 4, 5]
# Create a 2D array image.
array_img_2d = array_img_1d.arrayReshape(ee.Image([2, 3]).toArray(), 2)
print('2D 2x3 array image (pixel):', samp_arr_img(array_img_2d).getInfo())
# [[0, 1, 2],
# [4, 0, 5]]
# Slice out a row to use as a column mask.
row_as_mask_for_cols = array_img_2d.arraySlice(0, 1, 2)
print('2D mask for cols (pixel):', samp_arr_img(row_as_mask_for_cols).getInfo())
# [[4, 0, 5]]
array_img_2d_mask_cols = array_img_2d.arrayMask(row_as_mask_for_cols);
print(
'2D array image cols masked (pixel):',
samp_arr_img(array_img_2d_mask_cols).getInfo()
)
# [[0, 2],
# [4, 5]]
# Slice out a column to use as a row mask.
col_as_mask_for_rows = array_img_2d.arraySlice(1, 1, 2)
print('2D mask for rows (pixel):', samp_arr_img(col_as_mask_for_rows).getInfo())
# [[1],
# [0]]
array_img_2d_mask_rows = array_img_2d.arrayMask(col_as_mask_for_rows)
print(
'2D array image rows masked (pixel):',
samp_arr_img(array_img_2d_mask_rows).getInfo()
)
# [[0, 1, 2]]
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003e\u003ccode\u003earrayMask\u003c/code\u003e creates a masked array image by retaining elements where the mask image is non-zero.\u003c/p\u003e\n"],["\u003cp\u003eIt can be applied to 1D or 2D array images, using either single-band or multi-band masks.\u003c/p\u003e\n"],["\u003cp\u003eIf the mask has one band, it's applied to all bands of the input array image; otherwise, they must have the same number of bands.\u003c/p\u003e\n"],["\u003cp\u003eThe masking operation effectively filters out array elements corresponding to zero values in the mask.\u003c/p\u003e\n"],["\u003cp\u003eThis function is useful for selectively manipulating or analyzing specific elements within array images.\u003c/p\u003e\n"]]],[],null,["# ee.Image.arrayMask\n\nCreates an array image where each array-valued pixel is masked with another array-valued pixel, retaining only the elements where the mask is non-zero. If the mask image has one band it will be applied to all the bands of 'input', otherwise they must have the same number of bands.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|-------------------------|---------|\n| Image.arrayMask`(mask)` | Image |\n\n| Argument | Type | Details |\n|---------------|-------|---------------------------|\n| this: `input` | Image | Array image to mask. |\n| `mask` | Image | Array image to mask with. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// A function to print arrays for a selected pixel in the following examples.\nfunction sampArrImg(arrImg) {\n var point = ee.Geometry.Point([-121, 42]);\n return arrImg.sample(point, 500).first().get('array');\n}\n\n// Create a 1D array image with length 6.\nvar arrayImg1D = ee.Image([0, 1, 2, 4, 0, 5]).toArray();\nprint('1D array image (pixel)', sampArrImg(arrayImg1D));\n// [0, 1, 2, 4, 0, 5]\n\n// Create a mask using a relational operator to mask values greater than 2.\nvar mask1D = arrayImg1D.lte(2);\nprint('1D mask for greater than value 2 (pixel)', sampArrImg(mask1D));\n// [1, 1, 1, 0, 1, 0]\n\nvar arrayImg1DMask = arrayImg1D.arrayMask(mask1D);\nprint('1D array image mask (pixel)', sampArrImg(arrayImg1DMask));\n// [0, 1, 2, 0]\n\n// Self mask the 1D array image. Value zero will be masked out.\nvar arrayImg1DselfMask = arrayImg1D.arrayMask(arrayImg1D);\nprint('1D array image self mask (pixel)', sampArrImg(arrayImg1DselfMask));\n// [1, 2, 4, 5]\n\n// Create a 2D array image.\nvar arrayImg2D = arrayImg1D.arrayReshape(ee.Image([2, 3]).toArray(), 2);\nprint('2D 2x3 array image (pixel)', sampArrImg(arrayImg2D));\n// [[0, 1, 2],\n// [4, 0, 5]]\n\n// Slice out a row to use as a column mask.\nvar rowAsMaskForCols = arrayImg2D.arraySlice(0, 1, 2);\nprint('2D mask for cols (pixel)', sampArrImg(rowAsMaskForCols));\n// [[4, 0, 5]]\n\nvar arrayImg2DMaskCols = arrayImg2D.arrayMask(rowAsMaskForCols);\nprint('2D array image cols masked (pixel)', sampArrImg(arrayImg2DMaskCols));\n// [[0, 2],\n// [4, 5]]\n\n// Slice out a column to use as a row mask.\nvar colAsMaskForRows = arrayImg2D.arraySlice(1, 1, 2);\nprint('2D mask for rows (pixel)', sampArrImg(colAsMaskForRows));\n// [[1],\n// [0]]\n\nvar arrayImg2DMaskRows = arrayImg2D.arrayMask(colAsMaskForRows);\nprint('2D array image rows masked (pixel)', sampArrImg(arrayImg2DMaskRows));\n// [[0, 1, 2]]\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 function to print arrays for a selected pixel in the following examples.\ndef samp_arr_img(arr_img):\n point = ee.Geometry.Point([-121, 42])\n return arr_img.sample(point, 500).first().get('array')\n\n# Create a 1D array image with length 6.\narray_img_1d = ee.Image([0, 1, 2, 4, 0, 5]).toArray()\nprint('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())\n# [0, 1, 2, 4, 0, 5]\n\n# Create a mask using a relational operator to mask values greater than 2.\nmask_1d = array_img_1d.lte(2)\nprint(\n '1D mask for greater than value 2 (pixel):',\n samp_arr_img(mask_1d).getInfo()\n)\n# [1, 1, 1, 0, 1, 0]\n\narray_img1d_mask = array_img_1d.arrayMask(mask_1d)\nprint('1D array image mask (pixel):', samp_arr_img(array_img1d_mask).getInfo())\n# [0, 1, 2, 0]\n\n# Self mask the 1D array image. Value zero will be masked out.\narray_img_1d_self_mask = array_img_1d.arrayMask(array_img_1d)\nprint(\n '1D array image self mask (pixel):',\n samp_arr_img(array_img_1d_self_mask).getInfo()\n)\n# [1, 2, 4, 5]\n\n# Create a 2D array image.\narray_img_2d = array_img_1d.arrayReshape(ee.Image([2, 3]).toArray(), 2)\nprint('2D 2x3 array image (pixel):', samp_arr_img(array_img_2d).getInfo())\n# [[0, 1, 2],\n# [4, 0, 5]]\n\n# Slice out a row to use as a column mask.\nrow_as_mask_for_cols = array_img_2d.arraySlice(0, 1, 2)\nprint('2D mask for cols (pixel):', samp_arr_img(row_as_mask_for_cols).getInfo())\n# [[4, 0, 5]]\n\narray_img_2d_mask_cols = array_img_2d.arrayMask(row_as_mask_for_cols);\nprint(\n '2D array image cols masked (pixel):',\n samp_arr_img(array_img_2d_mask_cols).getInfo()\n)\n# [[0, 2],\n# [4, 5]]\n\n# Slice out a column to use as a row mask.\ncol_as_mask_for_rows = array_img_2d.arraySlice(1, 1, 2)\nprint('2D mask for rows (pixel):', samp_arr_img(col_as_mask_for_rows).getInfo())\n# [[1],\n# [0]]\n\narray_img_2d_mask_rows = array_img_2d.arrayMask(col_as_mask_for_rows)\nprint(\n '2D array image rows masked (pixel):',\n samp_arr_img(array_img_2d_mask_rows).getInfo()\n)\n# [[0, 1, 2]]\n```"]]