var classes = 5;
var scale = 10;
var numpixels = 5000;
var start = '2020-11-01'
var end = '2021-03-31'
var roi = ee.FeatureCollection('projects/a-flyllf0313/assets/dachang');
function maskS2clouds(image) {
var qa = image.select('QA60');
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask).divide(10000);
}
var s2 = ee.ImageCollection('COPERNICUS/S2_SR');
var img = ee.Image(s2.filterBounds(roi)
.filterDate(start, end)
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
.map(maskS2clouds)
.mean()
.clip(roi));
var image = img.select('B2');
var image = image.addBands([img.select('B3'), img.select('B4'), img.select('B5'),
img.select('B6'), img.select('B7'), img.select('B8'),
img.select('B9')
]);
var training = image.sample({
region: roi,
scale: scale,
numPixels: numpixels
});
var clusterer = ee.Clusterer.wekaKMeans(classes).train(training);
var result = image.cluster(clusterer);
Map.centerObject(roi, 11);
Map.addLayer(image.clip(roi), {bands: ["B4", "B3", "B2"], min:0, max:0.25}, "raw_img");
Map.addLayer(result.randomVisualizer(), {}, 'clusters')
参考博文
【Earth Engine】基于GEE进行非监督学习