当前位置:   article > 正文

GEE随机森林分类代码(L5L8)_gee实现图像随机森林分类

gee实现图像随机森林分类

首先,显示图像来打点分类

  1. Map.centerObject(geometry,10);
  2. Map.addLayer(geometry);
  3. var s2 = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
  4. .filterDate('2020-11-01', '2021-02-28')
  5. .filterBounds(geometry)
  6. .filter(ee.Filter.lte('CLOUD_COVER',5))//云量设置
  7. .median();
  8. var visualization = {
  9. min: 0.0,
  10. max: 60000,
  11. bands: ['SR_B5', 'SR_B4', 'SR_B3'],
  12. };
  13. var clip_L8 = s2.clip(geometry)
  14. Map.addLayer(s2.clip(geometry), visualization, '假彩色');
  15. // 导出裁剪后的影像到 Google Drive
  16. Export.image.toDrive({
  17. image: clip_L8,
  18. description: 'clipped_image',
  19. folder: 'your_folder_name', // 替换为你想保存影像的 Google Drive 文件夹
  20. scale: 30, // 替换为你想要的分辨率
  21. region: geometry,
  22. maxPixels: 1e13,
  23. crs: 'EPSG:4326' // 替换为你的投影系统
  24. });

我的分类

Landsat5适用 
  1. Map.centerObject(geometry,10);
  2. Map.addLayer(geometry);
  3. var s2 = ee.ImageCollection('LANDSAT/LT05/C02/T1_L2')
  4. .filterDate('2010-11-18', '2010-11-20')
  5. .filterBounds(geometry)
  6. .filter(ee.Filter.lte('CLOUD_COVER',5))//云量设置
  7. var rgbVis = {
  8. min: 0.0,
  9. max: 60000,
  10. bands: ['SR_B4', 'SR_B3', 'SR_B2'],
  11. };
  12. //Map.addLayer(s2.clip(geometry), rgbVis, '假彩色');
  13. var add_RS_index = function(img){
  14. var ndvi = img.normalizedDifference(['SR_B4', 'SR_B3']).rename('NDVI').copyProperties(img,['system:time_start']);
  15. var ndwi = img.normalizedDifference(['SR_B2', 'SR_B4']).rename('NDWI').copyProperties(img,['system:time_start']);
  16. var ndmi = img.expression('(SWIR1 - GREEN) / (SWIR1 + GREEN)',
  17. {
  18. 'SWIR1': img.select('SR_B5'),
  19. 'GREEN': img.select('SR_B2')
  20. }).rename('NDMI').copyProperties(img,['system:time_start']);
  21. // Remove BSI and EVI
  22. var ibi = img.expression('(2 * SWIR1 / (SWIR1 + NIR) - (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1))) / (2 * SWIR1 / (SWIR1 + NIR) + (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1)))', {
  23. 'SWIR1': img.select('SR_B5'),
  24. 'NIR': img.select('SR_B4'),
  25. 'RED': img.select('SR_B3'),
  26. 'GREEN': img.select('SR_B2')
  27. }).rename('IBI').copyProperties(img,['system:time_start']);
  28. // Add NDSSI and NDBI
  29. var ndssi = img.expression('(GREEN - SWIR1) / (GREEN + SWIR1)',
  30. {
  31. 'GREEN': img.select('SR_B2'),
  32. 'SWIR1': img.select('SR_B5')
  33. }).rename('NDSSI').copyProperties(img,['system:time_start']);
  34. var ndbi = img.normalizedDifference(['SR_B4', 'SR_B7']).rename('NDBI').copyProperties(img,['system:time_start']);
  35. return img.addBands([ndvi, ndwi, ndmi, ibi, ndssi, ndbi]);
  36. };
  37. // Apply the function to the image collection
  38. var s2_with_indices = s2.map(add_RS_index);
  39. // 从图像集合中选择波段并计算中位数
  40. var bands = ['SR_B2','SR_B3', 'SR_B4', 'SR_B5', 'SR_B7', 'NDVI', 'NDWI', 'NDSSI'];
  41. var imgcol_median = s2_with_indices.select(bands).median();
  42. // 处理 DEM 数据
  43. var aoi_dem = dem.select('elevation').clip(geometry).rename('DEM');
  44. var construct_img = imgcol_median.addBands(aoi_dem).clip(geometry);
  45. var fc = vegetation.merge(water).merge(Tidalflat).merge(other).merge(spartina).merge(nomalforest).merge(plowland);
  46. var train_data=construct_img.sampleRegions({
  47. collection:fc,
  48. properties: ['landcover'],
  49. scale: 10
  50. });
  51. print(fc);
  52. var sampleData =train_data.randomColumn('random')
  53. var sample_training = sampleData.filter(ee.Filter.lte("random", 0.8));
  54. var sample_validate =sampleData.filter(ee.Filter.gt("random", 0.8));
  55. var rf = ee.Classifier.smileRandomForest({
  56. numberOfTrees: 20,
  57. bagFraction: 0.8
  58. }).train({
  59. features: sample_training,
  60. classProperty: 'landcover',
  61. inputProperties: bands
  62. });
  63. var img_classfication = construct_img.classify(rf);
  64. //运用测试样本分类,确定要进行函数运算的数据集以及函数
  65. var test =sample_validate.classify(rf);
  66. //计算混淆矩阵
  67. var confusionMatrix = test.errorMatrix('landcover', 'classification');
  68. print('confusionMatrix',confusionMatrix);//面板上显示混淆矩阵
  69. print('overall accuracy', confusionMatrix.accuracy());//面板上显示总体精度
  70. print('kappa accuracy', confusionMatrix.kappa());//面板上显示kappa值
  71. print('consumersAccuracy', confusionMatrix.consumersAccuracy());
  72. print('producersAccuracy', confusionMatrix.producersAccuracy());
  73. Map.centerObject(geometry)
  74. Map.addLayer(geometry);
  75. Map.addLayer(img_classfication.clip(geometry), {min: 1, max: 7, palette: ['blue', 'green', 'yellow', 'purple', 'orange', 'pink', 'brown',]});
  76. var class1=img_classfication.clip(geometry)
  77. //导出分类图
  78. Export.image.toDrive({
  79. image: class1,
  80. description: 'rfclass',
  81. fileNamePrefix: 'rf', //文件命名
  82. folder: "class", //保存的文件夹
  83. scale: 10, //分辨率
  84. region: geometry, //研究区
  85. maxPixels: 1e13, //最大像元素,默认就好
  86. crs: "EPSG:4326" //设置投影
  87. });
Landsat8适用
  1. Map.centerObject(geometry,10);
  2. Map.addLayer(geometry);
  3. var s2 = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
  4. .filterDate('2015-10-15', '2015-10-18')
  5. .filterBounds(geometry)
  6. .filter(ee.Filter.lte('CLOUD_COVER',6))//云量设置
  7. var rgbVis = {
  8. min: 0.0,
  9. max: 60000,
  10. bands: ['SR_B5', 'SR_B4', 'SR_B3'],
  11. };
  12. //Map.addLayer(s2.clip(geometry), rgbVis, '假彩色');
  13. var add_RS_index = function(img){
  14. var ndvi = img.normalizedDifference(['SR_B5', 'SR_B4']).rename('NDVI').copyProperties(img,['system:time_start']);
  15. var ndwi = img.normalizedDifference(['SR_B3', 'SR_B5']).rename('NDWI').copyProperties(img,['system:time_start']);
  16. var ndmi = img.expression('(SWIR1 - GREEN) / (SWIR1 + GREEN)',
  17. {
  18. 'SWIR1': img.select('SR_B6'),
  19. 'GREEN': img.select('SR_B3')
  20. }).rename('NDMI').copyProperties(img,['system:time_start']);
  21. // Remove BSI and EVI
  22. var ibi = img.expression('(2 * SWIR1 / (SWIR1 + NIR) - (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1))) / (2 * SWIR1 / (SWIR1 + NIR) + (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1)))', {
  23. 'SWIR1': img.select('SR_B6'),
  24. 'NIR': img.select('SR_B5'),
  25. 'RED': img.select('SR_B4'),
  26. 'GREEN': img.select('SR_B3')
  27. }).rename('IBI').copyProperties(img,['system:time_start']);
  28. // Add NDSSI and NDBI
  29. var ndssi = img.expression('(GREEN - SWIR1) / (GREEN + SWIR1)',
  30. {
  31. 'GREEN': img.select('SR_B3'),
  32. 'SWIR1': img.select('SR_B6')
  33. }).rename('NDSSI').copyProperties(img,['system:time_start']);
  34. var ndbi = img.normalizedDifference(['SR_B5', 'SR_B7']).rename('NDBI').copyProperties(img,['system:time_start']);
  35. return img.addBands([ndvi, ndwi, ndmi, ibi, ndssi, ndbi]);
  36. };
  37. // Apply the function to the image collection
  38. var s2_with_indices = s2.map(add_RS_index);
  39. // 从图像集合中选择波段并计算中位数
  40. var bands = ['SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7', 'NDVI', 'NDWI', 'NDSSI'];
  41. var imgcol_median = s2_with_indices.select(bands).median();
  42. // 处理 DEM 数据
  43. var aoi_dem = dem.select('elevation').clip(geometry).rename('DEM');
  44. var construct_img = imgcol_median.addBands(aoi_dem).clip(geometry);
  45. var fc = vegetation.merge(water).merge(Tidalflat).merge(other).merge(spartina).merge(nomalforest).merge(plowland);
  46. var train_data=construct_img.sampleRegions({
  47. collection:fc,
  48. properties: ['landcover'],
  49. scale: 10
  50. });
  51. print(fc);
  52. var sampleData =train_data.randomColumn('random')
  53. var sample_training = sampleData.filter(ee.Filter.lte("random", 0.8));
  54. var sample_validate =sampleData.filter(ee.Filter.gt("random", 0.8));
  55. var rf = ee.Classifier.smileRandomForest({
  56. numberOfTrees: 20,
  57. bagFraction: 0.8
  58. }).train({
  59. features: sample_training,
  60. classProperty: 'landcover',
  61. inputProperties: bands
  62. });
  63. var img_classfication = construct_img.classify(rf);
  64. //运用测试样本分类,确定要进行函数运算的数据集以及函数
  65. var test =sample_validate.classify(rf);
  66. //计算混淆矩阵
  67. var confusionMatrix = test.errorMatrix('landcover', 'classification');
  68. print('confusionMatrix',confusionMatrix);//面板上显示混淆矩阵
  69. print('overall accuracy', confusionMatrix.accuracy());//面板上显示总体精度
  70. print('kappa accuracy', confusionMatrix.kappa());//面板上显示kappa值
  71. print('consumersAccuracy', confusionMatrix.consumersAccuracy());
  72. print('producersAccuracy', confusionMatrix.producersAccuracy());
  73. Map.centerObject(geometry)
  74. Map.addLayer(geometry);
  75. Map.addLayer(img_classfication.clip(geometry), {min: 1, max: 7, palette: ['blue', 'green', 'yellow', 'purple', 'orange', 'pink', 'brown']});
  76. var class1=img_classfication.clip(geometry)
  77. //导出分类图
  78. Export.image.toDrive({
  79. image: class1,
  80. description: 'rfclass',
  81. fileNamePrefix: 'rf', //文件命名
  82. folder: "class", //保存的文件夹
  83. scale: 10, //分辨率
  84. region: geometry, //研究区
  85. maxPixels: 1e13, //最大像元素,默认就好
  86. crs: "EPSG:4326" //设置投影
  87. });

可能报错及解决:

运行后显示Property 'B2' of feature '1_1_1_1_1_1_1_1_0' is missing 

云量限制过大,放宽条件

 参考波段

 

 参考:

当时前一部分就是按照b站这个老哥来做的,后面他没录上,参考了第二个姐的代码

【google earth engine 随机森林分类详细流程】 https://www.bilibili.com/video/BV18v411a78D/?share_source=copy_web&vd_source=cf8beea9c5503f5fd4699cbf701fc2cf

GEE实现图像随机森林分类 - 简书 (jianshu.com)

【No.9 【Google Earth Engine】GEE 基于landsat8 监督分类土地利用并下载】 https://www.bilibili.com/video/BV1YY411s7tS/?share_source=copy_web&vd_source=cf8beea9c5503f5fd4699cbf701fc2cf

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/239185
推荐阅读
相关标签
  

闽ICP备14008679号