您可以使用 add()
和 subtract()
等運算子執行圖像運算,但如果要執行包含多個項的複雜運算,expression()
函式會是較佳的替代方案。如要進一步瞭解運算子和運算式,請參閱下列章節。
運算子
數學運算子可在圖像頻帶上執行基本算術運算。這類函式會接受兩個輸入內容:兩張圖片或一張圖片和常數,這會解讀為沒有遮罩像素的單一頻帶常數圖片。每個頻帶的每個像素都會執行作業。
舉例來說,假設您要使用 VIIRS 影像計算常態化差異植被指數 (NDVI),這裡使用 add()
、subtract()
和 divide()
運算子:
程式碼編輯器 (JavaScript)
// Load a VIIRS 8-day surface reflectance composite for May 2024. var viirs202405 = ee.ImageCollection('NASA/VIIRS/002/VNP09H1').filter( ee.Filter.date('2024-05-01', '2024-05-16')).first(); // Compute NDVI. var ndvi202405 = viirs202405.select('SurfReflect_I2') .subtract(viirs202405.select('SurfReflect_I1')) .divide(viirs202405.select('SurfReflect_I2') .add(viirs202405.select('SurfReflect_I1')));
import ee import geemap.core as geemap
Colab (Python)
# Load a VIIRS 8-day surface reflectance composite for May 2024. viirs202405 = ( ee.ImageCollection('NASA/VIIRS/002/VNP09H1') .filter(ee.Filter.date('2024-05-01', '2024-05-16')) .first() ) # Compute NDVI. ndvi202405 = ( viirs202405.select('SurfReflect_I2') .subtract(viirs202405.select('SurfReflect_I1')) .divide( viirs202405.select('SurfReflect_I2').add( viirs202405.select('SurfReflect_I1') ) ) )
系統只會考量兩個輸入內容之間未遮罩的像素交集,並以未遮罩的形式傳回,所有其他像素都會遭到遮罩。一般來說,如果任一輸入內容只有一個頻帶,則會用於比較其他輸入內容的所有頻帶。如果輸入內容的頻帶數量相同,但名稱不同,則會以自然順序成對使用。輸出頻帶的名稱會以兩個輸入內容中較長的輸入內容命名,如果兩者長度相同,則會以第一個輸入內容的順序命名。輸出像素的類型是輸入類型的聯集。
以下是多頻帶圖像減法範例,說明如何自動比對頻帶,進而為每個同時出現的頻帶產生「變更向量」。
程式碼編輯器 (JavaScript)
// Load a VIIRS 8-day surface reflectance composite for September 2024. var viirs202409 = ee.ImageCollection('NASA/VIIRS/002/VNP09H1').filter( ee.Filter.date('2024-09-01', '2024-09-16')).first(); // Compute multi-band difference between the September composite and the // previously loaded May composite. var diff = viirs202409.subtract(ndvi202405); Map.addLayer(diff, { bands: ['SurfReflect_I1', 'SurfReflect_I2', 'SurfReflect_I3'], min: -1, max: 1 }, 'difference'); // Compute the squared difference in each band. var squaredDifference = diff.pow(2); Map.addLayer(squaredDifference, { bands: ['SurfReflect_I1', 'SurfReflect_I2', 'SurfReflect_I3'], min: 0, max: 0.7 }, 'squared diff.');
import ee import geemap.core as geemap
Colab (Python)
# Load a VIIRS 8-day surface reflectance composite for September 2024. viirs202409 = ( ee.ImageCollection('NASA/VIIRS/002/VNP09H1') .filter(ee.Filter.date('2024-09-01', '2024-09-16')) .first() ) # Compute multi-band difference between the September composite and the # previously loaded May composite. diff = viirs202409.subtract(ndvi202405) m = geemap.Map() m.add_layer( diff, { 'bands': ['SurfReflect_I1', 'SurfReflect_I2', 'SurfReflect_I3'], 'min': -1, 'max': 1, }, 'difference', ) # Compute the squared difference in each band. squared_difference = diff.pow(2) m.add_layer( squared_difference, { 'bands': ['SurfReflect_I1', 'SurfReflect_I2', 'SurfReflect_I3'], 'min': 0, 'max': 0.7, }, 'squared diff.', ) display(m)
在本範例的第二部分中,我們使用 image.pow(2)
計算平方差。如需完整的數學運算子清單,瞭解如何處理基本算術、三角學、指數、捨入、轉型、位元運算等,請參閱 API 說明文件。
運算式
如要導入更複雜的數學運算式,建議您使用 image.expression()
,這個函式會剖析數學運算的文字表示法。以下範例使用 expression()
計算增強植被指數 (EVI):
程式碼編輯器 (JavaScript)
// Load a Landsat 8 image. var image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318'); // Compute the EVI using an expression. var evi = image.expression( '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', { 'NIR': image.select('B5'), 'RED': image.select('B4'), 'BLUE': image.select('B2') }); Map.centerObject(image, 9); Map.addLayer(evi, {min: -1, max: 1, palette: ['a6611a', 'f5f5f5', '4dac26']});
import ee import geemap.core as geemap
Colab (Python)
# Load a Landsat 8 image. image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318') # Compute the EVI using an expression. evi = image.expression( '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', { 'NIR': image.select('B5'), 'RED': image.select('B4'), 'BLUE': image.select('B2'), }, ) # Define a map centered on San Francisco Bay. map_evi = geemap.Map(center=[37.4675, -122.1363], zoom=9) # Add the image layer to the map and display it. map_evi.add_layer( evi, {'min': -1, 'max': 1, 'palette': ['a6611a', 'f5f5f5', '4dac26']}, 'evi' ) display(map_evi)
請注意,expression()
的第一個引數是數學運算的文字表示法,第二個引數是字典,其中索引鍵是運算式中使用的變數名稱,值則是應將變數對應至的圖像頻帶。圖片中的頻帶可稱為 b("band name")
或 b(index)
,例如 b(0)
,而非提供字典。使用頻帶對應字典時,可以從輸入圖像以外的圖像定義頻帶。請注意,expression()
會使用「整除法」,在兩個整數相除時,會捨去餘數並傳回整數。例如 10 / 20 = 0
。如要變更這項行為,請將其中一個運算元項乘以 1.0
:10 * 1.0 / 20 = 0.5
。評估多個來源圖片的頻帶時,系統只會考量未遮罩像素的交集,並將其視為未遮罩的像素傳回。下表列出支援的運算式運算子。
類型 | 符號 | 名稱 |
---|---|---|
算術運算 | + - * / % ** | 加、減、乘、除、取餘數、指數 |
關聯 | == != < > <= >= | 等於、不等於、小於、大於等。 |
Logical | && || ! ^。 | And、Or、Not、Xor |
三元組 | ? : | If then else |