Python RGB图像的主成分分析
我试图弄明白如何使用PCA在python中对RGB图像进行去相关处理。 我正在使用奥雷利计算机视觉书中的代码:Python RGB图像的主成分分析,python,numpy,pca,svd,Python,Numpy,Pca,Svd,我试图弄明白如何使用PCA在python中对RGB图像进行去相关处理。 我正在使用奥雷利计算机视觉书中的代码: from PIL import Image from numpy import * def pca(X): # Principal Component Analysis # input: X, matrix with training data as flattened arrays in rows # return: projection matrix (with im
from PIL import Image
from numpy import *
def pca(X):
# Principal Component Analysis
# input: X, matrix with training data as flattened arrays in rows
# return: projection matrix (with important dimensions first),
# variance and mean
#get dimensions
num_data,dim = X.shape
#center data
mean_X = X.mean(axis=0)
for i in range(num_data):
X[i] -= mean_X
if dim>100:
print 'PCA - compact trick used'
M = dot(X,X.T) #covariance matrix
e,EV = linalg.eigh(M) #eigenvalues and eigenvectors
tmp = dot(X.T,EV).T #this is the compact trick
V = tmp[::-1] #reverse since last eigenvectors are the ones we want
S = sqrt(e)[::-1] #reverse since eigenvalues are in increasing order
else:
print 'PCA - SVD used'
U,S,V = linalg.svd(X)
V = V[:num_data] #only makes sense to return the first num_data
#return the projection matrix, the variance and the mean
return V,S,mean_X
我知道我需要展平我的图像,但形状是512x512x3。3的维度会偏离我的结果吗?我如何截断这个?
如何找到保留了多少信息的定量数字 如果有三个波段(这是RGB图像的情况),则需要按照以下方式重塑图像:
X = X.reshape(-1, 3)
对于512x512图像,新的X
将具有形状(262144,3)
。3的维度不会偏离你的结果;该维度表示图像数据空间中的特征。X
的每一行都是一个样本/观察值,每一列代表一个变量/特征
图像中的总方差等于np.sum
,即特征值之和。保留的方差量取决于保留的特征值/特征向量。因此,如果只保留第一个特征值/特征向量,则保留的图像方差分数将等于
f = S[0] / np.sum(S)