forked from qkstngus000/Visual-Compressive-Sensing
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompress_sensing_example.py
More file actions
279 lines (228 loc) · 10.9 KB
/
Copy pathcompress_sensing_example.py
File metadata and controls
279 lines (228 loc) · 10.9 KB
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
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
from src.compress_sensing import *
from src.utility import *
from PIL import Image, ImageOps
#Preparing parameters needed for the examples:
#Hyperparameter Values
small_img = "tree_part1.jpg"
big_img="peppers.png"
method = 'dct'
observation="pixel"
mode = '-c'
alpha=0.1
num_cell_100 = 100
num_cell_300 = 300
cell_size = 7
blob_size = 2
## For wavelet variable
lv= 2
dwt_type= 'db2'
#Load Images:
# Represent image as numpy array to make it easier to process
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
small_img_arr = process_image(small_img, mode)
ax1.imshow(small_img_arr)
ax1.set_title("{img}".format(img = small_img.split('.')[0]))
ax1.axis('off')
print(small_img_arr.shape)
small_img_arr_gray = process_image(small_img, False) #change from 'gray' to False
ax2.imshow(small_img_arr_gray, 'gray')
print(small_img_arr_gray.shape)
ax2.set_title("{img} grayscaled".format(img = small_img.split('.')[0]))
ax2.axis('off')
plt.show()
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
big_img_arr = process_image(big_img, mode)
ax1.imshow(big_img_arr)
ax1.set_title("{img}".format(img = big_img.split('.')[0]))
ax1.axis('off')
print(big_img_arr.shape)
big_img_arr_gray = process_image(big_img, False) #change from 'gray' to False
ax2.imshow(big_img_arr_gray, 'gray')
ax2.set_title("{img} grayscaled".format(img = big_img.split('.')[0]))
ax2.axis('off')
print(big_img_arr_gray.shape)
plt.show()
#Reconstruct Images using Compress_Sensing_Library:
#1)Reconstruction Using Pixel Observation with grayscaled reconstruction Vs Color Reconstruction
# Pixel
pixel_W_100, pixel_y_100 = generate_pixel_observation(small_img_arr_gray, num_cell_100) #change generate_pixel_variables to generate_pixel_observation
pixel_W_300, pixel_y_300 = generate_pixel_observation(small_img_arr_gray, num_cell_300)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
fig.suptitle("Pixel Grascaled Reconstruction")
## Reconstruction with 100 number of cells grayscaled
reconst_gray_100 = reconstruct(pixel_W_100, pixel_y_100, alpha)
ax1.imshow(reconst_gray_100, 'gray')
ax1.set_title("{num_cell} number of cells".format(num_cell = num_cell_100))
ax1.axis("off")
## Reconstruction with 300 number of cells grayscaled
reconst_gray_300 = reconstruct(pixel_W_300, pixel_y_300, alpha)
ax2.imshow(reconst_gray_300, 'gray')
ax2.set_title("{num_cell} number of cells".format(num_cell = num_cell_300))
ax2.axis("off")
plt.subplots_adjust(top=1.35)
plt.show()
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
fig.suptitle("Pixel Color Reconstruction")
## Reconstruction with 100 number of cells
reconst_color_100 = color_experiment(small_img_arr, num_cell_100, alpha, observation = 'pixel')
ax1.imshow(reconst_color_100)
ax1.set_title("{num_cell} number of cells".format(num_cell = num_cell_100))
ax1.axis("off")
## Reconstruction with 300 number of cells
reconst_color_300 = color_experiment(small_img_arr, num_cell_300, alpha, observation = 'pixel')
ax2.imshow(reconst_color_300)
ax2.set_title("{num_cell} number of cells".format(num_cell = num_cell_300))
ax2.axis("off")
plt.subplots_adjust(top=1.35)
plt.show()
# Pixel
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
fig.suptitle("Pixel Grascaled Reconstruction")
## Reconstruction with 100 number of cells grayscaled
reconst_gray_100 = large_img_experiment(big_img_arr_gray, num_cell_100, cell_size, blob_size, alpha = alpha, observation = 'pixel')
ax1.imshow(reconst_gray_100, 'gray')
ax1.set_title("{num_cell} number of cells".format(num_cell = num_cell_100))
ax1.axis("off")
## Reconstruction with 300 number of cells grayscaled
reconst_gray_300 = large_img_experiment(big_img_arr_gray, num_cell_300, cell_size, blob_size, alpha = alpha, observation = 'pixel')
ax2.imshow(reconst_gray_300, 'gray')
ax2.set_title("{num_cell} number of cells".format(num_cell = num_cell_300))
ax2.axis("off")
plt.subplots_adjust(top=1.35)
plt.show()
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
fig.suptitle("Pixel Color Reconstruction")
## Reconstruction with 100 number of cells
reconst_color_100 = large_img_experiment(big_img_arr, num_cell_100, cell_size, blob_size, alpha = alpha, color = True, observation = 'pixel')
ax1.imshow(reconst_color_100)
ax1.set_title("{num_cell} number of cells".format(num_cell = num_cell_100))
ax1.axis("off")
## Reconstruction with 300 number of cells
reconst_color_300 = large_img_experiment(big_img_arr, num_cell_300, cell_size, blob_size, alpha = alpha, color = True, observation = 'pixel')
ax2.imshow(reconst_color_300)
ax2.set_title("{num_cell} number of cells".format(num_cell = num_cell_300))
ax2.axis("off")
plt.subplots_adjust(top=1.35)
plt.show()
#2)Reconstruction Using Gaussian Observation with grayscaled reconstruction Vs Color Reconstruction:
# Gaussian observation on small image
gaussian_W_100, gaussian_y_100 = generate_gaussian_observation(small_img_arr_gray, num_cell_100)
gaussian_W_300, gaussian_y_300 = generate_gaussian_observation(small_img_arr_gray, num_cell_300)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
fig.suptitle("gaussian Grascaled Reconstruction")
## Reconstruction with 100 number of cells grayscaled
reconst_gray_100 = reconstruct(gaussian_W_100, gaussian_y_100, alpha)
ax1.imshow(reconst_gray_100, 'gray')
ax1.set_title("{num_cell} number of cells".format(num_cell = num_cell_100))
ax1.axis("off")
# ## Reconstruction with 300 number of cells grayscaled
reconst_gray_300 = reconstruct(gaussian_W_300, gaussian_y_300, alpha)
ax2.imshow(reconst_gray_300, 'gray')
ax2.set_title("{num_cell} number of cells".format(num_cell = num_cell_300))
ax2.axis("off")
plt.subplots_adjust(top=1.35)
plt.show()
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
## Reconstruction with 100 number of cells
reconst_color_100 = color_experiment(small_img_arr, num_cell_100, alpha, observation = 'gaussian')
ax1.imshow(reconst_color_100)
ax1.set_title("{num_cell} number of cells".format(num_cell = num_cell_100))
ax1.axis("off")
## Reconstruction with 300 number of cells
reconst_color_300 = color_experiment(small_img_arr, num_cell_300, alpha, observation = 'gaussian')
ax2.imshow(reconst_color_300)
ax2.set_title("{num_cell} number of cells".format(num_cell = num_cell_300))
ax2.axis("off")
plt.show()
# Gaussian observation on big image
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
fig.suptitle("gaussian Grascaled Reconstruction")
## Reconstruction with 100 number of cells grayscaled
reconst_gray_100 = large_img_experiment(big_img_arr_gray, num_cell_100, cell_size, blob_size, alpha = alpha, observation = 'gaussian')
ax1.imshow(reconst_gray_100, 'gray')
ax1.set_title("{num_cell} number of cells".format(num_cell = num_cell_100))
ax1.axis("off")
## Reconstruction with 300 number of cells grayscaled
reconst_gray_300 = large_img_experiment(big_img_arr_gray, num_cell_300, cell_size, blob_size, alpha = alpha, observation = 'gaussian')
ax2.imshow(reconst_gray_300, 'gray')
ax2.set_title("{num_cell} number of cells".format(num_cell = num_cell_300))
ax2.axis("off")
plt.subplots_adjust(top=1.35)
plt.show()
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
fig.suptitle("gaussian Color Reconstruction")
## Reconstruction with 100 number of cells
reconst_color_100 = large_img_experiment(big_img_arr, num_cell_100, cell_size, blob_size, alpha = alpha, color = True, observation = 'gaussian')
ax1.imshow(reconst_color_100)
ax1.set_title("{num_cell} number of cells".format(num_cell = num_cell_100))
ax1.axis("off")
## Reconstruction with 300 number of cells
reconst_color_300 = large_img_experiment(big_img_arr, num_cell_300, cell_size, blob_size, alpha = alpha, color = True, observation = 'gaussian')
ax2.imshow(reconst_color_300)
ax2.set_title("{num_cell} number of cells".format(num_cell = num_cell_300))
ax2.axis("off")
plt.subplots_adjust(top=1.35)
plt.show()
#3)Reconstruction Using V1 Observation with grayscaled reconstruction Vs Color Reconstruction:
# V1 observation on small image
V1_W_100, V1_y_100 = generate_V1_observation(small_img_arr_gray, num_cell_100, cell_size, blob_size)
V1_W_300, V1_y_300 = generate_V1_observation(small_img_arr_gray, num_cell_300, cell_size, blob_size)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
fig.suptitle("V1 Grascaled Reconstruction")
## Reconstruction with 100 number of cells grayscaled
reconst_gray_100 = reconstruct(V1_W_100, V1_y_100, alpha)
ax1.imshow(reconst_gray_100, 'gray')
ax1.set_title("{num_cell} number of cells".format(num_cell = num_cell_100))
ax1.axis("off")
# ## Reconstruction with 300 number of cells grayscaled
reconst_gray_300 = reconstruct(V1_W_300, V1_y_300, alpha)
ax2.imshow(reconst_gray_300, 'gray')
ax2.set_title("{num_cell} number of cells".format(num_cell = num_cell_300))
ax2.axis("off")
plt.subplots_adjust(top=1.35)
plt.show()
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
## Reconstruction with 100 number of cells
reconst_color_100 = color_experiment(small_img_arr, num_cell_100, cell_size = cell_size, blob_size = blob_size, alpha = alpha, observation = 'V1')
ax1.imshow(reconst_color_100)
ax1.set_title("{num_cell} number of cells".format(num_cell = num_cell_100))
ax1.axis("off")
## Reconstruction with 300 number of cells
reconst_color_300 = color_experiment(small_img_arr, num_cell_300, cell_size = cell_size, blob_size = blob_size, alpha = alpha, observation = 'V1')
ax2.imshow(reconst_color_300)
ax2.set_title("{num_cell} number of cells".format(num_cell = num_cell_300))
ax2.axis("off")
plt.show()
# V1 observation on big image
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
fig.suptitle("V1 Grascaled Reconstruction")
## Reconstruction with 100 number of cells grayscaled
reconst_gray_100 = large_img_experiment(big_img_arr_gray, num_cell_100, cell_size, blob_size, alpha = alpha, observation = 'V1')
ax1.imshow(reconst_gray_100, 'gray')
ax1.set_title("{num_cell} number of cells".format(num_cell = num_cell_100))
ax1.axis("off")
## Reconstruction with 300 number of cells grayscaled
reconst_gray_300 = large_img_experiment(big_img_arr_gray, num_cell_300, cell_size, blob_size, alpha = alpha, observation = 'V1')
ax2.imshow(reconst_gray_300, 'gray')
ax2.set_title("{num_cell} number of cells".format(num_cell = num_cell_300))
ax2.axis("off")
plt.subplots_adjust(top=1.35)
plt.show()
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 10))
fig.suptitle("V1 Color Reconstruction")
## Reconstruction with 100 number of cells
reconst_color_100 = large_img_experiment(big_img_arr, num_cell_100, cell_size, blob_size, alpha = alpha, color = True, observation = 'V1')
ax1.imshow(reconst_color_100)
ax1.set_title("{num_cell} number of cells".format(num_cell = num_cell_100))
ax1.axis("off")
## Reconstruction with 300 number of cells
reconst_color_300 = large_img_experiment(big_img_arr, num_cell_300, cell_size, blob_size, alpha = alpha, color = True, observation = 'V1')
ax2.imshow(reconst_color_300)
ax2.set_title("{num_cell} number of cells".format(num_cell = num_cell_300))
ax2.axis("off")
plt.subplots_adjust(top=1.35)
plt.show()