Thanks for the reply. I wrote the below code snippet which is for the PCA calculation and the PCA statistics. I thought it is better to share it here for anyone how might need it. PRO PCA e = ENVI() ; Define the filepath and the filter filepath = 'D:\masterEO/Scores.tif' ; Number of PCA, if you put 0 or minus value, the PCA will be equel to the number of bands. PCA_Dimention = 5 ; Opening the images Raster = e.OpenRaster(filepath) data = raster.GetData() dim_data = Size(data, /Dimensions) bands = FltArr(dim_data[2], dim_data[0] * dim_data[1]) FOR I=0,dim_data[2]-1 DO bands[I,*] = Reform(data(*,*, I), 1, dim_data[0] * dim_data[1]) dim_bands = Size(bands, /Dimensions) means = TOTAL(bands, 2)/dim_bands[1] bands = bands - REBIN(means, dim_bands[0], dim_bands[1]) ;Test the PCA Dimention IF (PCA_Dimention GT dim_bands[0]) OR (PCA_Dimention LE 0) Then PCA_Dimention = dim_bands[0] ;Compute the principal components. result = PCOMP(bands, COEFFICIENTS = coefficients, /DOUBLE, $ EIGENVALUES=eigenvalues, VARIANCES=variances, /COVARIANCE, NVARIABLES = PCA_Dimention) Correlate = CORRELATE(bands) Raster_Result = FltArr(dim_data[0], dim_data[1], PCA_Dimention) FOR I=0, PCA_Dimention-1 DO Raster_Result[*,*,I] = Reform(result(I,*,*), dim_data[0], dim_data[1]) ; Create output image METADATA_PCA = ENVIRasterMetadata() METADATA_PCA.AddItem, 'BAND NAMES', string(indgen(PCA_Dimention, START=1))+":PCA" PCA_Raster = ENVIRaster(Raster_Result, URI=StrSplit(filepath, '.tif', /EXTRACT, /REGEX)+"_PCA.dat", SPATIALREF=Raster.SPATIALREF, METADATA=METADATA_PCA) PCA_Raster.Save ; Print the Stat file Write_format = STRJOIN(["('Band#',",'I2,',strcompress(dim_bands[0],/REMOVE_ALL),"(F10.6))"]) filepat='D:\test\stat.txt' OPENW, lun, filepat, /get_lun ;Write the Pat file ready for NN PRINTF, lun, 'Coefficients: ' FOR mode=0,dim_bands[0]-1 DO PRINTF, lun, mode+1, coefficients[*,mode], FORMAT=Write_format eigenvectors = coefficients/REBIN(eigenvalues, dim_bands[0], dim_bands[0]) PRINTF, lun PRINTF, lun, 'Correlations: ' FOR mode=0,dim_bands[0]-1 DO PRINTF, lun, mode+1, Correlate[*,mode], FORMAT=Write_format PRINTF, lun PRINTF, lun, 'Eigenvectors: ' FOR mode=0,dim_bands[0]-1 DO PRINTF, lun, mode+1, eigenvectors[*,mode], FORMAT=Write_format PRINTF, lun PRINTF, lun, 'Energy conservation: ', TOTAL(bands^2), $ TOTAL(eigenvalues)*(dim_bands[0]-1) PRINTF, lun PRINTF, lun, ' Mode Eigenvalue PercentVariance' FOR mode=0,dim_bands[0]-1 DO PRINTF, lun, $ mode+1, eigenvalues[mode], variances[mode]*100 FREE_LUN, lun ; Get the collection of data objects currently available in the Data Manager DataColl = e.Data ; Add the output to the Data Manager DataColl.Add, PCA_Raster ; Display the result View1 = e.GetView() Layer1 = View1.CreateLayer(PCA_Raster) END
|