最近因为实验的原因需要把矩阵分解为两个矩阵U?W,同时要求U是正交列矩阵 那肯定最先想到SVD分解,于是搜索有没有可以直接拿来用的方法,通过下文找到灵感 svd图像压缩
import numpy as np
import os
from PIL import Image
def restore(sigma, u, v, K):
m = len(u)
n = len(v[0])
a = np.zeros((m, n))
for k in range(K):
uk = u[:, k].reshape(m, 1)
vk = v[k].reshape(1, n)
a += sigma[k] * np.dot(uk, vk)
a = a.clip(0, 255)
return np.rint(a).astype('uint8')
if __name__ == "__main__":
A = Image.open(".\\Pokonyan.jpg", 'r')
output_path = r'.\SVD_Out'
a = np.array(A)
print('type(a) = ', type(a))
print('原始图片大小:', a.shape)
u_r, sigma_r, v_r = np.linalg.svd(a[:, :, 0])
u_g, sigma_g, v_g = np.linalg.svd(a[:, :, 1])
u_b, sigma_b, v_b = np.linalg.svd(a[:, :, 2])
K = 50
for k in range(1, K+1):
R = restore(sigma_r, u_r, v_r, k)
G = restore(sigma_g, u_g, v_g, k)
B = restore(sigma_b, u_b, v_b, k)
I = np.stack((R, G, B), axis=2)
Image.fromarray(I).save('%s\\svd_%d.jpg' % (output_path, k))
通过debug我发现,numpy的svd函数返回的u,v和奇异值的降序排序数组,而不是奇异值矩阵,那我需要得到u和奇异值矩阵?v,首先要恢复出这个奇异值矩阵,这里需要注意的是,要给非方阵的奇异值矩阵的无奇异值的地方填0
def restore(sigma, u, vt, K):
m = len(u)
n = len(vt[0])
S = np.zeros([m, n])
for i in range(K):
S[i][i] = sigma[i]
U = u
W = np.dot(S,vt)
print(np.dot(U,W))
结果说明是一致的,唉,我数学功底实在是太差了,而且感觉是补不起来的那种
[[ 1.80979033 2.06082192 2.31185351]
[ 4.41855027 5.03143657 5.64432287]
[ 7.02731022 8.00205122 8.97679223]
[ 9.63607016 10.97266587 12.30926159]]
[[ 1.80979033 2.06082192 2.31185351]
[ 4.41855027 5.03143657 5.64432287]
[ 7.02731022 8.00205122 8.97679223]
[ 9.63607016 10.97266587 12.30926159]]
|