beihai commited on
Commit
b2d86b7
1 Parent(s): 66eedf1

Upload func.py

Browse files
Files changed (1) hide show
  1. func.py +23 -0
func.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+ def rebuild_img(u, sigma, v, percent): #p表示奇异值的百分比
4
+ m = len(u)
5
+ n = len(v)
6
+ a = np.zeros((m, n))
7
+
8
+ #根据指定的清晰度提取奇异值
9
+ #(清晰度越高,压缩比越低,提取的奇异值的个数也就越多,图片也就越不会失真)
10
+ count = (int)(sum(sigma))
11
+ curSum = 0
12
+ k = 0
13
+ while curSum <= count * percent:
14
+ uk = u[:, k].reshape(m, 1)
15
+ vk = v[k].reshape(1, n)
16
+ a += sigma[k] * np.dot(uk, vk)
17
+ curSum += sigma[k]
18
+ k += 1
19
+
20
+ a[a < 0] = 0
21
+ a[a > 255] = 255
22
+ #按照最近距离取整数,并设置参数类型为uint8
23
+ return np.rint(a).astype("uint8")