学遥感,分类是是常事,今天带来代码版的非监督分类。
var roi = ee.Geometry.Polygon([[[114.88544904095511,25.804448767604917],
[115.09830914837698,25.804448767604917],
[115.09830914837698,25.932959664484418],
[114.88544904095511,25.932959664484418],
[114.88544904095511,25.804448767604917]]]);
Map.centerObject(roi,7);
Map.setOptions("SATELLITE");
Map.addLayer(roi,{color:"00ff00"},"roi",false);
var l8Col = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
.filterDate("2018-4-1","2018-10-1")
.filterBounds(roi)
.filter(ee.Filter.lte("CLOUD_COVER",50))
.map(rmCloud)
.map(scaleImage)
.map(NDVI);
print(l8Col);
var l8Image = l8Col.select(['B1','B2','B3','B4','B5','B6','B7','NDVI']).median().clip(roi);
var visParam = {
min:0,
max:0.3,
bands:["B4","B3","B2"]
};
Map.addLayer(l8Image,visParam,"l8Image");
//Landsat 8 remove cloud
function rmCloud(image){
var cloudShadowBitMask = (1<<3);
var cloudBitMask = (1<<5);
var qa = image.select("pixel_qa");
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0).and (qa.bitwiseAnd(cloudBitMask).eq(0));
return image.updateMask(mask);
}
//Landsat 8 scale
function scaleImage(image){
var time_start = image.get("system:time_start");
image = image.multiply(0.0001);
image = image.set("system:time_start",time_start);
return image;
}
//add NDVI Bands
function NDVI(image){
return image.addBands(image.normalizedDifference(["B5",'B4']).rename("NDVI"));
}
// training data
var sampleRoi = ee.Geometry.Polygon([[[114.9393895293283,25.875400747813824],
[114.94239360342497,25.875400747813824],
[114.94239360342497,25.87783333397478],
[114.9393895293283,25.87783333397478],
[114.9393895293283,25.875400747813824]]]);
var training = l8Image.sample({
region:roi,
scale:30,
numPixels:5000
});
// train previous training
var count = 10;
var clusterer = ee.Clusterer.wekaKMeans(count).train(training);
//classify
var result = l8Image.cluster(clusterer);
print("result",result);
//palette:color list,names:legend list
function addLegend(palette,names){
//the bottom of legend
var legend = ui.Panel({
style:{
position:'bottom-right',
padding:'5px 10px'
}
});
// the title of legend
var title = ui.Label({
value:'类别',
style:{
fontWeight:'bold',
color:"red",
fontSize:'16px'
}
});
legend.add(title);
//Add each column legend color and its description
var addLegendLabel = function(color,name){
var showColor = ui.Label({
style:{
backgroundColor:'#'+color,
padding:'8px',
margin:'0 0 4px 0'
}
});
var desc = ui.Label({
value:name,
style:{
margin:'0 0 4px 8px'
}
});
//set:Colors and descriptions are placed horizontally
return ui.Panel({
widgets:[showColor,desc],
layout:ui.Panel.Layout.Flow('horizontal')
});
};
//Add all legends to the list
for (var i = 0;i<palette.length;i++){
var label = addLegendLabel(palette[i],names[i]);
legend.add(label);
}
ui.root.insert(0,legend);
}
//color list and name list
var palette = ['ff0000','00ff00','0000ff','ff00ff',
'ffff00','00ffff','ffffff','000000',
'FF8C00','ADFF2F'];
var names = ['分类A','分类B','分类C','分类D','分类E',
'分类F','分类G','分类H','分类I','分类J'];
// add legend
addLegend(palette,names);
var visParam = {
min:0,
max:count-1,
palette:palette
};
Map.addLayer(result,visParam,"result");
运行结果如下:
大家可以把上面的代码作为监督分类的一种训练和学习,不妨试试看看!
更多的内容,欢迎大家关注小编的公众号“梧桐GIS”,谢谢大家!