המרה של רכיבים ראשיים (PC) (שנקראת גם המרת Karhunen-Loeve) היא רוטציה ספקטרלית שמקבלת נתוני תמונה עם קורלציה ספקטרלית ומפיקה נתונים ללא קורלציה. טרנספורמציית ה-PC עושה זאת על ידי יצירת אלכסון במטריצה של קורלציית הלהקה של הקלט באמצעות ניתוח אייג'ן. כדי לעשות זאת ב-Earth Engine, משתמשים במצמצם תנודות (covariance reducer) בתמונה של מערך, ובפקודה eigen()
במערך התנועה שנוצר.
אפשר להשתמש בפונקציה הבאה למטרה הזו (דוגמה שלה באפליקציה זמינה בתור סקריפט של Code Editor וביומן של Colab).
Code Editor (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 של המטריצה eigenVectors הוא וקטור עצמי. כמו בדוגמה של כובע קטן עם נוצות (TC), מבצעים את הטרנספורמציה על ידי כפל המטריצה arrayImage
בווקטורים העצמיים.
בדוגמה הזו, כל כפל של וקטור עצמי מניב רכיב PC.