image.sampleRegions 不是函数(在 google earth engine 进行监督分类)

image.sampleRegions is not a function (supervised classification at google earth engine)

您好,我是一名初学者,目前在 Google Earth Engine 中从事监督分类。我似乎无法解决问题 'image.sampleRegions is not a function'。这是我使用的脚本。

/**
 * Function to mask clouds using the Sentinel-2 QA band
 * @param {ee.Image} image Sentinel-2 image
 * @return {ee.Image} cloud masked Sentinel-2 image
 */
function maskS2clouds(image) {
  var qa = image.select('QA60');

  // Bits 10 and 11 are clouds and cirrus, respectively.
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;

  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      .and(qa.bitwiseAnd(cirrusBitMask).eq(0));

  return image.updateMask(mask).divide(10000);
}

var dataset = ee.ImageCollection('COPERNICUS/S2_SR')
                  .filterDate('2019-09-01', '2019-10-01') //september
                  // Pre-filter to get less cloudy granules.
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
                  .map(maskS2clouds);

var visualization = {
  min: 0.0,
  max: 0.3,
  bands: ['B4', 'B3', 'B2'],
};

Map.setCenter(101.68287285738528,0.6988384299139916, 16);
Map.addLayer(dataset.mean(), visualization, 'RGB');

// Use these bands for prediction.
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11'];


// Make a FeatureCollection from the hand-made geometries.
var polygons = ee.FeatureCollection([
  ee.Feature(Kebun1, {'class': 0}),
  ee.Feature(Kebun2, {'class': 0}),
  ee.Feature(Kebun3, {'class': 0}),
  ee.Feature(Canal1, {'class': 1}),
  ee.Feature(Canal2, {'class': 1}),
]);

//Define the image aduh anjir salah dimana sih
var imageCollection = ee.ImageCollection("COPERNICUS/S2");
var geometry = ee.FeatureCollection(polygons);
var image = imageCollection
.filterDate('2019-09-01', '2019-10-1')
                  // Pre-filter to get less cloudy granules.
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                  .map(maskS2clouds)
                  //filter according to drawn boundary
.filterBounds(geometry);

             

// Get the values for all pixels in each polygon in the training.
var training = image.sampleRegions({
  // Get the sample from the polygons FeatureCollection.
  collection: polygons,
  // Keep this list of properties from the polygons.
  properties: ['class'],
  // Set the scale to get Landsat pixels in the polygons.
  scale: 30
});

// Create an SVM classifier with custom parameters.
var classifier = ee.Classifier.libsvm({
  kernelType: 'RBF',
  gamma: 0.5,
  cost: 10
});

// Train the classifier.
var trained = classifier.train(training, 'class', bands);

// Classify the image.
var classified = image.classify(trained);

// Display the classification result and the input image.
Map.setCenter(101.68287285738528,0.6988384299139916,16);
Map.addLayer(image, {bands: ['B4', 'B3', 'B2'], max: 0.5, gamma: 2});
Map.addLayer(polygons, {}, 'training polygons');
Map.addLayer(classified,
             {min: 0, max: 1, palette: ['red', 'green']},
             'klasifikasi');

很难从您的代码中猜出,但通过一些调试您可能会弄明白。尝试 console.log image.sampleRegions 到底是什么以及 image object 的属性是什么。从那里您可能能够看到问题所在 - 未定义 属性、错误类型等

AD:在快速查看 GEE 文档后,我想尝试将您的 collection 处理包装到 ee.Image() 中。也许这会有所帮助:

var image = ee.Image(imageCollection
  .filterDate('2019-09-01', '2019-10-1')
  // Pre-filter to get less cloudy granules.
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
  .map(maskS2clouds)
  //filter according to drawn boundary
  .filterBounds(geometry));

问题出在这里:

var image = imageCollection
    .filterDate(...)
    .filter(...)
    .map(...)
    .filterBounds(...);

var training = image.sampleRegions(...);

您对 imageCollection 所做的一切仍然 returns 图像集,而不是图像。为了应用像 sampleRegions 这样的图像操作,您需要决定要做什么。

是否要为每个点获取筛选集合中可用的最新 像素?然后使用 .mosaic():

var image = imageCollection
    .filterDate(...)
    .filter(...)
    .map(...)
    .filterBounds(...)
    .mosaic();

你想要每个像素的时间序列的中值吗?然后使用 .median() 而不是 .mosaic()。 (或者meanminmax等也可以)

您想要在每个区域为集合中的每个图像单独采样点吗?然后对其进行映射以对每个图像进行采样:

var trainingImages = imageCollection
    .filterDate(...)
    .filter(...)
    .map(...)
    .filterBounds(...);

var trainingPoints = trainingImages.map(function (image) {
  return image.sampleRegions(...);
}).flatten();

(注意末尾的 .flatten();这是关键,因为这会为图像集合中的每个图像生成一个点集合,因此它将是点集合的集合,并且 .flatten()把它变成点的集合。)