-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalProject.py
More file actions
305 lines (216 loc) · 8.77 KB
/
Copy pathFinalProject.py
File metadata and controls
305 lines (216 loc) · 8.77 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
########################################################################
#
# File: FinalProject.py
# Author: S. B. Nashed, A. L. Steele
# Date: May 2015
#
########################################################################
import cv2
import numpy
import sys
import struct
import math
import bounce_mod
sys.path.append('../examples')
import cvk2
w = 1880
h = 1024
black = (0,0,0)
white = (255,255,255)
blue = (255,0,0)
red = (0,0,255)
yellow = (0, 255, 255)
########################################################################
# This is a helper function that reads a frame from camera, converts to grayscale
# and then check if frame is ok
def getFrame():
ok, frame = capture.read()
# The image is converted to grayscale
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY).astype('uint8')
#check if ok
if not ok or frame is None:
print 'No frames in video'
sys.exit(1)
return frame
########################################################################
# This is a helper function that takes an image, modifies it by adding
# some text, and displays it on the screen
def labelAndWaitForKey(frame, text1, text2):
# Get the image height, and width and make a temp copy to edit text
h = frame.shape[0]
w = frame.shape[1]
# Note that even though shapes are represented as (h, w), pixel
# coordinates below are represented as (x, y). Confusing!
cv2.putText(frame, "Interactive Physics-Based Sandbox", (w/4, h/6),
cv2.FONT_HERSHEY_SIMPLEX, 1.0,
(0,0,0), 3, cv2.CV_AA)
cv2.putText(frame, "Interactive Physics-Based Sandbox", (w/4, h/6),
cv2.FONT_HERSHEY_SIMPLEX, 1.0,
(255,255,255), 1, cv2.CV_AA)
cv2.putText(frame, text1, (w/4, 2*h/6),
cv2.FONT_HERSHEY_SIMPLEX, 2.0,
(0,0,0), 6, cv2.CV_AA)
cv2.putText(frame, text1, (w/4, 2*h/6),
cv2.FONT_HERSHEY_SIMPLEX, 2.0,
(255,255,255), 2, cv2.CV_AA)
cv2.putText(frame, text2, (w/4, 3*h/6),
cv2.FONT_HERSHEY_SIMPLEX, 2.0,
(0,0,0), 6, cv2.CV_AA)
cv2.putText(frame, text2, (w/4, 3*h/6),
cv2.FONT_HERSHEY_SIMPLEX, 2.0,
(255,255,255), 2, cv2.CV_AA)
cv2.imshow('Final Project', frame)
########################################################################
# This is a helper function that calculates a homography by mapping
# out a series of points and comparing their location in the camera frame
# and the projection frame.
def establishHomography(w,h,color,frame):
cameraDisplayCircle = []
calibrationProjection = []
# array that stores the centroids of each object.
calibrationCentroids=[]
for i in range(9):
x = int((1.0/6)*w + (1.0/3)*w*(i%3))
y = int((1.0/6)*h + (1.0/3)*h*int(i/3))
calibrationProjection.append([x,y])
new = blank.copy()
cv2.circle( new, ( x, y), 40, color, -1)
cv2.imshow("Final Project",new)
# Delay for 500ms and get a key
k = cv2.waitKey(500)
# Get the frame.
ok, frame = capture.read(frame)
#newFrame = frame - baseFrame
newFrame = cv2.absdiff(frame,baseFrame)
# An identity matrix of size 8x8 is created in order to determine
# the size of the morphological transformation
kernel = numpy.ones((8,8),numpy.uint8)
morph = cv2.morphologyEx(newFrame,cv2.MORPH_CLOSE,kernel)
# An identity matrix of size 5x5 is created in order to determine
# the size of the dilation
kernel = numpy.ones((15,15),numpy.uint8)
morph = cv2.dilate(morph,kernel,iterations=1)
# The image is converted to grayscale
morph = cv2.cvtColor(morph, cv2.COLOR_RGB2GRAY).astype('uint8')
# The image is thresholded to remove remaining noise
cv2.threshold(morph,100,255, cv2.THRESH_BINARY,morph)
# An identity matrix of size 8x8 is created in order to determine
# the size of the dilation
kernel = numpy.ones((8,8),numpy.uint8)
morph = cv2.erode(morph,kernel,iterations=1)
temp = morph.copy()
# The outlines of the objects are determined
contours = cv2.findContours(temp, cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
# Iterates through each object found in frame.
for j in range(len(contours[0])):
# Compute some statistics about this contour.
info = cvk2.getcontourinfo(contours[0][j])
# Mean location of every objects' centroid.
mu = info['mean']
calibrationCentroids.append(mu)
cameraDisplayCircle.append(morph)
# Bail if none.
if not ok or frame is None:
break
calibrationCentroids = numpy.asarray(calibrationCentroids, dtype='float32')
calibrationProjection = numpy.asarray(calibrationProjection, dtype='float32')
M = cv2.findHomography(calibrationCentroids,calibrationProjection,cv2.RANSAC)[0]
return M
########################################################################
# This is a helper function that rectifies an image.
# The image is then warped to display the region of interest.
def rectifyImage(img, w, h, M):
# Construct an array of points on the border of the image.
p = numpy.array( [ [[ 0, 0 ]],
[[ w, 0 ]],
[[ w, h ]],
[[ 0, h ]] ], dtype='float32' )
# Send the points through the transformation matrix.
pp = cv2.perspectiveTransform(p, M)
# Compute the bounding rectangle for all points (note this gives
# integer coordinates).
box = cv2.boundingRect(pp)
dims = box[2:4]
# Warp the image to the destination in the temp image.
rectifiedImage = cv2.warpPerspective(img, M, tuple(dims))
return rectifiedImage
########################################################################
# This is a helper function that outlines user created lines
# on the board.
def userDraw(rectifiedBase, w, h , M, sim):
while 1:
frame = getFrame()
# Warp the image to the destination in the temp image.
frame = rectifyImage(frame, w, h, M)
newFrame = cv2.absdiff(rectifiedBase,frame)
# The image is thresholded to remove remaining noise
cv2.threshold(newFrame,35,255, cv2.THRESH_BINARY,newFrame)
# An identity matrix of size 5x5 is created in order to determine
# the size of the dilation
kernel = numpy.ones((6,6),numpy.uint8)
newFrame = cv2.erode(newFrame,kernel,iterations=1)
newFrame = cv2.dilate(newFrame,kernel,iterations=1)
#isolate region of interest in newFrame
roi = newFrame[0:h,0:w]
#update the scene in ball sim
sim.updateScene(roi)
#run ball sim and update ball positions
sim.run()
# Try to get an integer argument:
try:
device = int(sys.argv[1])
del sys.argv[1]
except (IndexError, ValueError):
device = 0
# If we have no further arguments, open the device. Otherwise, get the
# filename.
if len(sys.argv) == 1:
capture = cv2.VideoCapture(device)
if capture:
print 'Opened device number', device, '- press Esc to stop capturing.'
else:
capture = cv2.VideoCapture(sys.argv[1])
if capture:
print 'Opened file', sys.argv[1]
# Bail if error.
if not capture:
print 'Error opening video capture!'
sys.exit(1)
#display blank black screen
blank = numpy.empty(shape=(h,w))
blank.fill(0)
cv2.imshow("Final Project",blank)
cv2.moveWindow("Final Project",0,0)
rgbArray = numpy.zeros((h,w,3), 'uint8')
# Delay for .5 seconds, let camera callibrate
k = cv2.waitKey(500)
ok, frame = capture.read()
baseFrame = frame.copy()
#establish homography
M = establishHomography(w,h,white,frame)
cv2.imshow("Final Project",blank)
k = cv2.waitKey(1000)
frame = getFrame()
# Warp the image to the destination in the temp image.
rectifiedBase = rectifyImage(frame, w, h, M)
labelAndWaitForKey(rgbArray,"Final Project", "Samer Nashed & Andrew Steele")
cv2.waitKey(1000)
cv2.imshow("Final Project",blank)
k = cv2.waitKey(500)
frame = getFrame()
# Warp the image to the destination in the temp image.
frame = rectifyImage(frame, w, h, M)
newFrame = cv2.absdiff(rectifiedBase,frame)
# The image is thresholded to remove remaining noise
cv2.threshold(newFrame,30,255, cv2.THRESH_BINARY,newFrame)
# An identity matrix of size 6x6 is created in order to determine
# the size of the erosion and dilation
kernel = numpy.ones((6,6),numpy.uint8)
#image is eroded and dilated to get rid of noise
newFrame = cv2.erode(newFrame,kernel,iterations=1)
newFrame = cv2.dilate(newFrame,kernel,iterations=1)
#isolate region of interest in newFrame
roi = newFrame[0:h,0:w]
#create BallSim object
sim = bounce_mod.BallSim(roi,5)
userDraw(rectifiedBase, w, h , M, sim)