查找图像的秩K逼近

时间:2019-03-06 01:46:11

标签: python pandas numpy jupyter-notebook

我正在编写一个函数,用于查找图像的秩近似。当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()

1 个答案:

答案 0 :(得分:0)

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

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

plt.imshow(rankTotal)
plt.show()

我只需要从rank1Total转到rankTotal,这是因为rankTotal是我想要保存的所有内容。