物件型方法

圖片物件是一組具有相同整數值的相連像素。類別、分箱和布林值圖片資料適合用於物件分析。

Earth Engine 提供多種方法,可為每個物件加上專屬 ID、計算組成物件的像素數量,以及計算與物件交集的像素值統計資料。

熱點

以下各節提供將物件式方法套用至 Landsat 8 地表溫度的範例,各節內容以前一節為基礎。執行下一個程式碼片段,生成基本圖片:舊金山一小塊區域的熱點 (> 303 K)。

程式碼編輯器 (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');

Python 設定

如要瞭解 Python API 和使用 geemap 進行互動式開發,請參閱「Python 環境」頁面。

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 值。結果會是輸入圖片的副本,並附加「labels」波段,根據圖片第一個波段中像素的連通性,將像素與物件 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');

Python 設定

如要瞭解 Python API 和使用 geemap 進行互動式開發,請參閱「Python 環境」頁面。

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');

Python 設定

如要瞭解 Python API 和使用 geemap 進行互動式開發,請參閱「Python 環境」頁面。

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');

Python 設定

如要瞭解 Python API 和使用 geemap 進行互動式開發,請參閱「Python 環境」頁面。

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 圖片做為遮罩,移除面積小於一公頃的物件。

程式碼編輯器 (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');

Python 設定

如要瞭解 Python API 和使用 geemap 進行互動式開發,請參閱「Python 環境」頁面。

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 圖片的副本,其中小於一公頃的物件會遮蓋。

圖 4a. 熱點物件,依專屬 ID 加上標籤和樣式。 圖 4b. 依最小面積 (1 公頃) 篩選的熱點物件。

分區統計資料

reduceConnectedComponents() 方法會將歸納函式套用至組成不重複物件的像素。下列程式碼片段使用該函式計算熱點物件的平均溫度。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'
);

Python 設定

如要瞭解 Python API 和使用 geemap 進行互動式開發,請參閱「Python 環境」頁面。

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. 熱點物件像素,依平均溫度匯總並設定樣式。