/
sasha.py
172 lines (150 loc) · 3.78 KB
/
sasha.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from __future__ import division
from numpy import *
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw
import photos
def toImage(I):
# converts matrix into image
img = Image.new('L',(shape(I)[0],shape(I)[1]))
pixels = img.load()
for i in range(img.size[0]):
for j in range(img.size[1]):
pixels[i, j] = I[j,i]
return img
def function(x):
coeff = [0,0,-1/2,1/3,-1/4,1/5,-1/6,1/7,-1/8,1/9,-1/10,1/11,-1/12,1/13,-1/14,1/15,-1/16,1/17,-1/18,1/19,-1/20,1/21]
# coefficients of the derivative
dcoeff = zeros(len(coeff))
for n in range(0,len(coeff)):
dcoeff[n] = (-1)*coeff[n]*n
# L2 norm of the derivative
norm = linalg.norm(dcoeff)
#normalized coefficients
ncoeff = coeff / norm
f = zeros(len(x))
for i in range(0,len(x)):
for n in range(0,len(coeff)):
f[i] = f[i]+coeff[n]*cos(x[i]*n)
return f
def NormD(f):
# Norm of the derivative
normD = zeros(len(f))
y = DCT(f,len(f))
# Derivative coefficients
d = zeros(len(y))
for n in range(0,len(y)):
d[n] = (-1)*y[n]*n
# L2 norm of the derivative
normD = linalg.norm(d)
return normD
def DCT(f,N):
# f - signal values
# N - number of coefficients to compute
# DCT Matrix
Phi = zeros((N,len(f)))
for j in range(0,len(f)):
Phi[0,j] = 1/sqrt(len(f))
for i in range(1,N):
for j in range (0,len(f)):
Phi[i,j] = sqrt(2/len(f)) * cos(i*(2*j+1)*pi/(2*len(f)))
# DCT coefficients of f
DCT = dot(Phi,f)
return DCT
def iDCT(f,N):
# f - DCT coefficients
# N - number of nodes
# Inverse DCT matrix is Phi.T
Phi = zeros((N,len(f)))
for i in range(0,N):
Phi[i,0] = 1/sqrt(len(f))
for i in range(0,N):
for j in range (1,len(f)):
Phi[i,j] = sqrt(2/len(f)) * cos(j*(2*i+1)*pi/(2*len(f)))
# inverse DCT of f
iDCT = dot(Phi,f)
return iDCT
def Opt(f,N,delta,C):
# Define p
S=0
k=0
while S<=C**2 and k<=len(f)-1:
S = S + delta[k]**2*k**2
k = k+1
p = k-1
#print('Number of coefficients in optimal method:',p+1)
# define filter a
a = ones(N)
if p < N-1:
a = zeros(N)
for i in range(0,p+1):
a[i] = 1-(i**2)/((p+1)**2)
#optimal recovery of f
Opt = iDCT(f*a,N)
return Opt
def Quantization(y,quant):
# quant - parameter of quantization
# divide q by a constant
q = y/quant
# round q to the integer values
for i in range(0,len(q)):
if q[i] >= 0:
q[i] = int(floor(q[i]))
else:
q[i] = int(ceil(q[i]))
# multiply q by the same constant
#q = (q+random.rand(len(q)))*quant
q = q*quant
return q
def CompressBlock(img,C,N,quant):
delta = ones(N)*quant/2
naturalMat = zeros((N,N))
optimalMat = zeros((N,N))
originalMat = zeros((N,N))
for i in range(0,N):
f = img[i,:]
C = NormD(f)
y = DCT(f,N)
g = Quantization(y,quant)
# shift by 0.5*quant
for j in range(0,len(g)):
if g[j]>0:
g[j] = g[j] + 0.5*quant
if g[j]<0:
g[j] = g[j] - 0.5*quant
u = iDCT(g,N)
v = Opt(g,N,delta,C)
naturalMat[i,:] = u
optimalMat[i,:] = v
return naturalMat, optimalMat
# MAIN CODE
C = 1
N = 8
quant = 2**4
#img = Image.open('sashaBig.png')
img = photos.pick_image(True)
I = img.convert('L')
Img = array(I)
original = zeros((len(Img),len(Img)))
natural = zeros((len(Img),len(Img)))
optimal = zeros((len(Img),len(Img)))
for i in range(0,32):
print i
for j in range(0,32):
img = Img[i*N:i*N+N,j*N:j*N+N]
naturalMat, optimalMat = CompressBlock(img,C,N,quant)
original[i*N:i*N+N,j*N:j*N+N] = Img[i*N:i*N+N,j*N:j*N+N]
natural[i*N:i*N+N,j*N:j*N+N] = naturalMat
optimal[i*N:i*N+N,j*N:j*N+N] = optimalMat
print ('Error of natural method:',
linalg.norm(original-natural))
print ('Error of optimal method:',
linalg.norm(original-optimal))
#original = toImage(original)
#photos.save_image(original)
#original.save('original.png')
natural = toImage(natural)
photos.save_image(natural)
#natural.save('natural.png')
optimal = toImage(optimal)
photos.save_image(optimal)
#optimal.save('optimal.png')