Annuncio: tutti i progetti non commerciali registrati per l'utilizzo di Earth Engine prima del
15 aprile 2025 devono
verificare l'idoneità non commerciale per mantenere l'accesso a Earth Engine.
ee.Image.arrayMask
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Crea un'immagine array in cui ogni pixel con valore array viene mascherato con un altro pixel con valore array, conservando solo gli elementi in cui la maschera è diversa da zero. Se l'immagine della maschera ha una banda, verrà applicata a tutte le bande di "input", altrimenti devono avere lo stesso numero di bande.
Utilizzo | Resi |
---|
Image.arrayMask(mask) | Immagine |
Argomento | Tipo | Dettagli |
---|
questo: input | Immagine | Immagine dell'array da mascherare. |
mask | Immagine | Immagine dell'array da mascherare. |
Esempi
Editor di codice (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]]
Configurazione di Python
Consulta la pagina
Ambiente Python per informazioni sull'API Python e sull'utilizzo di
geemap
per lo sviluppo interattivo.
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]]
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
Ultimo aggiornamento 2025-07-26 UTC.
[null,null,["Ultimo aggiornamento 2025-07-26 UTC."],[[["\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```"]]