【问题标题】:Reverse generation of RGB PCA image not workingRGB PCA图像的反向生成不起作用
【发布时间】:2018-07-29 13:52:22
【问题描述】:

夏奇拉.jpg

我正在尝试压缩上面的图像,但我得到的输出是不正确的图像。我认为我正确地执行了 PCA 步骤,但在最后一步出现了问题。

夏奇拉压缩

import pylab as plt
import numpy as np

img = 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 PCA

pca = 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()

【问题讨论】:

    标签: python pca


    【解决方案1】:

    你搞砸了重塑。

    1. 替换img_reshaped = np.reshape(img, (930, 1860)) img_reshaped = np.reshape(img, (img.shape[0] * img.shape[1], img.shape[2]))

    2. 替换img_inverse_reshaped = np.reshape(img_inverse, (930,620,3)) img_inverse_reshaped = np.reshape(img_inverse, img.shape)

    一旦我解决了这个问题,图像开始看起来或多或少合理。

    那你得把pca = PCA(.95)换成pca = PCA(n_components=3),图片会更漂亮=)

    【讨论】:

    • 谢谢。我也发现了我的代码中的错误。如果我将 img_inverse_reshape 除以 255,问题就会得到解决。是的,n_components=3 看起来很漂亮:)
    猜你喜欢
    • 2014-04-27
    • 1970-01-01
    • 2013-11-21
    • 2021-08-23
    • 2023-03-11
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2016-01-23
    相关资源
    最近更新 更多