如何将PCA应用于图像

问题描述 投票:0回答:1

我有一个灰色的嘈杂图像。我想应用PCA来降低噪音,并在应用后查看输出。

这是我试图做的:


from sklearn.datasets import load_sample_image
from sklearn.feature_extraction import image
from sklearn.decomposition import PCA
# Create patches of size 3 by 3 and create a matrix from all patches
patches = image.extract_patches_2d(image, (3, 3), random_state = 42)
# reshape patches because I got an error when applying fit_transform(ValueError: Found array with dim 4. Estimator expected <= 2.)
patches_reshaped = patches.reshape(4,-1)
pca = PCA()
projected = pca.fit_transform(patches_reshaped.data)

结果得到一个数组。如何查看去噪的图像?

python pca noise-reduction
1个回答
1
投票

为了查看降噪后的图像,您需要将使用主成分以低维表示的数据转换回原始空间。为此,您可以使用inverse_transform()功能。从文档here中可以看到,此函数将接受投影的数据并返回类似于原始图像的数组。所以你可以做类似的事情,

denoised_image = pca.inverse_transform(projected)
# then view denoised_image
© www.soinside.com 2019 - 2024. All rights reserved.