forked from Willy8m/TFG-Single-Pixel-Imaging
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_Image_capture.py
More file actions
213 lines (127 loc) · 5 KB
/
02_Image_capture.py
File metadata and controls
213 lines (127 loc) · 5 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
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 16 09:39:39 2023
@author: guill
"""
import whdynamic as wh
import numpy as np
import cv2
import time
from src import CameraWatcher
import matplotlib.pyplot as plt
import random
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
# =============================================================================
# # FUNCTIONS
# =============================================================================
def take_image():
# take image
img_buffer = cams.get_image(0)
# load the image
Jpeg_file = Image.open(img_buffer)
# convert image to numpy array and compute average
data = np.asarray(Jpeg_file)
image = (data[:,:,0] + data[:,:,1] + data[:,:,2]) // 3
return image
# =============================================================================
# # INPUTS
# =============================================================================
''' RECORDA QUE TENS deg AL NOM D'ARXIU '''
order = 7 # resolution = 2 ** (order - 1)
save_file = True
fps_in = 5 # camera handles around 6.5 fps max
delay = 0
degrees = 10 # Real life cam-projector displacement
# shown_fraction = 1 # Still not implemented with dynamic wh creation
# =============================================================================
# # STARTUP
# =============================================================================
if not save_file:
order = 3 # tests only
# DEFINE PARAMETERS
W = wh.W(order) # Matrix for the hadamard_ij() function
res = 2 ** (order - 1) # resolution
print('Expected runtime =', "{:.2f}".format((1.8 * 2/fps_in * res ** 2) / 60), 'minutes')
hs = res # hadamard size
mw = hs * 16 // 9 # matrix width (squares to fill the screen)
matrix_i = np.uint8(np.zeros((hs, mw))) # generate matrix to be shown
# START FULLSCREEN WINDOW
cv2.namedWindow('crame',cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty('crame',cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
if save_file:
cv2.waitKey(15000)
# START CAMERA
cams = CameraWatcher(1)
cams.watch_camera(("192.168.1.83", 8000), shutter_speed=300000)
# GET CAMERA IMAGE INFO
data = take_image()
h, w = data.shape
# TEST FIRST 17 IMAGES
Images_0 = np.empty([h, w, 17], dtype=np.uint8())
Images_1 = np.empty([h, w, 17], dtype=np.uint8())
# =============================================================================
# # DATA GETHERING
# =============================================================================
reconstruction = np.zeros((res, res), dtype=np.float64())
t_ini = time.time()
c = 0
for invert in [False, True]:
for i in range(res):
for j in range(res):
# Create matrix
wh_matrix = 255 * wh.hadamard_ij(W, i, j, invert)
# Fill central square and project matrix
matrix_i[:,(mw - hs) // 2 : (mw + hs) // 2] = wh_matrix
cv2.imshow('crame', matrix_i)
# Important pause before taking the image
cv2.waitKey(1000//fps_in)
# TAKE IMAGE
image = take_image()
if c<17:
# Save first 17 images for debugging
Images_0[:,:,c] = image
c+=1
# Calculate intensity
intensity = image[:,(w-h)//2:(w-h)//2+h].mean()
# Account for the delay of the captures
ii, jj = i, (j - delay) % res
if (j - delay) < 0:
ii -= 1
ii, jj = (ii < 0) * 0 + (ii >= 0) * ii, (ii < 0) * 0 + (ii >= 0) * jj
# Add to the reconstructed image
reconstruction += intensity * wh.hadamard_ij(W, ii, jj, invert)
# Close camera
cams.close()
# Time metrics
t_fin = time.time()
t_tot = t_fin - t_ini
fps = 2 * res ** 2 / t_tot
# Save file
if save_file:
filename = str('reconstructions/' + str(int(time.time())) + '_x'
+ str(res) + '_fps' + str(int(fps_in)) + '_del'
+ str(delay) + '_deg' + str(degrees))
np.save(filename, reconstruction)
# =============================================================================
# # PLOTS & OUTPUT
# =============================================================================
# Print time metrics
print("{:.2f}".format(fps_in), 'fps in')
print("{:.2f}".format(fps), 'fps out')
print("Runtime = ", "{:.2f}".format(t_tot))
# Show reconstruction
plt.figure()
plt.imshow(reconstruction, cmap='gray')
plt.axis('off')
# Show first 17 images
plt.figure()
for n in range(16):
plt.subplot(int(np.sqrt(2 * 17) + 1), int(np.sqrt(2 * 17) + 1), 2 * n + 1)
plt.imshow(Images_0[:,:,n], cmap='gray')
plt.axis('off')
''' FIX PLOT '''
plt.subplot(int(np.sqrt(2 * 17) + 1), int(np.sqrt(2 * 17) + 1), 2 * n + 2)
plt.imshow(wh.hadamard_ij(W, n // 4, n % 4, invert=False) , cmap='gray')
plt.axis('off')
plt.tight_layout()