单个图像 python 中的非负矩阵分解
Non negative matrix factorisation in python on individual images
我正在尝试将 NMF 应用于以灰度模式加载的特定图像。我已经尝试了几次link,但应用 NMF 后我的图像几乎保持不变,无法与最初加载的灰度图像区分开来。
然而,当我看到 scikit-learn 的关于在数据集上实现分解的代码时,我看到那里的人脸已经变成了鬼脸。这是 link:
这是我使用的代码:
import cv2
from sklearn import decomposition
import matplotlib.pyplot as plt
img = cv2.imread('test1.jpeg',0)
estimator = decomposition.NMF(n_components = 2, init = 'nndsvda', tol = 5e-3)
estimator.fit(img)
vmax = max(img.max(), -img.min())
plt.imshow(img, cmap=plt.cm.gray, interpolation = 'nearest',vmin=-vmax,vmax=vmax)
plt.show()
我对矩阵上的 NMF 技术很陌生,尤其是在这么大的图像 numpy 数组上。
我的图像是 test1.jpeg 即 225 * 224 .jpeg 图像。
有人可以帮我实现单张图片的代码吗?
非常感谢。
你在绘图中得到原始图像的原因是你实际绘制了原始图像。相反,您需要使用 estimator
.
的输出
NMF 分解产生两个矩阵 W
和 H
,它们构成了原始矩阵。你需要将它们相乘才能得到图像。
import cv2
from sklearn import decomposition
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('data/trump2.jpg',0)
vmax = max(img.max(), -img.min())
fig, (ax, ax2) =plt.subplots(ncols=2)
ax.imshow(img, cmap=plt.cm.gray, interpolation = 'nearest',vmin=-vmax,vmax=vmax)
n_components = 20
estimator = decomposition.NMF(n_components = n_components, init = 'random', tol=5e-3)
W = estimator.fit_transform(img)
H = estimator.components_
new_img = np.dot(W,H)
ax2.imshow(new_img, cmap=plt.cm.gray,
interpolation='nearest',
vmin=-vmax, vmax=vmax)
plt.show()
我正在尝试将 NMF 应用于以灰度模式加载的特定图像。我已经尝试了几次link,但应用 NMF 后我的图像几乎保持不变,无法与最初加载的灰度图像区分开来。
然而,当我看到 scikit-learn 的关于在数据集上实现分解的代码时,我看到那里的人脸已经变成了鬼脸。这是 link:
这是我使用的代码:
import cv2
from sklearn import decomposition
import matplotlib.pyplot as plt
img = cv2.imread('test1.jpeg',0)
estimator = decomposition.NMF(n_components = 2, init = 'nndsvda', tol = 5e-3)
estimator.fit(img)
vmax = max(img.max(), -img.min())
plt.imshow(img, cmap=plt.cm.gray, interpolation = 'nearest',vmin=-vmax,vmax=vmax)
plt.show()
我对矩阵上的 NMF 技术很陌生,尤其是在这么大的图像 numpy 数组上。
我的图像是 test1.jpeg 即 225 * 224 .jpeg 图像。
有人可以帮我实现单张图片的代码吗? 非常感谢。
你在绘图中得到原始图像的原因是你实际绘制了原始图像。相反,您需要使用 estimator
.
NMF 分解产生两个矩阵 W
和 H
,它们构成了原始矩阵。你需要将它们相乘才能得到图像。
import cv2
from sklearn import decomposition
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('data/trump2.jpg',0)
vmax = max(img.max(), -img.min())
fig, (ax, ax2) =plt.subplots(ncols=2)
ax.imshow(img, cmap=plt.cm.gray, interpolation = 'nearest',vmin=-vmax,vmax=vmax)
n_components = 20
estimator = decomposition.NMF(n_components = n_components, init = 'random', tol=5e-3)
W = estimator.fit_transform(img)
H = estimator.components_
new_img = np.dot(W,H)
ax2.imshow(new_img, cmap=plt.cm.gray,
interpolation='nearest',
vmin=-vmax, vmax=vmax)
plt.show()