티스토리 뷰
이번 글은 센티넬 위성영상자료을 이용해서 구글 어스엔지 프로그램을 이용해서 토지피복 분류 과정을 설명한다.
1. 다음 코드로 센티널 영상 가져오기
var s2 = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED');
2. 전체 국립공원 경계 파일을 다음 코드로 가져오기
var basin = ee.FeatureCollection("WWF/HydroSHEDS/v1/Basins/hybas_7");
3. 토지피복을 분류하기 위해 피복별 샘플 포인트를 다음 코드로 가져오기
var gcp = ee.FeatureCollection("users/ujavalgandhi/e2e/arkavathy_gcps");
4. 전체 국립공원에서 해당 공원 경계를 filter 함수를 이용해서 가져오기
var arkavathy = basin.filter(ee.Filter.eq('HYBAS_ID', 4071139640));
var geometry = arkavathy.geometry();
Map.centerObject(geometry);
var rgbVis = {
min: 0.0,
max: 3000,
bands: ['B4', 'B3', 'B2'],
};
var filtered = s2
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30))
.filter(ee.Filter.date('2019-01-01', '2020-01-01'))
.filter(ee.Filter.bounds(geometry))
.select('B.*');
print(filtered)
var composite = filtered.median().clip(geometry);
// Display the input composite.
Map.addLayer(composite, rgbVis, 'image');
// Add a random column and split the GCPs into training and validation set
var gcp = gcp.randomColumn({
columnName:'random',
seed:2023,
distribution: 'normal'});
print("랜덤칼럼",gcp)
print(gcp.size())
// for validation. Normal recommended ratio is
// 70% training, 30% validation
var trainingGcp = gcp.filter(ee.Filter.lt('random', 0.7));
var validationGcp = gcp.filter(ee.Filter.gte('random', 0.7));
print(trainingGcp.size())
print(validationGcp.size())
// Overlay the point on the image to get training data.
var training = composite.sampleRegions({
collection: trainingGcp,
properties: ['landcover'],
scale: 10,
tileScale: 16
});
// Train a classifier.
var classifier = ee.Classifier.smileRandomForest(50)
.train({
features: training,
classProperty: 'landcover',
inputProperties: composite.bandNames()
});
// Classify the image.
var classified = composite.classify(classifier);
Map.addLayer(classified, {min: 0, max: 3, palette: ['gray', 'brown', 'blue', 'green']}, '2019');
//**************************************************************************
// Accuracy Assessment
//**************************************************************************
// Use classification map to assess accuracy using the validation fraction
// of the overall training set created above.
var test = classified.sampleRegions({
collection: validationGcp,
properties: ['landcover'],
tileScale: 16,
scale: 10,
});
var testConfusionMatrix = test.errorMatrix('landcover', 'classification')
// Printing of confusion matrix may time out. Alternatively, you can export it as CSV
print('Confusion Matrix', testConfusionMatrix);
print('Test Accuracy', testConfusionMatrix.accuracy());
// Alternate workflow
// This is similar to machine learning practice
var validation = composite.sampleRegions({
collection: validationGcp,
properties: ['landcover'],
scale: 10,
tileScale: 16
});
var test = validation.classify(classifier);
var testConfusionMatrix = test.errorMatrix('landcover', 'classification')
// Printing of confusion matrix may time out. Alternatively, you can export it as CSV
print('Confusion Matrix', testConfusionMatrix);
print('Test Accuracy', testConfusionMatrix.accuracy());
print('User Accuracy',testConfusionMatrix.consumersAccuracy())
print('Producers Accuracy',testConfusionMatrix.producersAccuracy())