تحويل المكوّنات
الرئيسية (PC) (المعروف أيضًا باسم تحويل Karhunen-Loeve) هو
دوران طيفي يأخذ بيانات الصور المرتبطة طيفيًا ويعرض بيانات
غير مرتبطة. يحقّق تحويل المكونات الأساسية ذلك من خلال تقطيع مصفوفة ربط نطاقات الإدخال
إلى مصفوفة قطرية من خلال تحليل القيم الذاتية. لإجراء ذلك في Earth Engine، استخدِم أداة تقليل التباين على
صورة صفيف واستخدم الأمر eigen()
على صفيف التباين الناتج.
يمكنك استخدام الدالة التالية لهذا الغرض (يتوفر مثال على ذلك في التطبيق
كملف
نص برمجي في "محرِّر الرموز البرمجية"
ودفتر ملاحظات 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); };
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) )
الإدخال إلى الدالة هو صورة متوسّطها صفر، ومقياس ومنطقة يتم
إجراء التحليل عليها. يُرجى العِلم أنّه يجب أولاً تحويل الصور التي تم إدخالها إلى صورة مصفوفة ثنائية الأبعاد، ثم تقليلها باستخدام ee.Reducer.centeredCovariance()
. مصفوفة
التي يعرضها هذا التخفيض هي مصفوفة التباين المتماثل لمعامل الترابط للإدخال.
استخدِم الأمر eigen()
للحصول على القيم الذاتية والعناصر الذاتية لملف
مصفوفة التباين. تحتوي المصفوفة التي تعرضها الدالة eigen()
على القيم الذاتية
في الموضع 0 من المحور 1. كما هو موضّح في الدالة السابقة، استخدِم slice()
لفصل القيم الذاتية والقيم الذاتية للناقلات. كل عنصر على طول محور 0 في
مصفوفة المتجهات الذاتية هو متجه ذاتي. كما هو الحال في
مثال القبّعة المزيّنة بخيوط (TC)، نفِّذ
التحويل من خلال ضرب المصفوفة arrayImage
في المتجهات الذاتية.
في هذا المثال، يؤدي كلّ ضرب في متجه خاص إلى إنشاء مكوّن رئيسي.