【发布时间】:2014-04-27 07:45:07
【问题描述】:
我试图弄清楚如何使用 PCA 在 python 中对 RGB 图像进行去相关。 我正在使用 O'Reilly Computer vision book 中的代码:
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 的维度会影响我的结果吗?我该如何截断这个? 我如何找到保留了多少信息的数量?
【问题讨论】: