jar*_*ali 2 javascript classification google-earth google-earth-engine
我正在写一篇关于冰川变化的论文。我对 Landsat 8 图像进行了监督分类,我想计算每个类别中有多少像素。顺便我想做一个图表。
但我卡住了,我的代码出错了。我尝试使用带有指定参数的 ui.Chart.image.byClass() 方法,但不起作用。
我的代码:
var img = ee.Image('LANDSAT/LC8_L1T_TOA/LC81940282016238LGN00') ;
// Add pseudocolor image
Map.addLayer(img, {bands: ['B6', 'B5', 'B4'] }, 'Pseudocolor image' ) ;
// Training points for classification - Point geometries
var points = [class1,class2,class3, class4, class5] ;
var trainingPoints = ee.FeatureCollection(points) ;
var training = img.sampleRegions(trainingPoints, ['class'] ,30) ;
var trained = ee.Classifier.minimumDistance().train(training, 'class' ) ;
var classified = img.classify(trained) ;
var palette = ['red','red', '#696969' , '#90EE90' , '#008000' ] ;
Map.addLayer(classified, {min: 0 , max : 5 , palette : palette }, 'L8
classified' ) ;
print(classified);
var options = {
lineWidth: 1,
pointSize: 2,
hAxis: {title: 'Classes'},
vAxis: {title: 'Num of pixels'},
title: 'Number of pixels in each class.'
};
var chart = ui.Chart.image.byClass({
image : classified ,
classBand : 'classification',
region : aletsch //<-- A previousy defined line type geometry
}).setOptions(options) ;
Run Code Online (Sandbox Code Playgroud)
以及它抛出的错误:
Dictionary.get:字典不包含 key: 组。
有没有其他工具可以计算每个类别中的像素数?
你缺少的是一个可以聚合的乐队。Earth Engine 知道您想要使用“分类”对结果进行分组,但随后无法找到任何其他波段进行计数(或以某种方式求和或减少)。这是一种选择:
var pixelChart = ui.Chart.image.byClass({
image: ee.Image(1).addBands(classified),
classBand: 'classification',
region: region,
scale: 30,
reducer: ee.Reducer.count()
}).setOptions(options);
Run Code Online (Sandbox Code Playgroud)
它计算的是 1 的恒定图像中的像素数。也许更好的选择是求面积总和(以平方米为单位):
var areaChart = ui.Chart.image.byClass({
image: ee.Image.pixelArea().addBands(classified),
classBand: 'classification',
region: region,
scale: 30,
reducer: ee.Reducer.sum()
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6427 次 |
| 最近记录: |