-
Notifications
You must be signed in to change notification settings - Fork 0
/
5. Object-oriented classifications
715 lines (611 loc) · 31.4 KB
/
5. Object-oriented classifications
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/* Author: Afag Rizayeva
Date: 01-17-2022
Purpose: Ten fold object-based classification of Corona data
Inputs: Corona image geometry (study area polygon);
2 Corona image layers manually imported in GEE;
texture layers;
2.5m elevation layer;
training data directory;
testing data directory;
Adjust the input files' directories and palette variable
Run the code
Export the accuracies as csv files and the final classification maps
*/
var table = ee.FeatureCollection('users/rizayeva/DS10111041');
// Import and visualize textures
var entropy_DA = ee.Image('users/rizayeva/GLCM/entropy_DA').toFloat();
var entropy_DF = ee.Image('users/rizayeva/GLCM/entropy_DF').toFloat();
var homogeneity_DA = ee.Image('users/rizayeva/GLCM/homogeneity_DA').toFloat();
var homogeneity_DF = ee.Image('users/rizayeva/GLCM/homogeneity_DF').toFloat();
var angularSecondMoment_DA = ee.Image('users/rizayeva/GLCM/angularSecondMoment_DA').toFloat();
var angularSecondMoment_DF = ee.Image('users/rizayeva/GLCM/angularSecondMoment_DF').toFloat();
print('img band names', entropy_DA.bandNames()); // ee.List of band names
// Set the palette based on number and names of classes
var palette = ['075b0e'/* forest*/, '075b0e'/* forest*/, '075b0e'/* forest*/, 'ebebeb'/* barren*/, '9de64e'/* grass*/, 'e2d544'/* crop*/, 'ff0000'/* urban*/, '3f553c'/* wetland*/, '0033ff'/* lake*/, 'ffffff'/* snow*/, '88cdf6'/* river*/];
// Import two Corona images
// Rename the band names for afterward- ('da') and forward-facing ('df') cameras
var image2 = ee.Image('users/rizayeva/corona_2_5m/DS1011-1040DA_2_5m_JPEG_1band_EPSG32638').rename(['ds10111040da']);
var image3 = ee.Image('users/rizayeva/corona_2_5m/DS1011-1040DF_2_5m_JPEG_1band_EPSG32638').rename(['ds10111040df']);
Map.addLayer(image2, {min: 0, max: 200}, 'DS1011-1040DA', false);
Map.addLayer(image3, {min: 0, max: 200}, 'DS1011-1040DF', false);
// Define and visualize the elevation layers
var elevation = ee.Image('users/rizayeva/elevation').rename('elevation');
var slope = ee.Terrain.slope(elevation).clip(table);
var aspect = ee.Terrain.aspect(elevation).clip(table);
var aspect = aspect.subtract(30).multiply(Math.PI/180).cos().subtract(1).multiply(-1/2);
var imageVisParams = {bands: ['elevation'], min: 1, max: 3445, gamma: [1.85], opacity: 1};
Map.addLayer(elevation, imageVisParams, 'Elevation', false);
Map.addLayer(slope, {}, 'slope', false);
Map.addLayer(aspect, {}, 'aspect', false);
// Concatenate the two images, so that we have one image with two bands
var img = ee.Image.cat([image2, image3]);
// print('img', img);
var img = ee.Image(img).divide(255);
var bandNames = img.bandNames();
print('img band names', bandNames); // ee.List of band names
Map.addLayer(img, {bands: ["ds10111040df", "ds10111040df", "ds10111040da"], gamma: 0.8}, 'DS1011-1040', false);
Map.centerObject(img.geometry(), 7);
///////////// Segmentation
// Set the seed grid size
var seeds = ee.Algorithms.Image.Segmentation.seedGrid(40);
// Run SNIC on the regular square grid.
var snic = ee.Algorithms.Image.Segmentation.SNIC({
image: img,
size: 32,
compactness: 0.1,
connectivity: 4,
neighborhoodSize:256,
seeds: seeds
}).select(['ds10111040da_mean', 'ds10111040df_mean', 'clusters'], ['ds10111040da', 'ds10111040df', 'clusters']);//.reproject({crs: 'EPSG:4326', scale: 18});
// var snicbandNames = snic.bandNames();
// print('Snic band names: ', snicbandNames); // ee.List of band names
// Select clusters from segmentation results
var clusters = snic.select('clusters').reproject({crs: 'EPSG:4326', scale: 18});
//Map.addLayer(clusters.randomVisualizer(), {}, 'clusters', false)
Map.addLayer(snic, {bands: ['ds10111040da', 'ds10111040df'/*, 'b1_2', 'b1_3'*/], min:0, max:1, gamma: 0.8}, 'means', false);
// Compute per-cluster StdDev, Area, Perimeter, Width and Height, Shape, Texture metrics and Elevation layers
var stdDev = img.addBands(clusters).reduceConnectedComponents(ee.Reducer.stdDev(), 'clusters', 1024).select(['ds10111040da', 'ds10111040df'], ['ds10111040da_StdDev', 'ds10111040df_StdDev']).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(stdDev, {min:0, max:0.1}, 'StdDev', false);
var area = ee.Image.pixelArea().addBands(clusters).reduceConnectedComponents(ee.Reducer.sum(), 'clusters', 1024).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(area, {min:50000, max: 500000}, 'Cluster Area', false);
var minMax = clusters.reduceNeighborhood(ee.Reducer.minMax(), ee.Kernel.square(1)).reproject({crs: 'EPSG:4326', scale: 18}).reproject({crs: 'EPSG:4326', scale: 18});
var perimeterPixels = minMax.select(0).neq(minMax.select(1)).rename('perimeter').reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(perimeterPixels, {min: 0, max: 1, opacity: 0.5, palette: ['000000', 'ffffff']}, 'perimeterPixels', false);
var perimeter = perimeterPixels.addBands(clusters).reduceConnectedComponents(ee.Reducer.sum(), 'clusters', 1024).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(perimeter, {min: 100, max: 400}, 'Perimeter size', false);
var sizes = ee.Image.pixelLonLat().addBands(clusters).reduceConnectedComponents(ee.Reducer.minMax(), 'clusters', 1024).reproject({crs: 'EPSG:4326', scale: 18});
var width = sizes.select('longitude_max').subtract(sizes.select('longitude_min')).rename('width').reproject({crs: 'EPSG:4326', scale: 18});
var height = sizes.select('latitude_max').subtract(sizes.select('latitude_min')).rename('height').reproject({crs: 'EPSG:4326', scale: 18}).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(width, {min:0, max:0.02}, 'Cluster width', false);
Map.addLayer(height, {min:0, max:0.02}, 'Cluster height', false);
var shape = area.sqrt().divide(perimeter).select(['area'], ['shape']).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(shape, {}, 'shape', false);
var entropyDA = entropy_DA.addBands(clusters).reduceConnectedComponents(ee.Reducer.mean(), 'clusters', 1024).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(entropyDA, null, 'entropyDA', false);
var entropyDF = entropy_DF.addBands(clusters).reduceConnectedComponents(ee.Reducer.mean(), 'clusters', 1024).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(entropyDF, null, 'entropyDF', false);
var homogeneityDA = homogeneity_DA.addBands(clusters).reduceConnectedComponents(ee.Reducer.mean(), 'clusters', 1024).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(homogeneityDA, null, 'homogeneityDA', false);
var homogeneityDF = homogeneity_DF.addBands(clusters).reduceConnectedComponents(ee.Reducer.mean(), 'clusters', 1024).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(homogeneityDF, null, 'homogeneityDF', false);
var angularSecondMomentDA = angularSecondMoment_DA.addBands(clusters).reduceConnectedComponents(ee.Reducer.mean(), 'clusters', 1024).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(angularSecondMomentDA, null, 'angularSecondMomentDA', false);
var angularSecondMomentDF = angularSecondMoment_DF.addBands(clusters).reduceConnectedComponents(ee.Reducer.mean(), 'clusters', 1024).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(angularSecondMomentDF, null, 'angularSecondMomentDF', false);
var elevation_10111040 = elevation.addBands(clusters).reduceConnectedComponents(ee.Reducer.mean(), 'clusters', 1024).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(elevation_10111040, {}, 'elevation_10111040', false);
var slope_10111040 = slope.addBands(clusters).reduceConnectedComponents(ee.Reducer.mean(), 'clusters', 1024).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(slope_10111040, {}, 'slope_10111040', false);
var aspect_10111040 = aspect.addBands(clusters).reduceConnectedComponents(ee.Reducer.mean(), 'clusters', 1024).reproject({crs: 'EPSG:4326', scale: 18});
Map.addLayer(aspect_10111040, {}, 'aspect_10111040', false);
// Map asset directory that contains all Training and Testing data
var assetIdTR='users/rizayeva/Training_Testing/Training10111040';
var assetIdTE='users/rizayeva/Training_Testing/Testing10111040';
// Make a list that contains all the point data in the asset directory
var assetListTR = ee.List(ee.data.getList({'id':assetIdTR}));
var assetListTE = ee.List(ee.data.getList({'id':assetIdTE}));
print('Training data', assetListTR);
print('Testing data', assetListTE);
// Define the number of datasets
var n = assetListTR.size().getInfo();
///////////// Classification
///////////// ///////////// ///////////// ///////////// ///////////// no DEM no GEO
// Concatenate all layers into one
var objectPropertiesImage = ee.Image.cat([
snic.select(['ds10111040da', 'ds10111040df']),
stdDev,
entropyDA,
entropyDF,
homogeneityDA,
homogeneityDF,
angularSecondMomentDA,
angularSecondMomentDF
]).float();
// print (objectPropertiesImage);
var bandNames = objectPropertiesImage.bandNames();
print('objectPropertiesImage bandNames noDEMnoGEO', bandNames);
var bandss = ['ds10111040da', 'ds10111040df', 'ds10111040da_StdDev', 'ds10111040df_StdDev', 'ds10111040da_ent', 'ds10111040df_ent', 'ds10111040da_idm', 'ds10111040df_idm', 'ds10111040da_asm', 'ds10111040df_asm'];
// Generate an empty Image Collection to later add each of the 10 classification results
var classification = ee.ImageCollection([]);
// Generate an empty list to later add each of the overall accuracies to be averaged
var overallAccuracy = ee.List([]);
// Build a for loop to run a Random Forest classifier using one set of the training and testing data each time
for (var i=0; i<n; i++){
// Select the subsequent set of training data each time
var valueTR=ee.Dictionary(assetListTR.get(i));
var imgIDTR=ee.String(valueTR.get('id')).getInfo();
var train=ee.FeatureCollection(imgIDTR);
// print('train' + (i+1), train);
// trainingTR=training.merge(train)
// Overlay the points on the imagery to get training.
var training = objectPropertiesImage.sampleRegions({
collection: train,
properties: ['Classes'],
scale: 2.5
});
// Train a CART classifier with default parameters
var classifier = ee.Classifier.smileRandomForest(100).train(training, 'Classes', bandss);
// Classify the image with the same bands used for training
var classified = objectPropertiesImage.classify(classifier);
// print('classified', classified);
// Map.addLayer(classified, {min: 1, max: 11, palette: palette}, 'Classification map' + (i+1), false);
// // Export the Image - uncomment if want to export each
// Export.image.toAsset({
// image: classified.toByte(),//.toFloat(),
// description: "Obj_noDEMnoGEO" + (i+1),
// assetId: "Obj_noDEMnoGEO" + (i+1),
// scale: 2.5,
// region: table,
// maxPixels:1e13,
// });
// Overall map
classification=classification.merge(classified);
// Validation
// Select the subsequent set of testing data each time
var valueTE=ee.Dictionary(assetListTE.get(i));
var imgIDTE=ee.String(valueTE.get('id')).getInfo();
var test=ee.FeatureCollection(imgIDTE);
// print('test', test);
// testing=testing.merge(test)
var validation = objectPropertiesImage.sampleRegions({
collection: test,
properties: ['Classes'],
scale: 2.5
});
var validated = validation.classify(classifier);
// Get a confusion matrix representing expected accuracy and an overall accuracy
// Uncomment the "print" commands if want to visualize the accuracies and matrices for each set of training and testing data in GEE console
var testAccuracy = validated.errorMatrix('Classes', 'classification');
var Matrix = ee.Feature(null, {matrix: testAccuracy.array()});
// print('Validation error matrix noDEMnoGEO' + (i+1), testAccuracy);
var overall = testAccuracy.accuracy();
// print('Overall noDEMnoGEO' + (i+1), overall);
// // Export the matrix - uncomment if want to export each
// Export.table.toDrive({
// collection: ee.FeatureCollection(Matrix),
// description:'testAccuracy_Obj_noDEMnoGEO' + (i+1),
// folder: 'testAccuracy',
// fileFormat: 'csv'
// });
// Add subsequent overall accuracy with each loop, to initially empty "overallAccuracy" list
overallAccuracy = overallAccuracy.add(overall);
}
// Image collection of classification results from each set of training data
var classifiedX = classification;
// print('Classification maps collection', classifiedX);
// Best map representing the most common pixel value based on all sets of training data
var bestClassificationObj = classifiedX.mode();
// The following map may not load due to memory limits, but should succesfully export to Assets or Drive
Map.addLayer(bestClassificationObj.toByte(), {min: 1, max: 11, palette: palette}, "Best object-oriented classification no DEM no GEO", false);
// Export the best map to Drive
Export.image.toDrive({
image: bestClassificationObj.toByte(),
description: "Obj_noDEMnoGEO",
folder: "objectBasedClassifications",
scale: 2.5,
region: table,
maxPixels:1e13,
});
// Calculate the average overall accuracy
var overallAccuracyObj = ee.FeatureCollection(overallAccuracy.reduce(ee.Reducer.mean()));
var overallAccuracyObjExp = ee.FeatureCollection(ee.Feature(null,{prop: overallAccuracyObj}));
print('Overall accuracy noDEMnoGEO', overallAccuracyObjExp);
// Calculate the Standard Deviation based on overall accuracies from all training data
var standardDeviation = ee.FeatureCollection(overallAccuracy.reduce(ee.Reducer.stdDev()));
var standardDeviationExp = ee.FeatureCollection(ee.Feature(null,{prop: standardDeviation}));
print('SD noDEMnoGEO', standardDeviationExp);
// The "print" function may not always work due to the memory limits
// To make sure it works, we can reduce the number of training and testing datasets used
// For example, use just two datasets (2 instead of n in the beginning of the for loop)
// Otherwise, it should always be possible to export them to Drive
Export.table.toDrive({
collection: overallAccuracyObjExp,
description:'overallAccuracyObjnoDEMnoGEO10111040',
folder: 'classification_accuracies',
fileFormat: 'csv'
});
Export.table.toDrive({
collection: standardDeviationExp,
description:'standardDeviationObjnoDEMnoGEO10111040',
folder: 'classification_accuracies',
fileFormat: 'csv'
});
///////////// ///////////// ///////////// ///////////// ///////////// no DEM with GEO
// Concatenate all layers into one
var objectPropertiesImage = ee.Image.cat([
snic.select(['ds10111040da', 'ds10111040df']),
stdDev,
area,
perimeter,
width,
height,
shape,
entropyDA,
entropyDF,
homogeneityDA,
homogeneityDF,
angularSecondMomentDA,
angularSecondMomentDF
]).float();
// print (objectPropertiesImage);
var bandNames = objectPropertiesImage.bandNames();
print('objectPropertiesImage bandNames noDEMwGEO', bandNames);
var bandss = ['ds10111040da', 'ds10111040df', 'ds10111040da_StdDev', 'ds10111040df_StdDev', 'area', 'perimeter', 'width', 'height', 'shape', 'ds10111040da_ent', 'ds10111040df_ent', 'ds10111040da_idm', 'ds10111040df_idm', 'ds10111040da_asm', 'ds10111040df_asm'];
// Generate an empty Image Collection to later add each of the 10 classification results
var classification = ee.ImageCollection([]);
// Generate an empty list to later add each of the overall accuracies to be averaged
var overallAccuracy = ee.List([]);
// Build a for loop to run a Random Forest classifier using one set of the training and testing data each time
for (var i=0; i<n; i++){
// Select the subsequent set of training data each time
var valueTR=ee.Dictionary(assetListTR.get(i));
var imgIDTR=ee.String(valueTR.get('id')).getInfo();
var train=ee.FeatureCollection(imgIDTR);
// print('train' + (i+1), train);
// trainingTR=training.merge(train)
// Overlay the points on the imagery to get training.
var training = objectPropertiesImage.sampleRegions({
collection: train,
properties: ['Classes'],
scale: 2.5
});
// Train a CART classifier with default parameters
var classifier = ee.Classifier.smileRandomForest(100).train(training, 'Classes', bandss);
// Classify the image with the same bands used for training
var classified = objectPropertiesImage.classify(classifier);
// print('classified', classified);
// Map.addLayer(classified, {min: 1, max: 11, palette: palette}, 'Classification map' + (i+1), false);
// // Export the Image - uncomment if want to export each
// Export.image.toAsset({
// image: classified.toByte(),//.toFloat(),
// description: "Obj_noDEMwGEO" + (i+1),
// assetId: "Obj_noDEMwGEO" + (i+1),
// scale: 2.5,
// region: table,
// maxPixels:1e13,
// });
// Overall map
classification=classification.merge(classified);
// Validation
// Select the subsequent set of testing data each time
var valueTE=ee.Dictionary(assetListTE.get(i));
var imgIDTE=ee.String(valueTE.get('id')).getInfo();
var test=ee.FeatureCollection(imgIDTE);
// print('test', test);
// testing=testing.merge(test)
var validation = objectPropertiesImage.sampleRegions({
collection: test,
properties: ['Classes'],
scale: 2.5
});
var validated = validation.classify(classifier);
// Get a confusion matrix representing expected accuracy and an overall accuracy
// Uncomment the "print" commands if want to visualize the accuracies and matrices for each set of training and testing data in GEE console
var testAccuracy = validated.errorMatrix('Classes', 'classification');
var Matrix = ee.Feature(null, {matrix: testAccuracy.array()});
// print('Validation error matrix_noDEMwGEO' + (i+1), testAccuracy);
var overall = testAccuracy.accuracy();
// print('Overall noDEMwGEO' + (i+1), overall);
// // Export the matrix - uncomment if want to export each
// Export.table.toDrive({
// collection: ee.FeatureCollection(Matrix),
// description:'testAccuracy_Obj_noDEMwGEO' + (i+1),
// folder: 'testAccuracy',
// fileFormat: 'csv'
// });
// Add subsequent overall accuracy with each loop, to initially empty "overallAccuracy" list
overallAccuracy = overallAccuracy.add(overall);
}
// Image collection of classification results from each set of training data
var classifiedX = classification;
// print('Classification maps collection', classifiedX);
// Best map representing the most common pixel value based on all sets of training data
var bestClassificationObj = classifiedX.mode();
// The following map may not load due to memory limits, but should succesfully export to Assets or Drive
Map.addLayer(bestClassificationObj.toByte(), {min: 1, max: 11, palette: palette}, "Best object-oriented classification no DEM with GEO", false);
// Export the best map to Drive
Export.image.toDrive({
image: bestClassificationObj.toByte(),
description: "Obj_noDEMwGEO",
folder: "objectBasedClassifications",
scale: 2.5,
region: table,
maxPixels:1e13,
});
// Calculate the average overall accuracy
var overallAccuracyObj = ee.FeatureCollection(overallAccuracy.reduce(ee.Reducer.mean()));
var overallAccuracyObjExp = ee.FeatureCollection(ee.Feature(null,{prop: overallAccuracyObj}));
print('Overall accuracy noDEMwGEO', overallAccuracyObjExp);
// Calculate the Standard Deviation based on overall accuracies from all training data
var standardDeviation = ee.FeatureCollection(overallAccuracy.reduce(ee.Reducer.stdDev()));
var standardDeviationExp = ee.FeatureCollection(ee.Feature(null,{prop: standardDeviation}));
print('SD noDEMwGEO', standardDeviationExp);
// The "print" function may not always work due to the memory limits
// To make sure it works, we can reduce the number of training and testing datasets used
// For example, use just two datasets (2 instead of n in the beginning of the for loop)
// Otherwise, it should always be possible to export them to Drive
Export.table.toDrive({
collection: overallAccuracyObjExp,
description:'overallAccuracyObjnoDEMwGEO10111040',
folder: 'classification_accuracies',
fileFormat: 'csv'
});
Export.table.toDrive({
collection: standardDeviationExp,
description:'standardDeviationObjnoDEMwGEO10111040',
folder: 'classification_accuracies',
fileFormat: 'csv'
});
///////////// ///////////// ///////////// ///////////// ///////////// with DEM no GEO
// Concatenate all layers into one
var objectPropertiesImage = ee.Image.cat([
snic.select(['ds10111040da', 'ds10111040df']),
stdDev,
entropyDA,
entropyDF,
homogeneityDA,
homogeneityDF,
angularSecondMomentDA,
angularSecondMomentDF,
elevation_10111040,
slope_10111040,
aspect_10111040
]).float();
// print (objectPropertiesImage);
var bandNames = objectPropertiesImage.bandNames();
print('objectPropertiesImage bandNames wDEMnoGEO', bandNames);
var bandss = ['ds10111040da', 'ds10111040df', 'ds10111040da_StdDev', 'ds10111040df_StdDev', 'ds10111040da_ent', 'ds10111040df_ent', 'ds10111040da_idm', 'ds10111040df_idm', 'ds10111040da_asm', 'ds10111040df_asm', 'slope', 'aspect', 'elevation'];
// Generate an empty Image Collection to later add each of the 10 classification results
var classification = ee.ImageCollection([]);
// Generate an empty list to later add each of the overall accuracies to be averaged
var overallAccuracy = ee.List([]);
// Build a for loop to run a Random Forest classifier using one set of the training and testing data each time
for (var i=0; i<n; i++){
// Select the subsequent set of training data each time
var valueTR=ee.Dictionary(assetListTR.get(i));
var imgIDTR=ee.String(valueTR.get('id')).getInfo();
var train=ee.FeatureCollection(imgIDTR);
// print('train' + (i+1), train);
// trainingTR=training.merge(train)
// Overlay the points on the imagery to get training.
var training = objectPropertiesImage.sampleRegions({
collection: train,
properties: ['Classes'],
scale: 2.5
});
// Train a CART classifier with default parameters
var classifier = ee.Classifier.smileRandomForest(100).train(training, 'Classes', bandss);
// Classify the image with the same bands used for training
var classified = objectPropertiesImage.classify(classifier);
// print('classified', classified);
// Map.addLayer(classified, {min: 1, max: 11, palette: palette}, 'Classification map' + (i+1), false);
// // Export the Image - uncomment if want to export each
// Export.image.toAsset({
// image: classified.toByte(),//.toFloat(),
// description: "Obj_wDEMnoGEO" + (i+1),
// assetId: "Obj_wDEMnoGEO" + (i+1),
// scale: 2.5,
// region: table,
// maxPixels:1e13,
// });
// Overall map
classification=classification.merge(classified);
// Validation
// Select the subsequent set of testing data each time
var valueTE=ee.Dictionary(assetListTE.get(i));
var imgIDTE=ee.String(valueTE.get('id')).getInfo();
var test=ee.FeatureCollection(imgIDTE);
// print('test', test);
// testing=testing.merge(test)
var validation = objectPropertiesImage.sampleRegions({
collection: test,
properties: ['Classes'],
scale: 2.5
});
var validated = validation.classify(classifier);
// Get a confusion matrix representing expected accuracy and an overall accuracy
// Uncomment the "print" commands if want to visualize the accuracies and matrices for each set of training and testing data in GEE console
var testAccuracy = validated.errorMatrix('Classes', 'classification');
var Matrix = ee.Feature(null, {matrix: testAccuracy.array()});
// print('Validation error matrix_wDEMnoGEO' + (i+1), testAccuracy);
var overall = testAccuracy.accuracy();
// print('Overall wDEMnoGEO' + (i+1), overall);
// // Export the matrix - uncomment if want to export each
// Export.table.toDrive({
// collection: ee.FeatureCollection(Matrix),
// description:'testAccuracy_Obj_wDEMnoGEO' + (i+1),
// folder: 'testAccuracy',
// fileFormat: 'csv'
// });
// Add subsequent overall accuracy with each loop, to initially empty "overallAccuracy" list
overallAccuracy = overallAccuracy.add(overall);
}
// Image collection of classification results from each set of training data
var classifiedX = classification;
// print('Classification maps collection', classifiedX);
// Best map representing the most common pixel value based on all sets of training data
var bestClassificationObj = classifiedX.mode();
// The following map may not load due to memory limits, but should succesfully export to Assets or Drive
Map.addLayer(bestClassificationObj.toByte(), {min: 1, max: 11, palette: palette}, "Best object-oriented classification with DEM no GEO", false);
// Export the best map to Drive
Export.image.toDrive({
image: bestClassificationObj.toByte(),
description: "Obj_wDEMnoGEO",
folder: "objectBasedClassifications",
scale: 2.5,
region: table,
maxPixels:1e13,
});
// Calculate the average overall accuracy
var overallAccuracyObj = ee.FeatureCollection(overallAccuracy.reduce(ee.Reducer.mean()));
var overallAccuracyObjExp = ee.FeatureCollection(ee.Feature(null,{prop: overallAccuracyObj}));
print('Overall accuracy wDEMnoGEO', overallAccuracyObjExp);
// Calculate the Standard Deviation based on overall accuracies from all training data
var standardDeviation = ee.FeatureCollection(overallAccuracy.reduce(ee.Reducer.stdDev()));
var standardDeviationExp = ee.FeatureCollection(ee.Feature(null,{prop: standardDeviation}));
print('SD wDEMnoGEO', standardDeviationExp);
// The "print" function may not always work due to the memory limits
// To make sure it works, we can reduce the number of training and testing datasets used
// For example, use just two datasets (2 instead of n in the beginning of the for loop)
// Otherwise, it should always be possible to export them to Drive
Export.table.toDrive({
collection: overallAccuracyObjExp,
description:'overallAccuracyObjwDEMnoGEO10111040',
folder: 'classification_accuracies',
fileFormat: 'csv'
});
Export.table.toDrive({
collection: standardDeviationExp,
description:'standardDeviationObjwDEMnoGEO10111040',
folder: 'classification_accuracies',
fileFormat: 'csv'
});
///////////// ///////////// ///////////// ///////////// ///////////// with DEM with GEO
// Concatenate all layers into one
var objectPropertiesImage = ee.Image.cat([
snic.select(['ds10111040da', 'ds10111040df']),
stdDev,
area,
perimeter,
width,
height,
shape,
entropyDA,
entropyDF,
homogeneityDA,
homogeneityDF,
angularSecondMomentDA,
angularSecondMomentDF,
elevation_10111040,
slope_10111040,
aspect_10111040
]).float();
// print (objectPropertiesImage);
var bandNames = objectPropertiesImage.bandNames();
print('objectPropertiesImage bandNames wDEMwGEO', bandNames);
var bandss = ['ds10111040da', 'ds10111040df', 'ds10111040da_StdDev', 'ds10111040df_StdDev', 'area', 'perimeter', 'width', 'height', 'shape', 'ds10111040da_ent', 'ds10111040df_ent', 'ds10111040da_idm', 'ds10111040df_idm', 'ds10111040da_asm', 'ds10111040df_asm', 'slope', 'aspect', 'elevation'];
// Generate an empty Image Collection to later add each of the 10 classification results
var classification = ee.ImageCollection([]);
// Generate an empty list to later add each of the overall accuracies to be averaged
var overallAccuracy = ee.List([]);
// Build a for loop to run a Random Forest classifier using one set of the training and testing data each time
for (var i=0; i<n; i++){
// Select the subsequent set of training data each time
var valueTR=ee.Dictionary(assetListTR.get(i));
var imgIDTR=ee.String(valueTR.get('id')).getInfo();
var train=ee.FeatureCollection(imgIDTR);
// print('train' + (i+1), train);
// trainingTR=training.merge(train)
// Overlay the points on the imagery to get training.
var training = objectPropertiesImage.sampleRegions({
collection: train,
properties: ['Classes'],
scale: 2.5
});
// Train a CART classifier with default parameters
var classifier = ee.Classifier.smileRandomForest(100).train(training, 'Classes', bandss);
// Classify the image with the same bands used for training
var classified = objectPropertiesImage.classify(classifier);
// print('classified', classified);
// Map.addLayer(classified, {min: 1, max: 11, palette: palette}, 'Classification map' + (i+1), false);
// // Export the Image - uncomment if want to export each
// Export.image.toAsset({
// image: classified.toByte(),//.toFloat(),
// description: "Obj_wDEMwGEO" + (i+1),
// assetId: "Obj_wDEMwGEO" + (i+1),
// scale: 2.5,
// region: table,
// maxPixels:1e13,
// });
// Overall map
classification=classification.merge(classified);
// Validation
// Select the subsequent set of testing data each time
var valueTE=ee.Dictionary(assetListTE.get(i));
var imgIDTE=ee.String(valueTE.get('id')).getInfo();
var test=ee.FeatureCollection(imgIDTE);
// print('test', test);
// testing=testing.merge(test)
var validation = objectPropertiesImage.sampleRegions({
collection: test,
properties: ['Classes'],
scale: 2.5
});
var validated = validation.classify(classifier);
// Get a confusion matrix representing expected accuracy and an overall accuracy
// Uncomment the "print" commands if want to visualize the accuracies and matrices for each set of training and testing data in GEE console
var testAccuracy = validated.errorMatrix('Classes', 'classification');
var Matrix = ee.Feature(null, {matrix: testAccuracy.array()});
// print('Validation error matrix_wDEMwGEO' + (i+1), testAccuracy);
var overall = testAccuracy.accuracy();
// print('Overall wDEMwGEO' + (i+1), overall);
// // Export the matrix - uncomment if want to export each
// Export.table.toDrive({
// collection: ee.FeatureCollection(Matrix),
// description:'testAccuracy_Obj_wDEMwGEO' + (i+1),
// folder: 'testAccuracy',
// fileFormat: 'csv'
// });
// Add subsequent overall accuracy with each loop, to initially empty "overallAccuracy" list
overallAccuracy = overallAccuracy.add(overall);
}
// Image collection of classification results from each set of training data
var classifiedX = classification;
// print('Classification maps collection', classifiedX);
// Best map representing the most common pixel value based on all sets of training data
var bestClassificationObj = classifiedX.mode();
// The following map may not load due to memory limits, but should succesfully export to Assets or Drive
Map.addLayer(bestClassificationObj.toByte(), {min: 1, max: 11, palette: palette}, "Best object-oriented classification with DEM with GEO", false);
// Export the best map to Drive
Export.image.toDrive({
image: bestClassificationObj.toByte(),
description: "Obj_wDEMwGEO",
folder: "objectBasedClassifications",
scale: 2.5,
region: table,
maxPixels:1e13,
});
// Calculate the average overall accuracy
var overallAccuracyObj = ee.FeatureCollection(overallAccuracy.reduce(ee.Reducer.mean()));
var overallAccuracyObjExp = ee.FeatureCollection(ee.Feature(null,{prop: overallAccuracyObj}));
print('Overall accuracy wDEMwGEO', overallAccuracyObjExp);
// Calculate the Standard Deviation based on overall accuracies from all training data
var standardDeviation = ee.FeatureCollection(overallAccuracy.reduce(ee.Reducer.stdDev()));
var standardDeviationExp = ee.FeatureCollection(ee.Feature(null,{prop: standardDeviation}));
print('SD wDEMwGEO', standardDeviationExp);
// The "print" function may not always work due to the memory limits
// To make sure it works, we can reduce the number of training and testing datasets used
// For example, use just two datasets (2 instead of n in the beginning of the for loop)
// Otherwise, it should always be possible to export them to Drive
Export.table.toDrive({
collection: overallAccuracyObjExp,
description:'overallAccuracyObjwDEMwGEO10111040',
folder: 'classification_accuracies',
fileFormat: 'csv'
});
Export.table.toDrive({
collection: standardDeviationExp,
description:'standardDeviationObjwDEMwGEO10111040',
folder: 'classification_accuracies',
fileFormat: 'csv'
});