我正在尝试在没有使用任何库的情况下实现PCA来进行图像降维。我尝试了O’Reilly计算机视觉书中的代码,并将其应用于一个样本的lenna图片上:
from PIL import Image from numpy import * def pca(X): num_data, dim = X.shape mean_X = X.mean(axis=0) X = X - mean_X if dim > num_data: # PCA compact trick M = np.dot(X, X.T) # covariance matrix e, U = np.linalg.eigh(M) # calculate eigenvalues an deigenvectors tmp = np.dot(X.T, U).T V = tmp[::-1] # reverse since the last eigenvectors are the ones we want S = np.sqrt(e)[::-1] #reverse since the last eigenvalues are in increasing order for i in range(V.shape[1]): V[:,i] /= S else: # normal PCA, SVD method U,S,V = np.linalg.svd(X) V = V[:num_data] # only makes sense to return the first num_data return V, S, mean_Ximg=color.rgb2gray(io.imread('D:\lenna.png'))x,y,z=pca(img)plt.imshow(x)
但是PCA图像的显示结果与原始图像完全不相似。据我所知,PCA确实会降低图像的维度,但它仍然应该在某种程度上保留原始图像的轮廓,只是细节较少。代码哪里出了问题?
回答:
严格来说,你的代码本身并没有问题,但如果你明白你真正想要做的是什么,你展示的内容并不是正确的!
针对你的问题,我会这样写代码:
def pca(X, number_of_pcs): num_data, dim = X.shape mean_X = X.mean(axis=0) X = X - mean_X if dim > num_data: # PCA compact trick M = np.dot(X, X.T) # covariance matrix e, U = np.linalg.eigh(M) # calculate eigenvalues an deigenvectors tmp = np.dot(X.T, U).T V = tmp[::-1] # reverse since the last eigenvectors are the ones we want S = np.sqrt(e)[::-1] #reverse since the last eigenvalues are in increasing order for i in range(V.shape[1]): V[:,i] /= S return V, S, mean_X else: # normal PCA, SVD method U, S, V = np.linalg.svd(X, full_matrices=False) # reconstruct the image using U, S and V # otherwise you're just outputting the eigenvectors of X*X^T V = V.T S = np.diag(S) X_hat = np.dot(U[:, :number_of_pcs], np.dot(S[:number_of_pcs, :number_of_pcs], V[:,:number_of_pcs].T)) return X_hat, S, mean_X
这里的变化在于我们希望使用给定的特征向量数量(由
number_of_pcs
决定)来重建图像。
需要记住的是,在np.linalg.svd
中,U的列是X.X^T的特征向量。
这样做,我们可以得到以下结果(这里显示了使用1和10个主成分的结果):
X_hat, S, mean_X = pca(img, 1)plt.imshow(X_hat)
X_hat, S, mean_X = pca(img, 10)plt.imshow(X_hat)
附注:请注意,由于matplotlib.pyplot的原因,图片并未以灰度显示,但这是一个非常次要的问题。