function NDVI(img) {
var nir = img.select("B8");
var red = img.select("B4");
var ndvi = nir.subtract(red).divide(nir.add(red));
return ndvi;
}
var s2img = sen2.filterBounds(xcqbuf).filterDate('2020-08-11','2020-08-12').filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',50))
.first()
// // .map(maskS2clouds)
// // .select(['B2', 'B3', 'B4','B5', 'B6', 'B7','B8', 'B10', 'B11','B12'])
//
print(s2img)
Map.addLayer(s2img,{bands: ['B4', 'B3', 'B2'], min: 0, max: 3000})
Map.addLayer(s2img,{bands: ['B4', 'B8', 'B11'], min: 0, max: 3000})
Map.addLayer(NDVI(s2img))
// Map.addLayer(NDVI(s2img.first()))
Map.setCenter(120.548,31.484)
//随机森林分类
var mndwi = s2img.normalizedDifference(['B3', 'B6']).rename('MNDWI');//计算MNDWI
var ndbi = s2img.normalizedDifference(['B4', 'B8A']).rename('NDBI');//计算NDBI
var ndvi = s2img.normalizedDifference(['B8', 'B4']).rename('NDVI');//计算NDVI
s2img=s2img
.addBands(ndvi)
.addBands(ndbi)
.addBands(mndwi)
//选择样本
var classNames = zao.merge(water).merge(till).merge(hulu).merge(vege);
var bands = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7','B8','B8A', 'MNDWI','NDBI','NDVI'];
var training = s2img.select(bands).sampleRegions({
collection: classNames,
properties: ['landcover'],
scale: 10
});
// random uniforms to the training dataset.
var withRandom = training.randomColumn('random');//样本点随机的排列
// 我们想保留一些数据进行测试,以避免模型过度拟合。
var split = 0.7;
var trainingPartition = withRandom.filter(ee.Filter.lt('random', split));//筛选70%的样本作为训练样本
var testingPartition = withRandom.filter(ee.Filter.gte('random', split));//筛选30%的样本作为测试样本
// 选择分类的属性
var classProperty = 'landcover';
//分类方法选择smileCart() randomForest() minimumDistance libsvm
var classifier = ee.Classifier.smileRandomForest(10).train({
features: trainingPartition,
classProperty: 'landcover',
inputProperties: bands
});
var s2img1 = sen2.filterBounds(xcqbuf).filterDate('2020-08-16','2020-08-17').filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',10))
.first()
s2img=s2img
.addBands(ndvi)
.addBands(ndbi)
.addBands(mndwi)
// Map.addLayer(s2img1,{bands: ['B4', 'B8', 'B11'], min: 0, max: 3000})
//分类
var classified = s2img.select(bands).classify(classifier);
//Display the classification map
//Map.centerObject(classNames, 11);
var dict = classifier.explain();
print('Explain:',dict);//变量重要性
//Map.addLayer(classified,{min: 0, max: 4, palette: ['00FFFF', '00FF33','FFFF33','CC33FF','#C0C0C0']},'classification');
//print(classified)
var test = testingPartition.classify(classifier);//运用测试样本分类,确定要进行函数运算的数据集以及函数
var confusionMatrix = test.errorMatrix('landcover', 'classification');//计算混淆矩阵
print('confusionMatrix',confusionMatrix);//面板上显示混淆矩阵
print('overall accuracy', confusionMatrix.accuracy());//面板上显示总体精度
print('kappa accuracy', confusionMatrix.kappa());//面板上显示kappa值
// var smooth_map = s2img
// .focal_mode({
// radius: 2, kernelType: 'octagon', units: 'pixels', iterations: 1
// })
// .mask(classified.gte(1))
// var crude_object_removal = classified
// .updateMask(classified.connectedPixelCount(2, false).gte(2))
// .unmask(smooth_map)
// Define a palette for the Land Use classification.
var palette = [
'd63000', // forest(0)
'47f2ff', // water(1)
'42fd55', // arableland (2)
'ebea4a',// constructionland (4)
'd0d3ff',
];
// Display the classification result and the input image.
Map.addLayer(classified, {min: 0, max:4, palette: palette}, 'Land Use Classification');
//Map.addLayer(result,{min: 0, max: 4, palette: ['00FFFF', '00FF33', 'FFFF33','CC33FF','#C0C0C0']},'paddies');
//决策树数量选择
var numTrees = ee.List.sequence(5, 50, 5);
var accuracies = numTrees.map(function(t)
{
var classifier = ee.Classifier.smileRandomForest(t)
.train({
features: trainingPartition,
classProperty: 'landcover',
inputProperties: bands
});
return testingPartition
.classify(classifier)
.errorMatrix('landcover', 'classification')
.accuracy();
});
print(ui.Chart.array.values({
array: ee.Array(accuracies),
axis: 0,
xLabels: numTrees
}));