Shakira.jpgI am trying to compress the above image but the output that I am getting is an improper image. I think I am doing the PCA steps correctly, but something is going wrong at the final step.Shakira compressedimport pylab as pltimport numpy as npimg = plt.imread("shakira.jpg")print(img.shape)plt.axis('off') plt.imshow(img)plt.show()img_reshaped = np.reshape(img, (930, 1860))print(img_reshaped.shape)from sklearn.decomposition import PCApca = PCA(.95)pca.fit(img_reshaped)img_transformed = pca.transform(img_reshaped)print(img_transformed.shape)img_inverse = pca.inverse_transform(img_transformed)print(img_inverse.shape)plt.imshow(img_inverse)plt.show()img_inverse_reshaped = np.reshape(img_inverse, (930,620,3))print(img_inverse.shape)plt.axis('off') plt.imshow(img_inverse_reshaped)plt.show() 解决方案 You have messed up the reshape.replace img_reshaped = np.reshape(img, (930, 1860))with img_reshaped = np.reshape(img, (img.shape[0] * img.shape[1], img.shape[2]))replace img_inverse_reshaped = np.reshape(img_inverse, (930,620,3))with img_inverse_reshaped = np.reshape(img_inverse, img.shape)Once I fixed that, the image started to look more or less reasonable.Then you have to replace pca = PCA(.95) to pca = PCA(n_components=3), and the image will look even pretty =) 这篇关于RGB PCA 图像的反向生成不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-25 07:37