-
Notifications
You must be signed in to change notification settings - Fork 0
/
dna_body_map_kinect
executable file
·443 lines (252 loc) · 11.5 KB
/
dna_body_map_kinect
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/python
# import opencv
import cv2
import numpy as np
import sys
import freenect
# import opengl stuff
from OpenGL.GL import *
from OpenGL.GLU import *
import random
from math import * # trigonometry
import Image
# 0
DEFAULT = 48
# #1
# BACKGROUND = 49
2
#2
CALIBRATE = 50
#3
PROJECT = 51
mode = DEFAULT
projected = []
real = []
# empty callback function, necessary for the trackbars
def no_func(i):
pass
# mouse callback function
def draw_translation_calibration(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
print "click"
print x,
drawing = True
ix = x
iy = y
projected.append([x,y])
elif event == cv2.EVENT_MOUSEMOVE:
pass
elif event == cv2.EVENT_LBUTTONUP:
real.append([x,y])
# # create video capture
# cap = cv2.VideoCapture(0)
# cap.set(cv2.cv.CV_CAP_PROP_FPS, 60)
# # this background subtractor is supposedly la leche but is not recognized. Problem with CV version?
# # fgbg = cv2.createBackgroundSubtractorMOG()
# # read first frame and set default background image to 0s
# _,frame = cap.read()
# frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
# # background = np.zeros(frame.shape,np.uint8)
# # make wndow for trackbars for various variables
# cv2.namedWindow('vars')
# # make viz windows
# cv2.namedWindow('bw', cv2.WINDOW_AUTOSIZE)
# cv2.namedWindow('frame', cv2.WINDOW_AUTOSIZE)
# cv2.namedWindow('thresh', cv2.WINDOW_AUTOSIZE)
# for window that will be mapping, enable openGL
# cv2.namedWindow('mapping', cv2.WINDOW_AUTOSIZE, cv2.WINDOW_OPENGL)
# cv2.namedWindow('calibration', cv2.WND_PROP_FULLSCREEN)
cv2.namedWindow('calibration', cv2.WINDOW_AUTOSIZE)
# cv2.resizeWindow('calibration', 1024, 768)
cv2.setMouseCallback('calibration', draw_translation_calibration)
# cv2.namedWindow('Depth', cv2.WINDOW_AUTOSIZE)
cv2.namedWindow('Thresh', cv2.WINDOW_AUTOSIZE)
# cv2.namedWindow('Proj', cv2.WINDOW_AUTOSIZE)
near_threshold = 0
far_threshold = 100
cv2.createTrackbar('near_threshold', 'Thresh', 0, 2048, no_func)
cv2.createTrackbar('far_threshold', 'Thresh', 0, 2048, no_func)
# projected_image = np.zeros((480,680,3), np.uint8)
image = cv2.imread('/home/lala/fun/dev/BOS_installation/genome2.png')
projected_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# projected_image = cv2.resize(projected_image, (1024, 768))
proj_img_size = np.zeros((768,1024,3), np.uint8)
color1 = np.zeros((480,640,3), np.uint8)
color1[::] = (255,0,0) # B, G, R
color2 = np.zeros((480,640,3), np.uint8)
color2[::] = (255,0,0) # B, G, R
color3 = np.zeros((480,640,3), np.uint8)
color3[::] = (255,0,0) # B, G, R
kernel = np.ones((5,5),np.float32)/25
blur = False
# # adjustable variables
# # cutoff for thresholding
# cutoff = 100
# cv2.createTrackbar('thresh_cutoff', 'vars', cutoff, 255, no_func)
# # approx_frac = 10
# # cv2.createTrackbar('approx_frac', 'vars', approx_frac, 500, no_func)
# # kernel size for closing
# kernel_size = 5
# cv2.createTrackbar('kernel_size', 'Thresh', kernel_size, 500, no_func)
# loop while waiting for background image capture
while(1):
key = cv2.waitKey(33)
if key in [DEFAULT, CALIBRATE, PROJECT]:
mode = key
print mode
if mode == CALIBRATE:
projected = []
real = []
# cv2.resizeWindow('calibration', 1024, 768)
cv2.setWindowProperty('calibration', cv2.WND_PROP_FULLSCREEN, cv2.cv.CV_WINDOW_FULLSCREEN)
if mode == PROJECT:
cv2.namedWindow('mapping')
# cv2.resizeWindow('calibration', 1024, 768)
# cv2.namedWindow('mapping', cv2.WND_PROP_FULLSCREEN)
# cv2.setWindowProperty('mapping', cv2.WND_PROP_FULLSCREEN, cv2.cv.CV_WINDOW_FULLSCREEN)
depth, timestamp = freenect.sync_get_depth()
# projected_image[::] = (255,0,0) # B, G, R
# _,frame = cap.read()
# if mode == BACKGROUND:
# background = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
# background = cv2.blur(background,(10,10))
# cv2.imshow('bck', background)
# # key press space takes background image and breaks
# if cv2.waitKey(33) == 32:
# mode = DEFAULT
# # KILL BCK WINDOW
if mode == CALIBRATE:
cv2.setWindowProperty('calibration', cv2.WND_PROP_FULLSCREEN, cv2.cv.CV_WINDOW_FULLSCREEN)
for (ix, iy), (x,y) in zip(projected, real):
cv2.circle(db_masked_frame,(ix,iy),5,(0,0,255),-1)
cv2.line(db_masked_frame, (ix,iy), (x,y) , (0,0,255), 3)
cv2.circle(db_masked_frame,(x,y),5,(0,0,255),-1)
# cv2.resizeWindow('calibration', 1024, 768)
cv2.imshow('calibration', db_masked_frame)
if cv2.waitKey(33) == 32:
mode = DEFAULT
# TODO CALCULATE HOMOGRAPHY MATRIX
# KILL CALIBRATION WINDOW
# this mode is default, and can be used to reset from another mode
if mode == DEFAULT or mode == PROJECT:
# naive background substraction: subtract first frame
# bw = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
# bw = cv2.blur(bw, (10,10))
# bw = bw - background
# # fancy background subtraction not wrking now
# # bw = frame
# # bw2 = fgbg.apply(bw)
near_threshold = cv2.getTrackbarPos('near_threshold','Thresh')
far_threshold = cv2.getTrackbarPos('far_threshold','Thresh')
# print np.max(depth)
# print np.min(depth)
#print depth.shape
#depth_scale = depth - 800
# depth_scale = (255.0 / 2407.0) * depth
# cv2.imshow('Depth', depth_scale)
thresh = 255 * np.logical_and(depth > near_threshold,
depth < far_threshold)
thresh = thresh.astype(np.uint8)
thresh = cv2.resize(thresh, (1024, 768))
# cv2.imshow('bw',bw)
# operations on images modify the source image, so make a copy to operate on
# cutoff = cv2.getTrackbarPos('thresh_cutoff', 'vars')
# ret,thresh = cv2.threshold(bw.copy(),cutoff,255,cv2.THRESH_BINARY)
# # cv2.imshow('thresh',thresh)
# # reduce back blobs within white profile with "closing"
# # kernel size influences what size region to close
# kernel_size = cv2.getTrackbarPos('kernel_size', 'Thresh')
# if kernel_size < 1:
# kernel_size = 1
# kernel = np.ones((kernel_size,kernel_size),np.uint8)
# # closing = cv2.morphologyEx(thresh.copy(), cv2.MORPH_CLOSE, kernel)
# cv2.imshow('closing',closing)
# # find contours in the threshold image
# contours,hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# if len(contours) > 0:
# print "CONTOURS"
# # sort contours by decreasing area
# sorted_contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
# # this is to smooth contours -- not needed i think
# # approx_frac = cv2.getTrackbarPos('approx_frac', 'vars')
# # for top four contours:
# for cnt in sorted_contours[:3]:
# # calculate moments and centroid, and draw a circle at centroid
# M = cv2.moments(cnt)
# if M['m00'] > 0 and M['m00'] > 0:
# cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
# cv2.circle(projected_image,(cx,cy),5,255,-1)
# # calculate smoothed contour
# # epsilon = (approx_frac / 10000.0) * cv2.arcLength(cnt,True)
# # approx = cv2.approxPolyDP(cnt,epsilon,True)
# # cv2.drawContours(frame, [approx], -1, (0,0,255), 2)
# # calculate bouding rectangle of best contour
# x,y,w,h = cv2.boundingRect(sorted_contours[0])
# cv2.rectangle(projected_image,(x,y),(x+w,y+h),(0,255,0),2)
# # draw best contour over frame in red
# cv2.drawContours(projected_image, sorted_contours[0], -1, (0,0,255), 2)
#### COLORED IMG VERSION
# for x,y in np.ndindex(thresh.shape):
# if thresh[x,y]:
# projected_image[x,y] = (255,0,0)
# else:
# projected_image[x,y] = (0,0,0)
##### END COLORED
if mode == DEFAULT:
cv2.imshow('Thresh', thresh)
## make mask to mask in black all the image that is not inside the contour:
# # make matrix of zeros (black) same size as frame
# mask = np.zeros(thresh.shape,np.uint8)
# # draw best contour in white on black mask, and set to fill with thickness=-1
# cv2.drawContours(mask,[sorted_contours[0]],0,(255,255,255),thickness=-1)
# # invert the mask, now profile is balck (0) and background is white (255)
# mask_inv = cv2.bitwise_not(mask)
# # add mask to frame, pixels inside the profile are unchanged (+0), those in the background are maxed out (+255)
# masked_frame = cv2.add(frame, mask_inv)
# # subtract mask from frame, pixels inside the mask are unchanged (-0), those in the background go to 0 (255-255)
# db_masked_frame = masked_frame - mask_inv
# # cv2.imshow('frame_masked',masked_frame)
# cv2.imshow('db_masked_frame', db_masked_frame)
mask_inv = cv2.bitwise_not(thresh)
if cv2.waitKey(33) == 98:
blur = not blur
if blur:
print "blur"
blurred_img = cv2.filter2D(projected_image,-1,kernel)
masked_frame = cv2.add(blurred_img, mask_inv)
else:
masked_frame = cv2.add(projected_image, mask_inv)
# add mask to frame, pixels inside the profile are unchanged (+0), those in the background are maxed out (+255)
# masked_frame = cv2.add(projected_image, mask_inv)
# subtract mask from frame, pixels inside the mask are unchanged (-0), those in the background go to 0 (255-255)
db_masked_frame = masked_frame - mask_inv
# cv2.imshow('Proj', db_masked_frame)
# ############## BW ###################
# ############## END BW ##############
############### COLOR ################
# masked_frame_color = cv2.cvtColor(projected_image,cv2.COLOR_GRAY2RGB)
# mask_inv_color = cv2.cvtColor(mask_inv,cv2.COLOR_GRAY2RGB)
# db_masked_frame_color = masked_frame_color - mask_inv_color
# cv2.imshow('Proj', db_masked_frame_color)
################ END COLOR ###############
# db_masked_frame_color = cv2.cvtColor(db_masked_frame,cv2.COLOR_GRAY2RGB)
# print len(db_masked_frame_color)
# print len(db_masked_frame_color[0])
# print len(color1)
# print len(color1[0])
# db_masked_frame_tint = cv2.addWeighted(db_masked_frame_color,0.7,color1,0.3,0)
# #display various windows
# cv2.imshow('frame',frame)
if mode == PROJECT:
# calculate homographied contour and project
# matrix = cv2.getPerspectiveTransform(np.array(projected), np.array(real))
# have to convert coordinates to float and list to np array
p = np.float32(projected)
r = np.float32(real)
matrix, mask = cv2.findHomography(p, r)
projection = cv2.warpPerspective(db_masked_frame, matrix, (1024,768))
cv2.imshow('mapping', projection)
# if key pressed is 'Esc', exit the loop
if cv2.waitKey(33)== 27:
cv2.destroyAllWindows()
sys.exit(0)