-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwt.py
395 lines (222 loc) · 9.04 KB
/
dwt.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import matplotlib.pyplot as plt
from PIL import Image
from sys import getsizeof
import numpy as np
from helpers import *
# converts rgb to gray
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
# reading # img_3
img = plt.imread('img_3.jpg')
img = rgb2gray(img)
# taking the first 400x400 pixels
img = img[:400,:400]
# img = np.ones((64,64))*127
# plt.imshow(img, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
# takes a vector and a filter and do normal convolution
def filter(vec,f):
newvec = np.convolve(vec, f)
return newvec
# down samples the vector to half
def downsample(vec):
vec=vec[::2]
return vec
# upsamples by interpolating 0's
def upsample(vec):
length = len(vec)
newvec = np.zeros((2*length))
for i in range(length):
newvec[2*i]=vec[i]
# for i in range(1,len(newvec)-1,2):
# newvec[i]=(newvec[i-1]+newvec[i+1])/2
return newvec
import numpy as np
# takes a vector and a value positive if append at the end and negative if at the beginning
# and then flips the abs(value) pixels to pad the vector
def extend_vec(vec,extension_value):
vec = list(vec)
if(extension_value<0):
ex = vec[1:-extension_value+1]
ex.reverse()
vec = ex + vec
elif(extension_value > 0):
ex = vec[-extension_value-1:-1]
ex.reverse()
vec = vec + ex
return np.asarray(vec)
x = [1,1,1,1,1]
y = [1,1,1,1,1]
ll = []
############################################
#discrete wavelet transform
# takes an image and an array n that represents the quantization values
def dwt(img,n):
#taking a copy of the img
copy_img = np.copy(img)
h0=np.array([-1/8 , 1/4 , 3/4 , 1/4 , -1/8])
h1=np.array([-1/2 , 1 , -1/2])
rows, columns = img.shape
######################
# here I do filtering either low or high on the x axis then on the y axis
# with the extension for correct convolution and then downsample
# I repeat this for 4 times
# then assign the final output in ll_2, lh_2, hl_2, and hh_2 ( those are the 4 block each assigned in its place)
# ll lh
# hl hh
# copy img and copy_img_2 are just helper matrcies to easily downsample and operate on the data
copy_img = np.zeros((img.shape[0], img.shape[1]+len(h0)-1))
for i in range(copy_img.shape[0]):
vec = extend_vec(img[i,:],-(len(h0)-1)//2)
vec = extend_vec(vec,(len(h0)-1)//2)
copy_img[i,:] = vec
copy_img_2 = np.zeros((copy_img.shape[0]+len(h0)-1, copy_img.shape[1]))
for i in range(copy_img_2.shape[1]):
vec = extend_vec(copy_img[:,i],-(len(h0)-1)//2)
vec = extend_vec(vec,(len(h0)-1)//2)
copy_img_2[:,i] = vec
ll_1 = np.zeros((copy_img_2.shape[0], img.shape[1]//2))
ll_2 = np.zeros((img.shape[0]//2, ll_1.shape[1]))
for row in range(rows):
ll_1[row,:] = downsample(filter(copy_img_2[row,:], h0)[len(h0)-1:-len(h0)+1])
for column in range(ll_1.shape[1]):
ll_2 [:,column] = downsample(filter(ll_1[:, column], h0)[len(h0)-1:-len(h0)+1])
img[0:img.shape[0]//2,0:img.shape[1]//2] = ll_2//n[0]
#######################
copy_img = np.zeros((img.shape[0], img.shape[1]+len(h0)-1))
for i in range(copy_img.shape[0]):
vec = extend_vec(img[i,:],-(len(h0)-1)//2)
vec = extend_vec(vec,(len(h0)-1)//2)
copy_img[i,:] = vec
copy_img_2 = np.zeros((copy_img.shape[0]+len(h1)-1, copy_img.shape[1]))
for i in range(copy_img_2.shape[1]):
vec = extend_vec(copy_img[:,i],-(len(h1)-1))
copy_img_2[:,i] = vec
lh_1 = np.zeros((copy_img_2.shape[0], img.shape[1]//2))
lh_2 = np.zeros((img.shape[0]//2, ll_1.shape[1]))
for row in range(rows):
lh_1[row,:] = downsample(filter(copy_img_2[row,:], h0)[len(h0)-1:-len(h0)+1])
for column in range(lh_1.shape[1]):
lh_2 [:,column] = downsample(filter(lh_1[:,column], h1)[len(h1)-1:-len(h1)+1])
img[0:img.shape[0]//2,img.shape[1]//2:] = lh_2//n[1]
#######################
copy_img = np.zeros((img.shape[0], img.shape[1]+len(h1)-1))
for i in range(copy_img.shape[0]):
vec = extend_vec(img[i,:],-(len(h1)-1))
copy_img[i,:] = vec
copy_img_2 = np.zeros((copy_img.shape[0]+len(h0)-1, copy_img.shape[1]))
for i in range(copy_img_2.shape[1]):
vec = extend_vec(copy_img[:,i],-(len(h0)-1)//2)
vec = extend_vec(vec,(len(h0)-1)//2)
copy_img_2[:,i] = vec
hl_1 = np.zeros((copy_img_2.shape[0], img.shape[1]//2))
hl_2 = np.zeros((img.shape[0]//2, hl_1.shape[1]))
for row in range(rows):
hl_1[row,:] = downsample(filter(copy_img_2[row,:], h1)[len(h1)-1:-len(h1)+1])
for column in range(hl_1.shape[1]):
hl_2 [:,column] = downsample(filter(hl_1[:,column], h0)[len(h0)-1:-len(h0)+1])
img[img.shape[0]//2:,0:img.shape[1]//2] = hl_2//n[2]
#######################
copy_img = np.zeros((img.shape[0], img.shape[1]+len(h1)-1))
for i in range(copy_img.shape[0]):
vec = extend_vec(img[i,:],-(len(h1)-1))
copy_img[i,:] = vec
copy_img_2 = np.zeros((copy_img.shape[0]+len(h1)-1, copy_img.shape[1]))
for i in range(copy_img_2.shape[1]):
vec = extend_vec(copy_img[:,i],-(len(h1)-1))
copy_img_2[:,i] = vec
hh_1 = np.zeros((copy_img_2.shape[0], img.shape[1]//2))
hh_2 = np.zeros((img.shape[0]//2, hh_1.shape[1]))
for row in range(rows):
hh_1[row,:] = downsample(filter(copy_img[row,:], h1)[len(h1)-1:-len(h1)+1])
for column in range(hh_1.shape[1]):
hh_2 [:,column] = downsample(filter(hh_1[:,column], h1)[len(h1)-1:-len(h1)+1])
img[img.shape[0]//2:,img.shape[1]//2:] = hh_2//n[3]
#######################
return img
quantization_array = [4,19,31,49]
img = dwt(img, quantization_array)
img[0:img.shape[0]//2,0:img.shape[1]//2] = dwt(img[0:img.shape[0]//2,0:img.shape[1]//2], quantization_array)
img[0:img.shape[0]//4,0:img.shape[1]//4] = dwt(img[0:img.shape[0]//4,0:img.shape[1]//4], quantization_array)
###########################################################
# 2d to 1d
one_d = takestwoD(img)
# run length
run_length_encoded = run_length(one_d)
# huffman encoding
huffman = Huffman_encoding()
huffman_encoded = huffman.compress(run_length_encoded)
from sys import getsizeof
# getting the size of raw image
print("size of image in bits:")
print(getsizeof(one_d))
print("\n\n")
# print(len(run_length_encoded))
#####
# doing decoding
# size of compressed image
print("size of compressed in bits:")
print(len(huffman_encoded))
print("\n\n")
huffman_decoded = huffman.decode_text(huffman_encoded)
run_length_decoded = reverse_run_length(run_length_encoded)
two_d = takesoneD(run_length_decoded, img.shape[0], img.shape[1])
############################################################################
# inverse discrete wavelet transform
# here is the inverse dwt
# almost the same operations but reversed
def idwt(img,n):
g0=np.array([1/2 , 1 , 1/2]) #-2 -1 0
g1=np.array([ -1/8 , -1/4 , 3/4 , -1/4 , -1/8 ]) # -1 0 1 2 3
rows , columns = img.shape
#creating 2 helper arrays to manipulate the input
# then do the operation 4 times
# x_1 is what is retreived from ll block
# x_2 is what is retreived from lh block
# x_3 is what is retreived from hl block
# x_4 is what is retreived from hh block
x1 = np.zeros((img.shape[0],img.shape[1]//2))
x3 = np.zeros((img.shape[0],img.shape[1]//2))
for column in range(columns//2):
upsampled = upsample(img[0:img.shape[0]//2,column]*n[0])
upsampled = extend_vec(upsampled,-(len(g0)-1))
for column in range(columns//2):
upsampled = upsample(img[img.shape[0]//2:,column]*n[2])
upsampled = extend_vec(upsampled,-(len(g0)-1))
x1[:,column] = filter(upsampled, g0)[len(g0)-1:-len(g0)+1]
x2 = np.zeros((img.shape[0],img.shape[1]//2))
x4 = np.zeros((img.shape[0],img.shape[1]//2))
for column in range(columns//2):
upsampled = upsample(img[img.shape[0]//2:,column]*n[1])
upsampled = extend_vec(upsampled,-1)
upsampled = extend_vec(upsampled, 3)
x2[:,column] = filter(upsampled, g1)[len(g1)-1:-len(g1)+1]
for column in range(columns//2):
upsampled = upsample(img[img.shape[0]//2:,column]*n[1])
upsampled = extend_vec(upsampled,-1)
upsampled = extend_vec(upsampled, 3)
x4[:,column] = filter(upsampled, g1)[len(g1)-1:-len(g1)+1]
######################
x_1 = x1 + x2
x_2 = x3 + x4
x_11 = np.zeros(img.shape)
x_22 = np.zeros(img.shape)
for row in range(x_1.shape[0]):
upsampled = upsample(x_1[row,:])
upsampled = extend_vec(upsampled,-(len(g0)-1))
x_11[row,:] = filter(upsampled, g0)[len(g0)-1:-len(g0)+1]
for column in range(columns//2):
upsampled = upsample(x_1[row,:])
upsampled = extend_vec(upsampled,-1)
upsampled = extend_vec(upsampled, 3)
x_22[:,column] = filter(upsampled, g1)[len(g1)-1:-len(g1)+1]
return (x_11 + x_22)
# img[0:img.shape[0]//4,0:img.shape[1]//4] = idwt(img[0:img.shape[0]//4,0:img.shape[1]//4],[1,1,1,1])
# img[0:img.shape[0]//2,0:img.shape[1]//2] = idwt(img[0:img.shape[0]//2,0:img.shape[1]//2], [1,1,1,1])
# img = idwt(img,[1,1,1,1])
# img = two_d
img[0:img.shape[0]//4,0:img.shape[1]//4] = idwt(img[0:img.shape[0]//4,0:img.shape[1]//4], quantization_array)
img[0:img.shape[0]//2,0:img.shape[1]//2] = idwt(img[0:img.shape[0]//2,0:img.shape[1]//2], quantization_array)
img = idwt(img, quantization_array)
print(img.shape)
plt.imshow(img, cmap = "gray")
#print(img)