RGB图像的PCA [英] PCA of RGB Image
问题描述
我试图弄清楚如何使用PCA在python中解相关RGB图像. 我正在使用O'Reilly Computer视觉书籍中的代码:
I'm trying to figure out how to use PCA to decorrelate an RGB image in python. I'm using the code found in the 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的维度会影响我的结果吗?我该如何截断? 我如何找到保留多少信息的定量数量?
I know I need to flatten my image, but the shape is 512x512x3. Will the dimension of 3 throw off my result? How do I truncate this? How do I find a quantitative number of how much information is retained?
推荐答案
如果有三个波段(RGB图像就是这种情况),则需要像这样重塑图像
If there are three bands (which is the case for an RGB image), you need to reshape your image like
X = X.reshape(-1, 3)
在512x512图像的情况下,新的X
将具有形状(262144, 3)
. 3维将不会影响您的结果;该维度表示图像数据空间中的特征. X
的每一行都是一个样本/观测值,每一列代表一个变量/特征.
In your case of a 512x512 image, the new X
will have shape (262144, 3)
. The dimension of 3 will not throw off your result; that dimension represents the features in the image data space. Each row of X
is a sample/observation and each column represents a variable/feature.
图像中的总方差等于np.sum(S)
,这是特征值的总和.您保留的方差量将取决于您保留的特征值/特征向量.因此,如果仅保留第一个特征值/特征向量,则保留的图像方差分数将等于
The total amount of variance in the image is equal to np.sum(S)
, which is the sum of eigenvalues. The amount of variance you retain will depend on which eigenvalues/eigenvectors you retain. So if you only keep the first eigenvalue/eigenvector, then the fraction of image variance you retain will be equal to
f = S[0] / np.sum(S)
这篇关于RGB图像的PCA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!