gpt4 book ai didi

python - 为什么 PCA 图像与原始图像完全不相似?

转载 作者:行者123 更新时间:2023-11-30 09:03:27 27 4
gpt4 key购买 nike

我正在尝试在没有任何图像降维库的情况下实现 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_X
img=color.rgb2gray(io.imread('D:\lenna.png'))
x,y,z=pca(img)
plt.imshow(x)

original image PCA image

但是 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

The change here lies in the fact that we want to reconstruct the image using a given number of eigenvectors (determined by number_of_pcs).

要记住的是,在 np.linalg.svd 中,U 的列是 X.X^T 的特征向量。

这样做时,我们获得以下结果(此处使用 1 和 10 个主成分显示):

<小时/>
X_hat, S, mean_X = pca(img, 1)
plt.imshow(X_hat)

enter image description here

<小时/>
X_hat, S, mean_X = pca(img, 10)
plt.imshow(X_hat)

enter image description here

PS:请注意,由于 matplotlib.pyplot,图片不会以灰度显示,但这是一个非常小的问题。

关于python - 为什么 PCA 图像与原始图像完全不相似?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58976380/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com