簡體   English   中英

使用 Python API 對 GEE 圖像集合進行閾值處理

[英]Thresholding a GEE Image Collection with Python API

I am looking for a way to thresholding image collection by a given with Python API, in this script i want to calculate images per year and make classification With the python API I get the error: 'ImageCollection' object has no attribute 'gte'

collection = ee.ImageCollection('MODIS/006/MCD43A4')     .filterBounds(study_area)
    
yearlist = range(start_year, end_year) \
#ee.List([2013,2014,2015, 2016, 2017, 2018,2019,2020])

#CLOUD MASKING
def mask_clouds(image):
  # Select the QA band.
  QA = image.select('BRDF_Albedo_Band_Mandatory_Quality_Band1')
  # Make a mask to get bit 10, the internal_cloud_algorithm_flag bit.
  bitMask = 1 << 10
  # Return an image masking out cloudy areas.
  return image.multiply(0.0001).updateMask(QA.bitwiseAnd(bitMask).eq(0))

#PERHITUNGAN MNDVI

def calculate_mndvi (year):
    image = collection      .filter(ee.Filter.calendarRange(year, year, 'year'))       .map(mask_clouds)     .median()
    NDVI = image    .normalizedDifference(['Nadir_Reflectance_Band2', 'Nadir_Reflectance_Band1'])      .rename('NDVI')

    H1 = image.expression(
              '((C11*RED)-BLUE+C12)/((NIR*NIR)-(RED*RED))', {
              'C11': 0.55,
              'C12': 0.12,
              'RED': image.select('Nadir_Reflectance_Band1'),
              'BLUE': image.select('Nadir_Reflectance_Band3'),
              'NIR': image.select('Nadir_Reflectance_Band2')
              }).rename('H1')

    H2 = image.expression(
              '1/((C11*RED)-BLUE+C12)', {
              'C11': 0.55,
              'C12': 0.12,
              'RED': image.select('Nadir_Reflectance_Band1'),
              'BLUE': image.select('Nadir_Reflectance_Band3')
              }).rename('H2')

    mndvi = image.expression(
              '((1+(C2*H2))*NDVI)/(1+(C1*H1))', {
              'C1': 0.6,
              'C2': 0.03,
              'H1': H1,
              'H2': H2,
              'NDVI' : NDVI
              }).rename('MNDVI')

    return mndvi    .set('year', year)     .set('month', 1)     .set('date', ee.Date.fromYMD(year,1,1))     .set('system:time_start',ee.Date.fromYMD(year,1,1))

mndvi_images = ee.ImageCollection.fromImages([
    calculate_mndvi(year) for year in yearlist
])

print(mndvi_images.getInfo())

threshold1 = mndvi_images.select('MNDVI').gte(-1).And(mndvi_images.select('MNDVI').lte(0.2)).selfMask()
threshold2 = mndvi_images.select('MNDVI').gte(0.2).And(mndvi_images.select('MNDVI').lte(0.4)).selfMask().multiply(2)
threshold3 = mndvi_images.select('MNDVI').gte(0.4).And(mndvi_images.select('MNDVI').lte(0.6)).selfMask().multiply(3)
threshold4 = mndvi_images.select('MNDVI').gte(0.6).And(mndvi_images.select('MNDVI').lte(1)).selfMask().multiply(4)

threshold1 = threshold1.toShort().select(0).rename('MNDVI')
threshold2 = threshold2.toShort().select(0).rename('MNDVI')
threshold3 = threshold3.toShort().select(0).rename('MNDVI')
threshold4 = threshold4.toShort().select(0).rename('MNDVI')

# STACKING LAYER

stacking_layer = ee.ImageCollection.fromImages([threshold1, threshold2, threshold3, threshold4])
print(stacking_layer,'staking'+i)

# Mosaic the ImageCollection.
stacking = stacking_layer.mosaic()
Map.addLayer(stacking, {'palette': ['e81410',  'f0fc0a',  '30bf21', '198f0d'], 'min':1, 'max':4},'Vegetation'+i)

每一個小小的幫助,我真的很感激。 謝謝

.gte.lte等對圖像進行操作,而不是圖像 collections。 您需要創建一個 function 來對圖像進行操作,並使用.map將其應用於集合中的每個圖像。 像這樣:

def threshold1_image(image):
    return image.gte(-1).And(image.lte(0.2)).selfMask()


threshold1 = mndvi_images.select("MNDVI").map(threshold1_image)

或者您可以使用 lambda function:

threshold1 = mndvi_images.select("MNDVI").map(lambda x: x.gte(-1).And(x.lte(0.2)).selfMask())         

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM