采样点信息:
设置一下采样点参数:
代码:
//设置研究区位置
var table = ee.FeatureCollection("users/cduthes1991/boundry/China_province_2019");
var roi = table.filter(ee.Filter.eq('provinces','beijing'));
Map.centerObject(roi, 8)
Map.addLayer(roi.style({color:'black',fillColor:'00000000'}),{},'roi');//设置时间窗口
var startDate = '2023-03-01';
var endDate = '2023-10-01';
var L9 = ee.ImageCollection('LANDSAT/LC09/C02/T1_L2');// 进行比例换算
function applyScaleFactors(image) {var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);return image.addBands(opticalBands, null, true).addBands(thermalBands, null, true);
}
//去云处理
function rmL89Cloud(image) { var Cirrus = (1 << 2);var cloud = (1 << 3); var cloudsShadow = (1 << 4); var qa = image.select('QA_PIXEL'); var mask = qa.bitwiseAnd(Cirrus).eq(0) .and(qa.bitwiseAnd(cloud).eq(0)).and(qa.bitwiseAnd(cloudsShadow).eq(0)); return image.updateMask(mask);
}
//指数计算
function addIndexs (image){var ndvi = image.normalizedDifference(['SR_B5','SR_B4']).rename('ndvi');var bsi = image.expression("((SR_B7+SR_B3)-(SR_B4-SR_B1))/(SR_B7+SR_B3)+(SR_B4-SR_B1)",{"SR_B7": image.select('SR_B7'),"SR_B4": image.select('SR_B4'),"SR_B3": image.select('SR_B3'),"SR_B1": image.select('SR_B1')}).rename('bsi');var lswi = image.normalizedDifference(["SR_B4", "SR_B7"]).rename('lswi');var mndwi = image.addBands(image.normalizedDifference(["SR_B1", "SR_B7"]).rename('mndwi'));return image.addBands(ndvi).addBands(bsi).addBands(lswi).addBands(mndwi);
}
//获取研究区时间窗口内数据
var img = L9.filterDate(startDate,endDate).filterBounds(roi).map(applyScaleFactors).map(addIndexs).map(rmL89Cloud).median().clip(roi).select(['SR_B2','SR_B3','SR_B4','SR_B5','SR_B6','SR_B7','ndvi','bsi','lswi','mndwi'])
var visualization = {bands: ['SR_B4', 'SR_B3', 'SR_B2'],min: 0.0,max: 0.3,
};
Map.addLayer(img,visualization,'img');
print(img)//合并样本
var samples = water.merge(building).merge(forest);
print('Sample Collection', samples)//利用样本点对遥感数据进行采样
var samples_img = img.sampleRegions({collection:samples,properties:['landcover'],scale:30,tileScale:8,geometries:true,
})
print("sample information: ", samples_img)//给样本添加随机数,用于训练和验证
var samples_img = samples_img.randomColumn();
print('samples_img',samples_img)
// 将样本随机分为7:3,70%用于训练,30%用于验证
var training_samples = samples_img.filter(ee.Filter.lte('random',0.7));
var validation_samples = samples_img.filter(ee.Filter.gte('random',0.7));
//设置分类器
var classifer = ee.Classifier.smileRandomForest(300).train({features:training_samples,classProperty:'landcover',inputProperties:img.bandNames(),
})
//利用分类器对影像进行分类
var Classified_RF = img.classify(classifer)
var visParam = ['blue', // 水体water'red', // 建筑用地urban'green', // 林地forest
];//显示分类结果(色彩设置依据IGBG)
Map.addLayer(Classified_RF,{'palette':visParam, min:1,max:3},'Classified_RF');
//精度验证
var test = validation_samples.classify(classifer);
var confussionMatrix = test.errorMatrix('landcover','classification');
var accuracy = confussionMatrix.accuracy();
var kappa = confussionMatrix.kappa();
var userAccuracy = confussionMatrix.consumersAccuracy();
var productAccuracy = confussionMatrix.producersAccuracy();
print('accuracy',accuracy);
print('kappa',kappa);
print('userAccuracy',userAccuracy);
print('productAccuracy',productAccuracy);// 图像导出
Export.image.toDrive({image: Classified_RF,description: 'Classified_RF',region: roi,scale: 250,maxPixels: 1e13,folder: 'LUCC'
})
结果展示:
以上结果只是一个简单示例,需要设置更多的采样点才能更精确分类。
- GEE代码链接