首页 > 解决方案 > 寻找图像的 Rank K 近似值

问题描述

我正在编写一个函数来查找图像的等级近似值。当 K 等于 1 和 2 时,我设法让它工作,但我找不到制作通用函数的方法。此外,我通过调用 linalg.svd() 成功地创建了 U/Vh/s 变量。

对于 2 级,我做了这样的事情来组合图像,并且成功地总结了 SVD

rank1TotalOne = np.dstack((rank1Redone, rank1Greenone, rank1Blueone))
rank1TotalTwo = np.dstack((rank1Redtwo, rank1Greentwo, rank1Bluetwo))

plt.imshow((rank1TotalOne + rank1TotalTwo).astype(int))

现在我试图将它概括为这样的东西,但我的图像没有正确显示。

k = 10
rankTotal = 0
j = 0
for i in range(0, k):
    while(j < k):
        columnVect = UR[:, j].reshape(300,1)
        rowVect = VhR.T[:,j].reshape(1, 200)
        rank1Red = sR[j] * columnVect * rowVect

        columnVect = UG[:, j].reshape(300,1)
        rowVect = VhG.T[:,j].reshape(1,200)
        rank1Green = sG[j] * columnVect * rowVect

        columnVect = UB[:, j].reshape(300,1)
        rowVect = VhB.T[:,j].reshape(1,200)
        rank1Blue = sB[j] * columnVect * rowVect

        rank1Total = rank1Total + np.dstack((rank1Red, rank1Green, rank1Blue))
        rank1Total = rank1Total.astype(int)
        j = j + 1

plt.imshow(rank1Total)
plt.show()

标签: pythonpandasnumpyjupyter-notebook

解决方案


我真的很笨,我刚刚注意到我的代码有一个 rank1Total 和 rankTotal 变量。

rankTotal = rankTotal + np.dstack((rank1Red, rank1Green, rank1Blue))
rankTotal = rankTotal.astype(int)
j = j + 1

plt.imshow(rankTotal)
plt.show()

我只需要从 rank1Total 到 rankTotal,这是因为 rankTotal 我希望将所有内容保存到。


推荐阅读