-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_based_analysis
More file actions
205 lines (181 loc) · 6.8 KB
/
object_based_analysis
File metadata and controls
205 lines (181 loc) · 6.8 KB
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
// 1.1 Unsupervised k-Means classification
// This function does unsupervised clustering classification
// input = any image. All bands will be used for clustering.
// numberOfUnsupervisedClusters = tunable parameter for how
// many clusters to create.
var afn_Kmeans = function(input,
numberOfUnsupervisedClusters,
defaultStudyArea, nativeScaleOfImage) {
// Make a new sample set on the input. Here the sample
// randomly selected spatially.
var training = input.sample({
region: defaultStudyArea,
scale: nativeScaleOfImage,
numPixels: 1000
});
var cluster = ee.Clusterer.wekaKMeans(
numberOfUnsupervisedClusters)
.train(training);
// Now apply that clusterer to the raw image that was also passed in. set is
var toexport = input.cluster(cluster);
// The first item is the unsupervised classification. Name the band.
var clusterUnsup = toexport.select(0).rename(
'unsupervisedClass');
return (clusterUnsup);
};
// 1.2 Simple normalization by maxes function.
var afn_normalize_by_maxes = function(img, bandMaxes) {
return img.divide(bandMaxes);
};
// 1.4 Simple add mean to Band Name function
var afn_addMeanToBandName = (function(i) {
return i + '_mean'; });
////////////////////////////////////////////////////////////
// 2. Parameters to function calls
////////////////////////////////////////////////////////////
// 2.1. Unsupervised KMeans Classification Parameters
var numberOfUnsupervisedClusters = 4;
////////////////////////////////////////////////////////////
// 2.2. Visualization and Saving parameters
// For different images, you might want to change the min and max
// values to stretch. Useful for images 2 and 3, the normalized images.
var centerObjectYN = true;
/////3.1 selecting image to classify
var whichImage = 1;
if (whichImage == 1){
var whichCollection = 'COPERNICUS/S2'
var ImageToUseID= '20160421T191704_20160421T212107_T10TDT';
var originalImage = ee.Image(whichCollection + '/' +ImageToUseID);
print(ImageToUseID, originalImage);
var nativeScaleOfImage = 10;
var threeBandsToDraw = ['B4', 'B3', 'B2'];
var bandsToUse = ['B4', 'B3', 'B2'];
var bandMaxes = [1e4, 1e4, 1e4];
var drawMin = 0
var drawMax = 0.3
var defaultStudyArea = ee.Geometry.Polygon(
[
[
[-123.13105468749993, 47.612974066532004],
[-123.13105468749993, 47.56214700543596],
[-123.00179367065422, 47.56214700543596],
[-123.00179367065422, 47.612974066532004]
]
]);
var zoomArea = ee.Geometry.Polygon(
[
[
[-123.13105468749993, 47.612974066532004],
[-123.13105468749993, 47.56214700543596],
[-123.00179367065422, 47.56214700543596],
[-123.00179367065422, 47.612974066532004]
]
], null,false);
}
Map.addLayer(originalImage.select(threeBandsToDraw), {
min: 0,
max: 2000
}, '3.1 '+ ImageToUseID, true, 1);
////////////////////////////////////////////////////////////
// 4. Image Preprocessing
////////////////////////////////////////////////////////////
var clippedImageSelectedBands =
originalImage.clip(defaultStudyArea)
.select(bandsToUse);
var ImageToUse =
afn_normalize_by_maxes(clippedImageSelectedBands,
bandMaxes);
Map.addLayer(ImageToUse.select(threeBandsToDraw), {
min: 0.028,
max: 0.12
},
'4.3 Pre-normalized image', true, 0);
////////////////////////////////////////////////////////////
// 6. Execute Classifications
////////////////////////////////////////////////////////////
// 6.1 Per Pixel Unsupervised Classification for Comparison
var PerPixelUnsupervised = afn_Kmeans(ImageToUse,
numberOfUnsupervisedClusters, defaultStudyArea,
nativeScaleOfImage);
Map.addLayer(PerPixelUnsupervised.select('unsupervisedClass'
)
.randomVisualizer(), {}, '6.1 Per-Pixel Unsupervised',
true, 0
);
print('6.1b Per-Pixel Unsupervised Results:',PerPixelUnsupervised);
////////////////////////////////////////////////////////////
// 7. Zoom if requested
////////////////////////////////////////////////////////////
if (centerObjectYN === true) {
Map.centerObject(zoomArea, 14); }
///////////////////////////////////
//Detecting Objects in Imagery with the SNIC
//Algorithm
////////////////////////////////
// 1.3 Seed Creation and SNIC segmentation Function
var afn_SNIC = function(imageOriginal, SuperPixelSize,
Compactness,
Connectivity, NeighborhoodSize, SeedShape) {
var theSeeds =
ee.Algorithms.Image.Segmentation.seedGrid(
SuperPixelSize, SeedShape);
var snic2 = ee.Algorithms.Image.Segmentation.SNIC({
image: imageOriginal,
size: SuperPixelSize,
compactness: Compactness,
connectivity: Connectivity,
neighborhoodSize: NeighborhoodSize,
seeds: theSeeds
});
var theStack = snic2.addBands(theSeeds);
return (theStack);};
// 2.3 Object-growing parameters to change
// Adjustable Superpixel Seed and SNIC segmentation Parameters:
// The superpixel seed location spacing, in pixels.
var SNIC_SuperPixelSize = 16;
// Larger values cause clusters to be more compact (square/hexagonal).
// Setting this to 0 disables spatial distance weighting.
var SNIC_Compactness = 0;
// Connectivity. Either 4 or 8.
var SNIC_Connectivity = 4;
// Either 'square' or 'hex'.
var SNIC_SeedShape = 'square';
// 2.4 Parameters that can stay unchanged
// Tile neighborhood size (to avoid tile boundary artifacts). Defaults to 2 * size.
var SNIC_NeighborhoodSize = 2 *SNIC_SuperPixelSize;
////////////////////////////////////////////////////////////
// 5. SNIC Clustering
////////////////////////////////////////////////////////////
// This function returns a multi-banded image that has had SNIC
// applied to it. It automatically determine the new names
// of the bands that will be returned from the segmentation.
print('5.1 Execute SNIC');
var SNIC_MultiBandedResults = afn_SNIC(
ImageToUse,
SNIC_SuperPixelSize,
SNIC_Compactness,
SNIC_Connectivity,
SNIC_NeighborhoodSize,
SNIC_SeedShape
);
var SNIC_MultiBandedResults = SNIC_MultiBandedResults
.reproject('EPSG:3857', null, nativeScaleOfImage);
print('5.2 SNIC Multi-Banded Results',
SNIC_MultiBandedResults);
Map.addLayer(SNIC_MultiBandedResults.select('clusters')
.randomVisualizer(), {}, '5.3 SNIC Segment Clusters',
true, 1);
var theSeeds = SNIC_MultiBandedResults.select('seeds');
Map.addLayer(theSeeds, {
palette: 'red'
}, '5.4 Seed points of clusters', true, 1);
var bandMeansToDraw =
threeBandsToDraw.map(afn_addMeanToBandName);
print('5.5 band means to draw', bandMeansToDraw);
var clusterMeans =
SNIC_MultiBandedResults.select(bandMeansToDraw);
print('5.6 Cluster Means by Band', clusterMeans);
Map.addLayer(clusterMeans, {
min: drawMin,
max: drawMax
}, '5.7 Image repainted by segments', true, 0);