6.2 非监督分类
在 Earth Engine 中,非监督分类器是 ee.Clusterer 对象。它们是 “自学” 算法,不使用一组标记的训练数据(即,它们是 “无监督的”)。
分类流程如下:
1. 组装具有数值属性的特征,以在其中查找聚类 (训练数据)。
2. 选择并实例化一个集群程序。
3. 使用训练数据训练聚类器。
4. 将聚类器应用于场景 (分类)。
5. 标记集群。
为了生成训练数据,我们将使用 sample 方法,该方法从某个区域随机获取样本(与 sampleRegions 不同,后者从预定义位置获取样本)。我们将通过调用 geometry 方法使用图像的足迹作为区域。此外,我们将定义要采样的像素数 (numPixels)(在本例中为 1000 像素),并将 tileScale 定义为 8,以避免由于区域大小而导致的计算错误。
// 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');
Unsupervised classification
// Make the training dataset.
var training = landsat.sample({
region: landsat.geometry(),
scale: 30,
numPixels: 1000,
tileScale: 8 });
使用 k-means 聚类算法,这是遥感中常用的方法。此算法使用迭代重新分组策略来识别光谱空间(图像 x 波段)中彼此靠近的像素组。
// Instantiate the clusterer and train it.
var clusterer = ee.Clusterer.wekaKMeans(4).train(training);
randomVisualizer:可视化分配颜色
// Cluster the input using the trained clusterer.
var Kclassified = landsat.cluster(clusterer);
// Display the clusters with random colors.
Map.addLayer(Kclassified.randomVisualizer(),
{},
'K-means classified - random colors');