-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompression.py
60 lines (47 loc) · 1.67 KB
/
compression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python
#coding: utf-8
# file: compression.py
# brief: This file contains a simple compression image using SVD decomposition
# author: Petrucio Ricardo Tavares de Medeiros
# date: 13/05/2019
import sys
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from scipy import linalg
import numpy as np
# RBG to Grayscale function
def rgb2gray(rgb):
r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
if __name__ == '__main__':
print("ipython compression.py image reduzedDimension"+ "\n"+
"- reduzedDimension: Dimension reduzed to reduction")
file = sys.argv[1]
k = int(sys.argv[2])
# Reading image
img = mpimg.imread(file)
# Converting RGB to Grayscale
img = rgb2gray( img )
print(img.shape)
m = img.shape[0]
n = img.shape[1]
# Singular Value Decomposition
U, S, V = linalg.svd( img[:, :] )
print( "Dimensao de U, S, V, respectivamente" )
print( U.shape, S.shape, V.shape )
# Reconstruct SVD
#Sigma = np.zeros((m, n))
#Sigma[:n, :n] = np.diag(S)
#imgReconstruct = np.dot( np.dot( U, Sigma ), V )
#imgplot = plt.imshow( imgReconstruct, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
# Reduzed SVD
reduzedU = U[:, :k]
reduzedV = V[:k, :]
reduzedS = np.zeros((k, k))
reduzedS[:k, :k] = np.diag(S[:k])
print( "Dimensao de U, S, V, respectivamente, apos a reducao" )
print( reduzedU.shape, reduzedS.shape, reduzedV.shape )
imgReduzed = np.dot( np.dot( reduzedU, reduzedS ), reduzedV )
imgplot = plt.imshow( imgReduzed, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()