首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >image.sampleRegions不是一种功能( google有监督的分类)

image.sampleRegions不是一种功能( google有监督的分类)
EN

Stack Overflow用户
提问于 2020-07-17 06:45:45
回答 2查看 2.7K关注 0票数 0

你好,我是一个初学者,目前正在谷歌地球引擎监督分类工作。我似乎无法克服'image.sampleRegions不是一个函数‘的问题。下面是我使用的脚本。

代码语言:javascript
运行
复制
/**
 * 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');

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-17 15:26:12

问题在于:

代码语言:javascript
运行
复制
var image = imageCollection
    .filterDate(...)
    .filter(...)
    .map(...)
    .filterBounds(...);

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

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

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

代码语言:javascript
运行
复制
var image = imageCollection
    .filterDate(...)
    .filter(...)
    .map(...)
    .filterBounds(...)
    .mosaic();

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

您是否希望为每个区域的集合中的每个图像分别设置采样点?然后对其进行映射,对每幅图像进行采样:

代码语言:javascript
运行
复制
var trainingImages = imageCollection
    .filterDate(...)
    .filter(...)
    .map(...)
    .filterBounds(...);

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

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

票数 1
EN

Stack Overflow用户

发布于 2020-07-17 07:34:06

很难从您的代码中猜测,但是通过一点点调试,您可能会发现它。尝试console.log image.sampleRegions到底是什么以及image对象的属性是什么。从那里你可以看到什么是错误的--未定义的属性,错误的类型等等。

广告:在快速查看了GEE文档之后,我想尝试将您的收藏包装到ee.Image()中。也许这会有帮助:

代码语言:javascript
运行
复制
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));

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62948540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档