การเปลี่ยนรูปแบบคอมโพเนนต์หลัก (PC) (หรือที่เรียกว่าการเปลี่ยนรูปแบบ Karhunen-Loeve) คือการหมุนสเปกตรัมที่ใช้ข้อมูลภาพซึ่งมีการเชื่อมโยงสเปกตรัมและแสดงผลข้อมูลที่ไม่มีการเชื่อมโยง การเปลี่ยนรูปแบบ PC ทําเช่นนี้ได้โดยการเปลี่ยนเมทริกซ์ความสัมพันธ์ของย่านความถี่อินพุตให้เป็นรูปแบบแนวทแยงผ่านการวิเคราะห์ Eigen หากต้องการดำเนินการนี้ใน 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) )
อินพุตของฟังก์ชันคือรูปภาพที่มีค่าเฉลี่ยเป็น 0, มาตราส่วน และภูมิภาคที่จะทำการวิเคราะห์ โปรดทราบว่าภาพอินพุตต้องได้รับการแปลงเป็นภาพอาร์เรย์ 1 มิติก่อน จากนั้นจึงลดขนาดโดยใช้ ee.Reducer.centeredCovariance()
อาร์เรย์ที่แสดงผลจากการลดขนาดนี้คือเมทริกซ์ความแปรปรวน-ความแปรปรวนร่วมแบบสมมาตรของอินพุต
ใช้คำสั่ง eigen()
เพื่อรับค่า Eigenvalue และ Eigenvector ของเมทริกซ์ความแปรปรวน เมทริกซ์ที่ eigen()
แสดงผลจะมีค่า Eigenvalue ในตำแหน่งที่ 0 ของแกนที่ 1 ดังที่แสดงในฟังก์ชันก่อนหน้านี้ ให้ใช้ slice()
เพื่อแยกค่า Eigenvalue และ Eigenvector องค์ประกอบแต่ละรายการตามแนวแกน 0 ของเมทริกซ์ EigenVectors คือ Eigenvector เช่นเดียวกับในตัวอย่างหมวกปีก (TC) ให้ทำการเปลี่ยนรูปแบบโดยการคูณเมทริกซ์ arrayImage
ด้วยเวกเตอร์ Eigen
ในตัวอย่างนี้ การคูณด้วยเวกเตอร์ลักษณะเฉพาะแต่ละรายการจะให้ผลลัพธ์เป็น PC