代码之家  ›  专栏  ›  技术社区  ›  Paris

基于随机森林的GEE分类图像光谱特征

  •  0
  • Paris  · 技术社区  · 3 年前

    我使用这个脚本通过GEE中的RF算法,将所有类别的平均光谱特征画在一起,并分别画出分类图像的每个类别。

    var bands = ['B1', 'B2', 'B3', 'B4','B5','B6','B7', 'B8', 'B8A', 'B9' ,'B11', 'B12','NDVI', 'EVI', 'GNDVI', 'NBR', 'NDII'];
    var Training_Points = Water.merge(Residential).merge(Agricultural).merge(Arbusti).merge(BoschiMisti).merge(Latifoglie).merge(Conifere).merge(BareSoil);
    var classes = ee.Image().byte().paint(Training_Points, "land_class").rename("land_class")
    
    
    var stratified_points = classes.stratifiedSample({
          numPoints: 50,
          classBand: 'land_class',
          scale: 10,
          region: Training_Points,
          geometries: false,
          tileScale: 6
    })
    
    print(stratified_points, 'stratified_points')
    
    
    //Create training data
    var training_Stratified = RF_classified.select(bands).sampleRegions({
      collection: stratified_points,
      properties: ['land_class'],
      scale:10,
      tileScale:2
    });
    
    
    var bands = RF_classified.bandNames()
    var numBands = bands.length()
    var bandsWithClass = bands.add('land_class')
    var classIndex = bandsWithClass.indexOf('land_class')
    
    // Use .combine() to get a reducer capable of computing multiple stats on the input
    var combinedReducer = ee.Reducer.mean().combine({
      reducer2: ee.Reducer.stdDev(),
      sharedInputs: true})
    
    // Use .repeat() to get a reducer for each band and then use .group() to get stats by class
    var repeatedReducer = combinedReducer.repeat(numBands).group(classIndex)
    
    var stratified_points_Stats = training_Stratified.reduceColumns({
        selectors: bands.add('land_class'),
        reducer: repeatedReducer,
    })
    
    // Result is a dictionary, we do some post-processing to extract the results
    var groups = ee.List(stratified_points_Stats.get('groups'))
    
    var classNames = ee.List(['Water','Residential', 'Agricultural', 'Arbusti', 'BoschiMisti', 'Latifoglie','Conifere', 'BareSoil'])
    
    var fc = ee.FeatureCollection(groups.map(function(item) {
      // Extract the means
      var values = ee.Dictionary(item).get('mean')
      var groupNumber = ee.Dictionary(item).get('group')
      var properties = ee.Dictionary.fromLists(bands, values)
      var withClass = properties.set('class', classNames.get(groupNumber))
      return ee.Feature(null, withClass)
    }))
    
    // Chart spectral signatures of training data
    var options = {
      title: 'Average Spectral Signatures',
      hAxis: {title: 'Bands'},
      vAxis: {title: 'Reflectance', 
        viewWindowMode:'explicit',
        viewWindow: {
            max:6000,
            min:0
        }},
      lineWidth: 1,
      pointSize: 4,
      series: {
        0: {color: '105af0'},                                   
        1: {color: 'dc350a'}, 
        2: {color: 'caa712'}, 
        3: {color: 'b9ffa4'},
        4: {color: '369b47'},
        5: {color: '21ff2d'},
        6: {color: '275b25'},
        7: {color: 'f7e084'},   
    }};
    
    // Default band names don't sort propertly Instead, we can give a dictionary with labels for each band in the X-Axis
    var bandDescriptions = {
      'B2': 'B2/Blue',
      'B3': 'B3/Green',
      'B4': 'B4/Red',
      'B5': 'B5/Red Edge 1',
      'B6': 'B5/Red Edge 2',
      'B7': 'B7/Red Edge 3',
      'B8': 'B8/NIR',
      'B8A': 'B8A/Red Edge 4',
      'B11': 'B11/SWIR-1',
      'B12': 'B12/SWIR-2'
    }
    
    // Create the chart and set options.
    var chart = ui.Chart.feature.byProperty({
      features: fc,
      xProperties: bandDescriptions,
      seriesProperty: 'class'
    })
    .setChartType('ScatterChart')
    .setOptions(options);
    
    print(chart)
    
    var classChart = function(land_class, label, color) {
      var options = {
      title: 'Spectral Signatures for ' + label + ' Class',
      hAxis: {title: 'Bands'},
      vAxis: {title: 'Reflectance', 
        viewWindowMode:'explicit',
        viewWindow: {
            max:6000,
            min:0
        }},
      lineWidth: 1,
      pointSize: 4,  
      };
    
      var fc = training_Stratified.filter(ee.Filter.eq('land_class', land_class))
      var chart = ui.Chart.feature.byProperty({
      features: fc,
      xProperties: bandDescriptions,
      })
    .setChartType('ScatterChart')
    .setOptions(options);
    
    print(chart)
    }
    classChart(0, 'Water')
    classChart(1, 'Residential')
    classChart(2, 'Agricultural')
    classChart(3, 'Arbusti')
    classChart(4, 'BoschiMisti')
    classChart(5, 'Latifoglie')
    classChart(6, 'Conifere')
    classChart(7, 'BareSoil')
    

    我收到错误信息:

    生成图表时出错:图像。选择:图案“B1”与任何 乐队。

    我不明白问题出在哪里,因为我之前使用了相同的脚本来绘制训练数据的直方图,而且效果很好。

    0 回复  |  直到 3 年前
    推荐文章