고유 분석

주요 구성요소 (PC) 변환 (카르후넨-로에베 변환이라고도 함)은 스펙트럼 상 상관이 있는 이미지 데이터를 가져와 상관이 없는 데이터를 출력하는 스펙트럼 회전입니다. PC 변환은 고유 분석을 통해 입력 밴드 상관 행렬을 대각화하여 이를 실행합니다. Earth Engine에서 이를 수행하려면 배열 이미지에 공분산 감소기를 사용하고 결과 공분산 배열에 eigen() 명령어를 사용합니다. 이 목적으로 다음 함수를 고려해 보세요. 애플리케이션의 예는 Code 편집기 스크립트Colab 노트북으로 확인할 수 있습니다.

코드 편집기 (JavaScript)

var getPrincipalComponents = function(centered, scale, region) {
  // Collapse the bands of the image into a 1D array per pixel.
  var arrays = centered.toArray();

  // Compute the covariance of the bands within the region.
  var covar = arrays.reduceRegion({
    reducer: ee.Reducer.centeredCovariance(),
    geometry: region,
    scale: scale,
    maxPixels: 1e9
  });

  // Get the 'array' covariance result and cast to an array.
  // This represents the band-to-band covariance within the region.
  var covarArray = ee.Array(covar.get('array'));

  // Perform an eigen analysis and slice apart the values and vectors.
  var eigens = covarArray.eigen();

  // This is a P-length vector of Eigenvalues.
  var eigenValues = eigens.slice(1, 0, 1);
  // This is a PxP matrix with eigenvectors in rows.
  var eigenVectors = eigens.slice(1, 1);

  // Convert the array image to 2D arrays for matrix computations.
  var arrayImage = arrays.toArray(1);

  // Left multiply the image array by the matrix of eigenvectors.
  var principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage);

  // Turn the square roots of the Eigenvalues into a P-band image.
  var sdImage = ee.Image(eigenValues.sqrt())
      .arrayProject([0]).arrayFlatten([getNewBandNames('sd')]);

  // Turn the PCs into a P-band image, normalized by SD.
  return principalComponents
      // Throw out an an unneeded dimension, [[]] -> [].
      .arrayProject([0])
      // Make the one band array image a multi-band image, [] -> image.
      .arrayFlatten([getNewBandNames('pc')])
      // Normalize the PCs by their SDs.
      .divide(sdImage);
};

Python 설정

Python API 및 대화형 개발을 위한 geemap 사용에 관한 자세한 내용은 Python 환경 페이지를 참고하세요.

import ee
import geemap.core as geemap

Colab (Python)

def get_principal_components(centered, scale, region):
  # Collapse bands into 1D array
  arrays = centered.toArray()

  # Compute the covariance of the bands within the region.
  covar = arrays.reduceRegion(
      reducer=ee.Reducer.centeredCovariance(),
      geometry=region,
      scale=scale,
      maxPixels=1e9,
  )

  # Get the 'array' covariance result and cast to an array.
  # This represents the band-to-band covariance within the region.
  covar_array = ee.Array(covar.get('array'))

  # Perform an eigen analysis and slice apart the values and vectors.
  eigens = covar_array.eigen()

  # This is a P-length vector of Eigenvalues.
  eigen_values = eigens.slice(1, 0, 1)
  # This is a PxP matrix with eigenvectors in rows.
  eigen_vectors = eigens.slice(1, 1)

  # Convert the array image to 2D arrays for matrix computations.
  array_image = arrays.toArray(1)

  # Left multiply the image array by the matrix of eigenvectors.
  principal_components = ee.Image(eigen_vectors).matrixMultiply(array_image)

  # Turn the square roots of the Eigenvalues into a P-band image.
  sd_image = (
      ee.Image(eigen_values.sqrt())
      .arrayProject([0])
      .arrayFlatten([get_new_band_names('sd')])
  )

  # Turn the PCs into a P-band image, normalized by SD.
  return (
      # Throw out an an unneeded dimension, [[]] -> [].
      principal_components.arrayProject([0])
      # Make the one band array image a multi-band image, [] -> image.
      .arrayFlatten([get_new_band_names('pc')])
      # Normalize the PCs by their SDs.
      .divide(sd_image)
  )

함수의 입력은 평균이 0인 이미지, 크기, 분석을 실행할 영역입니다. 입력 이미지는 먼저 1차원 배열 이미지로 변환한 다음 ee.Reducer.centeredCovariance()를 사용하여 줄여야 합니다. 이 감소에 의해 반환되는 배열은 입력의 대칭 분산-공분산 행렬입니다. eigen() 명령어를 사용하여 공분산 행렬의 고유값과 고유 벡터를 가져옵니다. eigen()에서 반환된 행렬에는 1축의 0번째 위치에 고유값이 포함됩니다. 이전 함수에서 볼 수 있듯이 slice()를 사용하여 고유값과 고유 벡터를 구분합니다. eigenVectors 행렬의 0축을 따라 있는 각 요소는 고유 벡터입니다. tasseled cap (TC) 예에서와 같이 arrayImage에 고유 벡터를 행렬 곱하여 변환을 실행합니다. 이 예에서 각 고유 벡터 곱셈은 PC를 생성합니다.