如何计算 Google Earth Engine 中 class 化图像 (Landsat) 中每个 class 的像素总和?
How to count sum of pixels in each class in a classified image (Landsat) in 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) ;
以及它抛出的错误:
Dictionary.get: 词典不包含关键字:组。
有没有其他工具可以计算每个类中的像素数?
你缺少的是一个聚合乐队。 Earth Engine 知道您想要使用 'classification' 对结果进行分组,但是找不到任何其他要计算的波段(或以某种方式求和或减少)。这是一个选项:
var pixelChart = ui.Chart.image.byClass({
image: ee.Image(1).addBands(classified),
classBand: 'classification',
region: region,
scale: 30,
reducer: ee.Reducer.count()
}).setOptions(options);
计算一个常量为 1 的图像中的像素数。也许更好的选择是对面积求和(以平方米为单位):
var areaChart = ui.Chart.image.byClass({
image: ee.Image.pixelArea().addBands(classified),
classBand: 'classification',
region: region,
scale: 30,
reducer: ee.Reducer.sum()
});
另见 this tutorial. By the way, always specify scale。
我正在写一篇关于冰川变化的论文。 我对 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) ;
以及它抛出的错误:
Dictionary.get: 词典不包含关键字:组。
有没有其他工具可以计算每个类中的像素数?
你缺少的是一个聚合乐队。 Earth Engine 知道您想要使用 'classification' 对结果进行分组,但是找不到任何其他要计算的波段(或以某种方式求和或减少)。这是一个选项:
var pixelChart = ui.Chart.image.byClass({
image: ee.Image(1).addBands(classified),
classBand: 'classification',
region: region,
scale: 30,
reducer: ee.Reducer.count()
}).setOptions(options);
计算一个常量为 1 的图像中的像素数。也许更好的选择是对面积求和(以平方米为单位):
var areaChart = ui.Chart.image.byClass({
image: ee.Image.pixelArea().addBands(classified),
classBand: 'classification',
region: region,
scale: 30,
reducer: ee.Reducer.sum()
});
另见 this tutorial. By the way, always specify scale。