對圖像進(jìn)行土地利用分類颓屑,因此下面是監(jiān)督分類的流程以及代碼案例禾进。
1.首先分類最開始應(yīng)該建立樣本數(shù)據(jù)集,在這里我分了四類酷勺,然后就開始自己的采樣戴已,設(shè)立好分類后固该,對目標(biāo)進(jìn)行分類。
image.png
然后對每個樣本進(jìn)行顏色選擇和屬性定義
image.png
//選擇需要裁剪的矢量數(shù)據(jù)
var aoi = ee.FeatureCollection("users/yangyao19960805/NewFolder");
//加載矢量邊框糖儡,以便于在邊界內(nèi)選取樣本點(diǎn)
var empty = ee.Image().toByte();
var outline = empty.paint({
featureCollection:aoi, // 行政邊界命名為fc
color:0, //顏色透明
width:3 //邊界寬度
});
Map.addLayer(outline, {palette: "ff0000"}, "outline");
//Function to mask the clouds in Sentinel-2
function maskS2clouds(image) {
var qa = image.select('QA60');
// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask).divide(10000);
}
//Build the Sentinel 2 collection, filtered by date, bounds and percentage of cloud cover
var dataset = ee.ImageCollection('COPERNICUS/S2_SR')
.filterDate('2019-01-01','2020-12-31')
.filterBounds(aoi)
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',10))
.map(maskS2clouds);
print("Sentinel 2 Image Collection",dataset);
var dem = ee.Image("NASA/NASADEM_HGT/001")
// Construct Classfication Dataset
// RS Index Cacluate(NDVI\NDWI\EVI\BSI)
var add_RS_index = function(img){
var ndvi = img.normalizedDifference(['B8', 'B4']).rename('NDVI').copyProperties(img,['system:time_start']);
var ndwi = img.normalizedDifference(['B3', 'B8']).rename('NDWI').copyProperties(img,['system:time_start']);
var evi = img.expression('2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))',
{
'NIR': img.select('B8'),
'RED': img.select('B4'),
'BLUE': img.select('B2')
}).rename('EVI').copyProperties(img,['system:time_start']);
var bsi = img.expression('((RED + SWIR1) - (NIR + BLUE)) / ((RED + SWIR1) + (NIR + BLUE)) ',
{
'RED': img.select('B4'),
'BLUE': img.select('B2'),
'NIR': img.select('B8'),
'SWIR1': img.select('B11'),
}).rename('BSI').copyProperties(img,['system:time_start']);
var ibi = img.expression('(2 * SWIR1 / (SWIR1 + NIR) - (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1))) / (2 * SWIR1 / (SWIR1 + NIR) + (NIR / (NIR + RED) + GREEN / (GREEN + SWIR1)))', {
'SWIR1': img.select('B11'),
'NIR': img.select('B8'),
'RED': img.select('B4'),
'GREEN': img.select('B3')
}).rename('IBI').copyProperties(img,['system:time_start']);
return img.addBands([ndvi, ndwi, evi, bsi, ibi]);
};
var dataset = dataset.map(add_RS_index);
var bands = ['B2','B3','B4','B5','B6','B7','B8','B8A','B11','NDVI','NDWI','BSI'];
var imgcol_median = dataset.select(bands).median();
var aoi_dem = dem.select('elevation').clip(aoi).rename('DEM');
var construct_img = imgcol_median.addBands(aoi_dem).clip(aoi);
//分類樣本
var train_points = cropland.merge(grassland).merge(city).merge(forest).merge(water);
var train_data= construct_img.sampleRegions({
collection: train_points,
properties: ['landcover'],
scale: 10
});
//精度評價
var withRandom = train_data.randomColumn('random');//樣本點(diǎn)隨機(jī)的排列
var split = 0.7;
var trainingPartition = withRandom.filter(ee.Filter.lt('random', split));//篩選70%的樣本作為訓(xùn)練樣本
var testingPartition = withRandom.filter(ee.Filter.gte('random', split));//篩選30%的樣本作為測試樣本
//分類方法選擇隨機(jī)森林
var rf = ee.Classifier.smileRandomForest({
numberOfTrees: 20,
bagFraction: 0.8
}).train({
features: train_data,
classProperty: 'landcover',
// inputProperties: inputbands
});
//對哨兵數(shù)據(jù)進(jìn)行隨機(jī)森林分類
var img_classfication = construct_img.classify(rf);
//運(yùn)用測試樣本分類伐坏,確定要進(jìn)行函數(shù)運(yùn)算的數(shù)據(jù)集以及函數(shù)
var test = testingPartition.classify(rf);
//計(jì)算混淆矩陣
var confusionMatrix = test.errorMatrix('landcover', 'classification');
print('confusionMatrix',confusionMatrix);//面板上顯示混淆矩陣
print('overall accuracy', confusionMatrix.accuracy());//面板上顯示總體精度
print('kappa accuracy', confusionMatrix.kappa());//面板上顯示kappa值
Map.centerObject(aoi)
Map.addLayer(aoi);
Map.addLayer(img_classfication.clip(aoi), {min: 1, max: 4, palette: ['orange', 'blue', 'green','yellow']});
var class1=img_classfication.clip(aoi)
//導(dǎo)出分類圖
Export.image.toDrive({
image: class1,
description: 'rfclass',
fileNamePrefix: 'rf', //文件命名
folder: "class", //保存的文件夾
scale: 10, //分辨率
region: aoi, //研究區(qū)
maxPixels: 1e13, //最大像元素,默認(rèn)就好
crs: "EPSG:4326" //設(shè)置投影
});
分類好啦握联!
image.png