你好,我是一个初学者,目前正在谷歌地球引擎监督分类工作。我似乎无法克服'image.sampleRegions不是一个函数‘的问题。下面是我使用的脚本。
/**
* 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');
发布于 2020-07-17 15:26:12
问题在于:
var image = imageCollection
.filterDate(...)
.filter(...)
.map(...)
.filterBounds(...);
var training = image.sampleRegions(...);
您对imageCollection
所做的一切仍然返回图像集合,而不是图像。为了应用像sampleRegions
这样的图像操作,您需要决定要做什么。
要获取筛选集合中每个点可用的最新像素吗?然后使用.mosaic()
var image = imageCollection
.filterDate(...)
.filter(...)
.map(...)
.filterBounds(...)
.mosaic();
你想要每个像素的时间序列的中位数吗?然后使用.median()
而不是.mosaic()
。(也可以使用mean
、min
、max
等)
您是否希望为每个区域的集合中的每个图像分别设置采样点?然后对其进行映射,对每幅图像进行采样:
var trainingImages = imageCollection
.filterDate(...)
.filter(...)
.map(...)
.filterBounds(...);
var trainingPoints = trainingImages.map(function (image) {
return image.sampleRegions(...);
}).flatten();
(请注意末尾的.flatten()
;这是关键,因为这会为图像集合中的每个图像生成一个点集合,因此它将是一个点集合,并且.flatten()
会将其转换为一个点集合。)
发布于 2020-07-17 07:34:06
很难从您的代码中猜测,但是通过一点点调试,您可能会发现它。尝试console.log image.sampleRegions
到底是什么以及image
对象的属性是什么。从那里你可以看到什么是错误的--未定义的属性,错误的类型等等。
广告:在快速查看了GEE文档之后,我想尝试将您的收藏包装到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));
https://stackoverflow.com/questions/62948540
复制相似问题