6、Classification
6.1 监督分类
分类步骤:1. Get a scene.
2. Collect training data.
3. Select and train a classifier using the training data.
4. Classify the image using the selected classifie(分类器).
.filterBounds() 是 Earth Engine 中用于过滤图像或特征集合的方法之一。它允许你根据空间范围来筛选数据,保留落在指定区域内的图像或特征。
// Create an Earth Engine Point object over Milan.
var pt = ee.Geometry.Point([9.453, 45.424]);
// Filter the Landsat 8 collection and select the least cloudy image.
var landsat = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
.filterBounds(pt)
.filterDate('2019-01-01', '2020-01-01')
.sort('CLOUD_COVER')
.first();
// Center the map on that image.
Map.centerObject(landsat, 8);
// Add Landsat image to the map.
var visParams = {
bands: ['SR_B4', 'SR_B3', 'SR_B2'],
min: 7000,
max: 12000 };
Map.addLayer(landsat, visParams, 'Landsat 8 image');
在整个数字世界中,使用十六进制值来表示计算机和操作系统中的特定颜色。它们由六个值指定,这些值排列成三对,每对值分别用于红色、绿色和蓝色亮度值。如果您不熟悉十六进制值,请想象一下颜色是以 10 为基数对而不是以 16 为基数对指定的。在这种情况下,亮红色的纯红色值将为 “990000”;亮绿色值为 “009900”;亮蓝色值为 “000099”。像 “501263” 这样的值是三种颜色的混合,不是特别亮,蓝色和红色的数量大致相等,绿色的数量要少得多
下一步是将所有训练特征集合合并为一个。复制并粘贴下面的代码,将它们合并到一个名为 trainingFeatures 的 FeatureCollection 中。在这里,我们使用 flatten 方法来避免拥有特征集合 - 我们希望在 FeatureCollection 中拥有单个特征。
// Combine training feature collections.
var trainingFeatures = ee.FeatureCollection([
forest, developed, water, herbaceous ]).flatten();
下面的代码以提取每个点位置每个类的波段信息。首先,我们定义预测波段,以从每个类别的不同波段中提取不同的光谱和热信息。然后,我们使用 sampleRegions 方法从每个点位置的 Landsat 图像中采样信息。此方法需要有关 FeatureCollection (我们的参考点)、要提取的属性 (“class”) 和像素比例 (以米为单位) 的信息。
// Define prediction bands.
var predictionBands = [ 'SR_B1', 'SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7','ST_B10' ];
// Sample training points.
var classifierTraining = landsat.select(predictionBands)
.sampleRegions({ collection: trainingFeatures,
properties: ['class'],
scale: 30 });
现在我们可以选择一个分类器。分类器的选择并不总是显而易见的,并且有许多选项可供选择 - 您可以快速扩展 ee.Classifier 对象来了解我们有多少个图像分类选项。因此,我们将测试不同的分类器并比较它们的结果。我们将从分类与回归树 (CART) 分类器开始,这是一种已经存在了几十年的著名分类算法 。
CART Classifier ///
// Train a CART Classifier.
var classifier = ee.Classifier.smileCart().train({ features: classifierTraining, classProperty: 'class', inputProperties: predictionBands });
对 Landsat 图像进行分类并将其添加到 Map。
// Classify the Landsat image.
var classified = landsat.select(predictionBands).classify(classifier);
// Define classification image visualization parameters.
var classificationVis = {
min: 0,
max: 3,
palette: ['589400', 'ff0000', '1a11ff', 'd0741e'] };
// Add the classified image to the map.
Map.addLayer(classified, classificationVis, 'CART classified');
最后图并不理想,可以从一下进行改进:
• Collect more training data
• Tune the mode
• Expand the collection location
• Add more predictors: We can try adding spectral indices to the input variables; this way, we are feeding the classifier new, unique information about each class. For example, there is a good chance that a vegetation index specialized for detecting vegetation health (e.g., NDVI) would improve the developed versus herbaceous classification.
现在,我们将尝试另一个广泛使用的监督学习分类器:随机森林 (RF)。
请注意,在 ee.Classifier.smileRandomForest 文档(文档选项卡)中,有一个种子(随机数)参数。设置种子允许您在每次运行模型时精确复制模型。任何数字都可以作为种子。
/// Random Forest Classifier /
// Train RF classifier.
var RFclassifier = ee.Classifier.smileRandomForest(50).train({ features: classifierTraining, classProperty: 'class', inputProperties: predictionBands });
// Classify Landsat image.
var RFclassified = landsat.select(predictionBands).classify( RFclassifier);
// Add classified image to the map.
Map.addLayer(RFclassified, classificationVis, 'RF classified');