admin管理员组文章数量:1410724
I'm making a dissertation about glacier changes. I did a supervised classification on a Landsat 8 image, and I would like to count how many pixels are there in each classes. I want to make a chart by the way.
But I stucked, my code runs into error. I tried to use ui.Chart.image.byClass() method with the specified parameters, but is does not works.
My code:
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) ;
And the error what it throws:
Dictionary.get: Dictionary does not contain key: groups.
Is there any other tools to count the number of pixels in each classes?
I'm making a dissertation about glacier changes. I did a supervised classification on a Landsat 8 image, and I would like to count how many pixels are there in each classes. I want to make a chart by the way.
But I stucked, my code runs into error. I tried to use ui.Chart.image.byClass() method with the specified parameters, but is does not works.
My code:
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) ;
And the error what it throws:
Dictionary.get: Dictionary does not contain key: groups.
Is there any other tools to count the number of pixels in each classes?
Share Improve this question asked Jan 10, 2018 at 18:00 jaraipalijaraipali 412 silver badges9 bronze badges1 Answer
Reset to default 5What you're missing is a band to aggregate. Earth Engine knows you want to use 'classification' to group the results, but then can't find any other band to count (or sum or reduce in some way). Here's one option:
var pixelChart = ui.Chart.image.byClass({
image: ee.Image(1).addBands(classified),
classBand: 'classification',
region: region,
scale: 30,
reducer: ee.Reducer.count()
}).setOptions(options);
That counts the number of pixels in a constant image of 1's. Perhaps a better option is to sum area (in square meters):
var areaChart = ui.Chart.image.byClass({
image: ee.Image.pixelArea().addBands(classified),
classBand: 'classification',
region: region,
scale: 30,
reducer: ee.Reducer.sum()
});
See also this tutorial. By the way, always specify scale.
本文标签:
版权声明:本文标题:javascript - How to count sum of pixels in each class in a classified image (Landsat) in Google Earth Engine? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744805200a2626104.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论