图片对象是具有相同整数值的一组连接像素。分类、分箱和布尔值图片数据适合进行对象分析。
Earth Engine 提供了多种方法,可为每个对象添加唯一 ID 标签、统计构成对象的像素数量,以及计算与对象相交的像素值的统计信息。
connectedComponents():使用唯一标识符标记每个对象。connectedPixelCount():计算每个对象中的像素数。reduceConnectedComponents():计算每个对象中像素的统计信息。
热点
以下各部分提供了应用于 Landsat 8 地表温度的基于对象的方法的示例,每个部分都以前一部分为基础。 运行下一个代码段以生成基础图片:旧金山一小片区域的热点(> 303 开氏度)。
代码编辑器 (JavaScript)
// Make an area of interest geometry centered on San Francisco. var point = ee.Geometry.Point(-122.1899, 37.5010); var aoi = point.buffer(10000); // Import a Landsat 8 image, subset the thermal band, and clip to the // area of interest. var kelvin = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318') .select(['B10'], ['kelvin']) .clip(aoi); // Display the thermal band. Map.centerObject(point, 13); Map.addLayer(kelvin, {min: 288, max: 305}, 'Kelvin'); // Threshold the thermal band to set hot pixels as value 1, mask all else. var hotspots = kelvin.gt(303) .selfMask() .rename('hotspots'); // Display the thermal hotspots on the Map. Map.addLayer(hotspots, {palette: 'FF0000'}, 'Hotspots');
import ee import geemap.core as geemap
Colab (Python)
# Make an area of interest geometry centered on San Francisco. point = ee.Geometry.Point(-122.1899, 37.5010) aoi = point.buffer(10000) # Import a Landsat 8 image, subset the thermal band, and clip to the # area of interest. kelvin = ( ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318') .select(['B10'], ['kelvin']) .clip(aoi) ) # Threshold the thermal band to set hot pixels as value 1, mask all else. hotspots = kelvin.gt(303).selfMask().rename('hotspots') # Define a map centered on Redwood City, California. map_objects = geemap.Map(center=[37.5010, -122.1899], zoom=13) # Add the image layers to the map. map_objects.add_layer(kelvin, {'min': 288, 'max': 305}, 'Kelvin') map_objects.add_layer(hotspots, {'palette': 'FF0000'}, 'Hotspots')

图 1. 旧金山某个区域的温度。温度高于 303 开氏度的像素通过红色(热点)进行区分。
标签对象
为对象添加标签通常是对象分析的第一步。在这里,connectedComponents() 函数用于识别图像对象并为每个对象分配一个唯一 ID;属于某个对象的所有像素都会被分配相同的整数 ID 值。结果是输入图片的副本,其中包含一个额外的“标签”波段,该波段根据图片第一个波段中像素的连通性,将像素与对象 ID 值相关联。
代码编辑器 (JavaScript)
// Uniquely label the hotspot image objects. var objectId = hotspots.connectedComponents({ connectedness: ee.Kernel.plus(1), maxSize: 128 }); // Display the uniquely ID'ed objects to the Map. Map.addLayer(objectId.randomVisualizer(), null, 'Objects');
import ee import geemap.core as geemap
Colab (Python)
# Uniquely label the hotspot image objects. object_id = hotspots.connectedComponents( connectedness=ee.Kernel.plus(1), maxSize=128 ) # Add the uniquely ID'ed objects to the map. map_objects.add_layer(object_id.randomVisualizer(), None, 'Objects')
请注意,最大补丁大小设置为 128 像素;由更多像素组成的对象会被遮盖。连通性由 ee.Kernel.plus(1) 内核指定,该内核定义了四邻域连通性;使用 ee.Kernel.square(1) 可实现八邻域连通性。

图 2. 热点对象,按唯一 ID 标记和设置样式。
对象大小
像素数量
使用 connectedPixelCount() 图片方法计算构成对象的像素数量。了解对象中的像素数量有助于按大小遮盖对象并计算对象面积。以下代码段将 connectedPixelCount() 应用于上一部分中定义的 objectId 图片的“labels”波段。
代码编辑器 (JavaScript)
// Compute the number of pixels in each object defined by the "labels" band. var objectSize = objectId.select('labels') .connectedPixelCount({ maxSize: 128, eightConnected: false }); // Display object pixel count to the Map. Map.addLayer(objectSize, null, 'Object n pixels');
import ee import geemap.core as geemap
Colab (Python)
# Compute the number of pixels in each object defined by the "labels" band. object_size = object_id.select('labels').connectedPixelCount( maxSize=128, eightConnected=False ) # Add the object pixel count to the map. map_objects.add_layer(object_size, None, 'Object n pixels')
connectedPixelCount() 会返回输入图片的副本,其中每个波段的每个像素都包含连接的邻域数量,具体取决于传递给 eightConnected 参数的布尔实参所确定的四邻域或八邻域连接规则。请注意,连通性是针对输入图像的每个波段独立确定的。在此示例中,输入的是表示对象 ID 的单波段图像 (objectId),因此返回的也是单波段图像,其中包含“labels”波段(与输入图像中的波段相同),但现在这些值表示构成对象的像素数量;每个对象的每个像素都将具有相同的像素数量值。

图 3. 热点对象,按大小添加标签和样式。
领域
通过将单个像素的面积乘以构成对象的像素数(由 connectedPixelCount() 确定)来计算对象面积。像素面积由 ee.Image.pixelArea() 生成的图片提供。
代码编辑器 (JavaScript)
// Get a pixel area image. var pixelArea = ee.Image.pixelArea(); // Multiply pixel area by the number of pixels in an object to calculate // the object area. The result is an image where each pixel // of an object relates the area of the object in m^2. var objectArea = objectSize.multiply(pixelArea); // Display object area to the Map. Map.addLayer(objectArea, {min: 0, max: 30000, palette: ['0000FF', 'FF00FF']}, 'Object area m^2');
import ee import geemap.core as geemap
Colab (Python)
# Get a pixel area image. pixel_area = ee.Image.pixelArea() # Multiply pixel area by the number of pixels in an object to calculate # the object area. The result is an image where each pixel # of an object relates the area of the object in m^2. object_area = object_size.multiply(pixel_area) # Add the object area to the map. map_objects.add_layer( object_area, {'min': 0, 'max': 30000, 'palette': ['0000FF', 'FF00FF']}, 'Object area m^2', )
结果是一张图片,其中对象的每个像素都与对象的面积(以平方米为单位)相关。在此示例中,objectSize 图片包含一个波段,如果是多波段,则乘法运算将应用于图片的每个波段。
按大小过滤对象
对象大小可用作遮罩条件,以便将分析重点放在特定大小的对象上(例如,遮盖过小的对象)。在这里,上一步中计算的 objectArea 图像用作遮罩,以移除面积小于 1 公顷的对象。
代码编辑器 (JavaScript)
// Threshold the `objectArea` image to define a mask that will mask out // objects below a given size (1 hectare in this case). var areaMask = objectArea.gte(10000); // Update the mask of the `objectId` layer defined previously using the // minimum area mask just defined. objectId = objectId.updateMask(areaMask); Map.addLayer(objectId, null, 'Large hotspots');
import ee import geemap.core as geemap
Colab (Python)
# Threshold the `object_area` image to define a mask that will mask out # objects below a given size (1 hectare in this case). area_mask = object_area.gte(10000) # Update the mask of the `object_id` layer defined previously using the # minimum area mask just defined. object_id = object_id.updateMask(area_mask) map_objects.add_layer(object_id, None, 'Large hotspots')
结果是 objectId 图像的副本,其中小于 1 公顷的对象会被遮盖。
![]() |
![]() |
|---|---|
| 图 4a. 热点对象,按唯一 ID 标记和设置样式。 | 图 4b. 按最小面积(1 公顷)过滤的热点对象。 |
可用区级统计信息
reduceConnectedComponents() 方法会将一个 reducer 应用于构成唯一对象的像素。以下代码段使用它来计算热点对象的平均温度。reduceConnectedComponents() 需要一个输入图片,其中包含要缩减的一个或多个波段,以及一个定义对象标签的波段。在这里,objectID“labels”图像波段被添加到 kelvin 温度图像中,以构建合适的输入图像。
代码编辑器 (JavaScript)
// Make a suitable image for `reduceConnectedComponents()` by adding a label // band to the `kelvin` temperature image. kelvin = kelvin.addBands(objectId.select('labels')); // Calculate the mean temperature per object defined by the previously added // "labels" band. var patchTemp = kelvin.reduceConnectedComponents({ reducer: ee.Reducer.mean(), labelBand: 'labels' }); // Display object mean temperature to the Map. Map.addLayer( patchTemp, {min: 303, max: 304, palette: ['yellow', 'red']}, 'Mean temperature' );
import ee import geemap.core as geemap
Colab (Python)
# Make a suitable image for `reduceConnectedComponents()` by adding a label # band to the `kelvin` temperature image. kelvin = kelvin.addBands(object_id.select('labels')) # Calculate the mean temperature per object defined by the previously added # "labels" band. patch_temp = kelvin.reduceConnectedComponents( reducer=ee.Reducer.mean(), labelBand='labels' ) # Add object mean temperature to the map and display it. map_objects.add_layer( patch_temp, {'min': 303, 'max': 304, 'palette': ['yellow', 'red']}, 'Mean temperature', ) display(map_objects)
结果是输入图片的副本,其中不包含用于定义对象的波段,像素值表示每个对象、每个波段的缩减结果。

图 5. 热点对象像素按平均温度汇总和设置样式。
在 Google Colab 中运行
在 GitHub 上查看源代码

