تبدیل اجزای اصلی (PC) (همچنین به عنوان تبدیل Karhunen-Loeve شناخته میشود) یک چرخش طیفی است که دادههای تصویر همبسته طیفی را میگیرد و دادههای غیرهمبسته را خروجی میدهد. تبدیل PC این کار را با مورب کردن ماتریس همبستگی باند ورودی از طریق تجزیه و تحلیل ویژه انجام می دهد. برای انجام این کار در Earth Engine، از یک کاهنده کوواریانس روی تصویر آرایه و دستور eigen()
روی آرایه کوواریانس حاصل استفاده کنید. تابع زیر را برای این منظور در نظر بگیرید (نمونه ای از آن در برنامه به عنوان یک اسکریپت ویرایشگر کد و یک نوت بوک Colab موجود است).
ویرایشگر کد (جاوا اسکریپت)
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
کولب (پایتون)
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) )
ورودی تابع یک تصویر میانگین صفر، مقیاس و ناحیه ای است که بر روی آن تجزیه و تحلیل انجام می شود. توجه داشته باشید که تصاویر ورودی ابتدا باید به یک تصویر آرایه 1 بعدی تبدیل شود و سپس با استفاده از ee.Reducer.centeredCovariance()
کاهش یابد. آرایه ای که با این کاهش بازگردانده می شود، ماتریس متقارن واریانس-کوواریانس ورودی است. از دستور eigen()
برای بدست آوردن مقادیر ویژه و بردارهای ویژه ماتریس کوواریانس استفاده کنید. ماتریس برگردانده شده توسط eigen()
حاوی مقادیر ویژه در موقعیت صفر محور 1 است. همانطور که در تابع قبلی نشان داده شد، slice()
برای جدا کردن مقادیر ویژه و بردارهای ویژه استفاده کنید. هر عنصر در امتداد محور صفر ماتریس eigenVectors یک بردار ویژه است. مانند مثال کلاهک منگوله ای (TC) ، تبدیل را با ماتریس ضرب آرایه arrayImage
در بردارهای ویژه انجام دهید. در این مثال، هر ضرب بردار ویژه منجر به یک PC می شود.