gee 基于landsat8影像多种机器学习算法用地分类

 使用的算法:随机森林、支持向量机、分类回归树

需要自己准备的数据:

Map.centerObject(studyarea,8);  //数字为缩放尺度
var roi = ee.Image().toByte().paint({featureCollection:studyarea,color:0,width:3});
Map.addLayer(roi, {palette: "red"}, 'studyarea');

var Date_Start = ee.Date('2018-01-01'); //定义起始时间
var Date_End = ee.Date('2019-01-01'); //定义终止时间

//Landsat-8数据筛选和去云
function maskL8sr(image) {
  // If the cloud bit (3) is set and the cloud confidence (9) is high
  // or the cloud shadow bit is set (4) and the cloud shadow confidence (11) is high, then it's a bad pixel.

  // Get the pixel QA band.
  var qa = image.select('QA_PIXEL');

  // Both flags should be set to zero, indicating clear conditions.
  var cloud = qa.bitwiseAnd(1<<3)
               .and(qa.bitwiseAnd(1<<9))
               .or(qa.bitwiseAnd(1<<4)
               .and(qa.bitwiseAnd(1<<11)))   //将多个掩膜合并加起来生成云和卷云的共同的大掩膜

// Remove edge pixels that don't occur in all bands and Return the masked image, scaled to reflectance, without the QA bands.
//not()函数 returns 0 if the input is non-zero, and 1 otherwise
  return image.updateMask(cloud.not())
      .select("SR_B[1-7]*").multiply(0.0000275).subtract(0.2).toFloat()   
      .copyProperties(image,["system:time_start"]);   //复制属性,默认即为所有属性
}
var L8_collection=L8SR
    .filterDate(Date_Start,Date_End)  //时间范围,结束日期不包含
    .filterBounds(studyarea)
    .map(maskL8sr)
    .median();  //对该集合调用算法
print(L8_collection,'L8_collection')  //给该数据集下方一个名称
//Map.addLayer(L8_collection)  //展示筛选的数据数据

//将波段号改为对应的波段颜色,便于直观调用
var L8_collection_1=L8_collection.select(["SR_B2","SR_B3","SR_B4","SR_B5","SR_B6","SR_B7"],["blue","green","red","nir","sir1","sir2"]);
print(L8_collection_1,'L8_collection_1')   
//Map.addLayer(L8_collection_1.select('blue'))

function addVariables(image) {
 
 
  return image
    // Add an NDVI band.
    .addBands(image.normalizedDifference(['nir', 'red']).rename('NDVI')).float()
    // //Add an NDBI band.
    .addBands(image.normalizedDifference(['sir1', 'nir']).rename('NDBI')).float()
  //Add an NDWI band.
    .addBands(image.normalizedDifference(['green', 'nir']).rename('NDWI')).float()
    //Add an EVI band.
    .addBands(image.expression('2.5*(b("nir")-b("red"))/(b("nir")+6*b("red")-7.5*b("blue")+1)').rename('EVI')).float()

}

var imgCol_s2=addVariables(L8_collection_1);
print(imgCol_s2,'imgCol_s2')

//加载样本数据
var samplepoints=building.merge(water).merge(forest).merge(crop).merge(luocaodi);
//定义特征指数
var bands = imgCol_s2.bandNames();

print(samplepoints,'samplepoints')
//Map.addLayer(crop.style({color: 'red', pointSize: 3, width: 1, fillColor: 'white'}),{}, 'Merged_Presence_Absence');
var samples = imgCol_s2.sampleRegions({
  collection: samplepoints,
  properties:['Id'],   //根据样本点的特有属性值区别不同的类别,此数据为ID标识
  scale: 30 });
print(samples,'samples')
Map.addLayer(samplepoints)

var withRandom = samples.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%的样本作为测试样本
print(trainingPartition,'trainingPartition')


//...........分类方法选择smileRandomForest()..............//
var classifier_smileRandomForest = ee.Classifier.smileRandomForest(20).train({
  features: trainingPartition,
  classProperty: 'Id',
  inputProperties: bands
});

//对Landsat-8进行分类
var class_img_smileRandomForest = imgCol_s2.select(bands).classify(classifier_smileRandomForest).clip(studyarea);
print(class_img_smileRandomForest,'class_img_smileRandomForest')
Map.addLayer(class_img_smileRandomForest, {min: 0, max: 4, palette: ['red','blue','green','orange','yellow']},'class_img_smileRandomForest');

//运用测试样本分类,确定要进行函数运算的数据集以及函数
var test_smileRandomForest = testingPartition.classify(classifier_smileRandomForest);
//计算混淆矩阵
var confusionMatrix_smileRandomForest = test_smileRandomForest.errorMatrix('Id', 'classification');
print('confusionMatrix_smileRandomForest',confusionMatrix_smileRandomForest);//面板上显示混淆矩阵
print('overall accuracy_smileRandomForest', confusionMatrix_smileRandomForest.accuracy());//面板上显示总体精度
print('kappa accuracy_smileRandomForest', confusionMatrix_smileRandomForest.kappa());//面板上显示kappa值
//...........分类方法smileRandomForest...........end..............//

//...........分类方法选择libsvm()..............//
var classifier_libsvm = ee.Classifier.libsvm().train({
  features: trainingPartition,
  classProperty: 'Id',
  inputProperties: bands
});

//对Landsat-8进行分类
var class_img_libsvm = imgCol_s2.select(bands).classify(classifier_libsvm).clip(studyarea);
print(class_img_libsvm,'class_img_libsvm')
Map.addLayer(class_img_libsvm, {min: 0, max: 4, palette: ['red','blue','green','orange','yellow']},'class_img_libsvm');

//运用测试样本分类,确定要进行函数运算的数据集以及函数
var test_libsvm = testingPartition.classify(classifier_libsvm);
//计算混淆矩阵
var confusionMatrix_libsvm = test_libsvm.errorMatrix('Id', 'classification');
print('confusionMatrix_libsvm',confusionMatrix_libsvm);//面板上显示混淆矩阵
print('overall accuracy_libsvm', confusionMatrix_libsvm.accuracy());//面板上显示总体精度
print('kappa accuracy_libsvm', confusionMatrix_libsvm.kappa());//面板上显示kappa值
//...........分类方法libsvm...........end..............//

//...........分类方法选择smileCart()..............//
var classifier_smileCart = ee.Classifier.smileCart().train({
  features: trainingPartition,
  classProperty: 'Id',
  inputProperties: bands
});

//对Landsat-8进行分类
var class_img_smileCart = imgCol_s2.select(bands).classify(classifier_smileCart).clip(studyarea);
print(class_img_smileCart,'class_img_smileCart')
Map.addLayer(class_img_smileCart, {min: 0, max: 4, palette: ['red','blue','green','orange','yellow']},'lass_img_smileCart');

//运用测试样本分类,确定要进行函数运算的数据集以及函数
var test_smileCart= testingPartition.classify(classifier_smileCart);
//计算混淆矩阵
var confusionMatrix_smileCart = test_smileCart.errorMatrix('Id', 'classification');
print('confusionMatrix_smileCart',confusionMatrix_smileCart);//面板上显示混淆矩阵
print('overall accuracy_smileCart', confusionMatrix_smileCart.accuracy());//面板上显示总体精度
print('kappa accuracy_smileCart', confusionMatrix_smileCart.kappa());//面板上显示kappa值
//...........分类方法smileCart...........end..............//

//...........输出图像..............//
Export.image.toDrive({
      image:class_img_smileRandomForest,
      fileNamePrefix:"2018landuse_smileRandomForest",
      crs: 'EPSG:4326',  //WGS84
      scale: 30,
      region: studyarea,
      maxPixels: 1e13
        })

Export.image.toDrive({
      image:class_img_libsvm,
      fileNamePrefix:"2018landuse_libsvm",
      crs: 'EPSG:4326',  //WGS84
      scale: 30,
      region: studyarea,
      maxPixels: 1e13
        })

Export.image.toDrive({
      image:class_img_smileCart,
      fileNamePrefix:"2018landuse_smileCart",
      crs: 'EPSG:4326',  //WGS84
      scale: 30,
      region: studyarea,
      maxPixels: 1e13
        })
//...........输出图像........end..............//

猜你喜欢

转载自blog.csdn.net/jiekeheiguanglan/article/details/131276082