-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcameraLocalisation2D.py
More file actions
executable file
·713 lines (547 loc) · 28.4 KB
/
cameraLocalisation2D.py
File metadata and controls
executable file
·713 lines (547 loc) · 28.4 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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
import cv2 as cv
import numpy as np
import time
import math
import colorsys
def draw_circle(event,x,y,flags,param):
if event == cv.EVENT_LBUTTONDBLCLK:
# cv.circle(img,(x,y),100,(255,0,0),-1)
# print hsvFrame[y,x]
pass
class FishEyeCamera:
def __init__(self, deviceID=0):
# The fish-eye cameras support 720p and their angle-of-view is affected by the resolution
# self.height = 720
# self.width = 1280
self.height = 480
self.width = 640
# Open camera
self.captureVideo(deviceID)
if self.height ==720:
# Camera calibration parameters obtained 2016-08-11 (by Mohsen and Matthew) using captureImages.py and calibrate.py
self.cameraMatrix = np.array([[416.25456303, 0., 663.64459394], [0., 387.2264034, 380.49903696], [0., 0., 1.]])
self.distortionCoefficients = np.array([2.00198060e-01, -2.28216265e-01, 2.26068631e-04, -5.00177586e-04, 5.84619782e-02])
self.newCameraMatrix, _ = cv.getOptimalNewCameraMatrix(self.cameraMatrix,
self.distortionCoefficients,
(self.width, self.height),
1,
(self.width, self.height))
elif self.height == 480:
# Camera calibration parameters obtained 2016-08-11 (by Mohsen and Matthew) using captureImages.py and calibrate.py
self.cameraMatrix = np.array([[254.35663264, 0., 331.98117752], [0., 238.03212471, 251.28182303], [0., 0., 1.]])
self.distortionCoefficients = np.array([0.16687251, -0.18482639, -0.00030592, 0.00256979, 0.04673033])
self.newCameraMatrix, _ = cv.getOptimalNewCameraMatrix(self.cameraMatrix,
self.distortionCoefficients,
(self.width, self.height),
1,
(self.width, self.height))
# TODO: The calibration parameters should really allow the undistorted image to be in real-world units.
# For the meantime, we need to be able to convert from pixels to real-world units
self.pixelsPerMillimeter = 0.5 # As at 2016-08-22
def captureVideo(self, deviceID=0):
'''Opens the camera (specified by deviceID). Sets the resolution to 720p (1280 x 720).'''
self.camera = cv.VideoCapture(deviceID)
self.camera.set(cv.CAP_PROP_FRAME_WIDTH, self.width)
self.camera.set(cv.CAP_PROP_FRAME_HEIGHT, self.height)
ret, frame = self.camera.read()
h, w = frame.shape[:2]
assert ret
assert h == self.height and w == self.width
def getFrame(self):
'''Returns a frame of video flipped in both horizontal and vertical axes.'''
ret, frame = self.camera.read()
frame = cv.flip(cv.flip(frame, 1), -1)
return frame
def getUndistortedFrame(self):
frame = self.getFrame()
undistortedFrame = cv.undistort(frame, self.cameraMatrix, self.distortionCoefficients, None, self.newCameraMatrix)
return undistortedFrame
class RobotDetector:
def __init__(self, numberOfColours = 3):
self.numberOfColours = numberOfColours
# Setup SimpleBlobDetector parameters.
def createDetectorParameters():
params = cv.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 10
params.maxThreshold = 200
# Filter by Colour
params.filterByColor = True
params.blobColor = 255
# Filter by Area.
params.filterByArea = True
params.minArea = 150
# Filter by Circularity
params.filterByCircularity = False
params.minCircularity = 0.1
# Filter by Convexity
params.filterByConvexity = False
params.minConvexity = 0.87
# Filter by Inertia
params.filterByInertia = False
params.minInertiaRatio = 0.01
return params
# Create SimpleBlobDetector (assumes OpenCV version 3)
params = createDetectorParameters()
self.detector = cv.SimpleBlobDetector_create(params)
self.colourHuesDegrees = [hue for hue in range(0,255,int(255/self.numberOfColours))]
self.colourHeusBandDegrees = [10]*numberOfColours
class Robot:
def __init__(self, colours):
self.colours = colours
self.location = None
self.orientation = None
def __str__(self):
return "colors: "+str(self.colours)+" location: "+str(self.location)+" orientation: "+str(self.orientation)
# Calculate robot indicies
assert (numberOfColours >= 2)
self.robots = []
for c1 in range(numberOfColours):
for c2 in range(c1+1, numberOfColours):
self.robots.append(Robot((c1,c2)))
def getMeanAndStdDevFromColourSamples(self, colourSamples):
'''From a list of colourSamples, find the mean and standard deviation using a delta from the first sampled
colour'''
assert len(colourSamples) > 0
# Source colour is the first of the colour samples
sourceColour = int(colourSamples[0])
# Go through the samples and delta between the current sample and the source. There are two deltas, one in each
# direction (possibly looping over 360 degrees).
deltaUpColourSamples = [(int(x) - sourceColour)%256 for x in colourSamples]
deltaDownColourSamples = [-((sourceColour - (int(x)))%256) for x in colourSamples]
# Find the minimum of the two deltas
deltaColourSamples = []
for i in range(len(deltaDownColourSamples)):
if abs(deltaDownColourSamples[i]) <= abs(deltaUpColourSamples[i]):
deltaColourSamples.append(deltaDownColourSamples[i])
else:
deltaColourSamples.append(deltaUpColourSamples[i])
# Find the mean and standard deviation of the (minimum) deltas
meanDelta = float(sum(deltaColourSamples)) / len(deltaColourSamples)
# Calculate the mean colour.
meanColour = (sourceColour + meanDelta) % 256
sdDelta = math.sqrt(float(sum([(x - meanDelta) ** 2 for x in deltaColourSamples])) / len(deltaColourSamples))
# Convert the colours (from uint8) to degrees
meanColourDegrees = float(meanColour) / 255 * 360
sdColourDegrees = float(sdDelta) / 255 * 360
return (meanColourDegrees, sdColourDegrees)
def calibrateColour(self, camera, bandWidthInStdDevs = 3):
self.colourHuesDegrees = []
self.colourHeusBandDegrees = []
print(("Calibrate the",self.numberOfColours,"colours. Click on colour samples, press space when done for each colour."))
def mouseCallbackGetColour(event, x, y, flags, param):
if event == cv.EVENT_LBUTTONDOWN:
colourValue = hsvFrame[y, x]
print(colourValue)
colourSamples.append(colourValue[0])
for i in range(self.numberOfColours):
colourSamples = []
while True:
# Get undistorted frame
undistortedFrame = camera.getUndistortedFrame()
# Convert to HSV
hsvFrame = cv.cvtColor(undistortedFrame, cv.COLOR_BGR2HSV_FULL)
windowTitle = "Calibrate Colour #"+str(i)
cv.imshow(windowTitle, undistortedFrame)
cv.setMouseCallback(windowTitle, mouseCallbackGetColour)
key = cv.waitKey(1)
if key == 1048608: # space
# Calculate mean and standard deviation of colour samples
meanColourDegrees, sdColourDegrees = self.getMeanAndStdDevFromColourSamples(colourSamples)
print(("mean colour (degrees): ",meanColourDegrees, "sd:", sdColourDegrees))
self.colourHuesDegrees.append(meanColourDegrees) # convert to degrees
self.colourHeusBandDegrees.append(sdColourDegrees * bandWidthInStdDevs)
cv.destroyWindow(windowTitle)
break
def displayCalibration(self):
print(("colourHuesDegrees: ", self.colourHuesDegrees))
print(("colourHeusBandDegrees: ", self.colourHeusBandDegrees))
def setCalibration(self, colourHuesDegrees, colourHeusBandDegrees):
assert len(colourHuesDegrees) == self.numberOfColours
assert len(colourHeusBandDegrees) == self.numberOfColours
self.colourHuesDegrees = colourHuesDegrees
self.colourHeusBandDegrees = colourHeusBandDegrees
def findColouredPixels(self, hsvFrame, hueDegrees, hueBandDegrees=20):
if hueDegrees - hueBandDegrees > 0 and hueDegrees + hueBandDegrees < 360:
# The band does not wrap around 0/360 degrees
hueLowerBoundInt = np.uint8((float(hueDegrees - hueBandDegrees) / 360) * 255)
hueUpperBoundInt = np.uint8((float(hueDegrees + hueBandDegrees) / 360) * 255)
lowerBound = np.array([hueLowerBoundInt, 50, 50])
upperBound = np.array([hueUpperBoundInt, 255, 255])
mask = cv.inRange(hsvFrame, lowerBound, upperBound)
# maskedFrame = cv.bitwise_and(frame, frame, mask=mask)
return mask
else:
# The band wraps around zero, so deal with it in two parts
# First deal with the portion of the band near zero degrees
hueZeroBoundInt = np.uint8((float((hueDegrees + hueBandDegrees)%360) / 360) * 255)
zero = np.array([0, 50, 50])
zeroBound = np.array([hueZeroBoundInt, 255, 255])
maskZero = cv.inRange(hsvFrame, zero, zeroBound)
# Next, deal with the portion of the band zero 360 degrees
hue360BoundInt = np.uint8((float((hueDegrees - hueBandDegrees)%360) / 360) * 255)
limit360Bound = np.array([hue360BoundInt, 50, 50])
limit360 = np.array([255, 255, 255])
mask360 = cv.inRange(hsvFrame, limit360Bound, limit360)
# The final mask is made of both the near-zero and near-360 masks
mask = cv.bitwise_or(maskZero, mask360)
return mask
def findColouredBlobs(self, frame, debug = False):
# Convert to HSV
# global hsvFrame
hsvFrame = cv.cvtColor(frame, cv.COLOR_BGR2HSV_FULL) #
masks = []
self.keypoints = []
for i in range(self.numberOfColours):
masks.append(self.findColouredPixels(hsvFrame, self.colourHuesDegrees[i], self.colourHeusBandDegrees[i]))
self.keypoints.append(self.detector.detect(masks[i]))
if debug:
frameWithKeypoints = frame
for i in range(self.numberOfColours):
# Show mask
maskWindowName = "Mask #"+str(i)
cv.imshow(maskWindowName, masks[i])
# cv.moveWindow(maskWindowName, 0, 0)
# Show keypoints
KeyPointColour = [255 * cl for cl in colorsys.hsv_to_rgb(float(self.colourHuesDegrees[i]) / 360, 1, 1)]
KeyPointColour.reverse()
KeyPointColour = tuple(KeyPointColour)
frameWithKeypoints = cv.drawKeypoints(frameWithKeypoints, self.keypoints[i], np.array([]), KeyPointColour,
cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv.imshow("Key Points", frameWithKeypoints)
def findRobot(self, robot, camera, debug=False):
'''Find robot assuming an unknown current location. Scan the whole field.'''
# Go through all the possible keypoint pairs. Expected distance between the two colour patches is 69mm
# (each of the squares has a side length of 49mm). Rank all the possibilities based on how far away they
# are from this ideal distance. Select the pair that is the closest.
distanceBetweenMarkers = 69 #mm
colour1Keypoints = self.keypoints[robot.colours[0]]
colour2Keypoints = self.keypoints[robot.colours[1]]
minimumError = None # start with undefined error
minimumErrorKeypoints = None
for keypoint1 in colour1Keypoints:
for keypoint2 in colour2Keypoints:
# Calculate distance between the two keypoints
distance = math.sqrt( (keypoint1.pt[0] - keypoint2.pt[0])**2 + \
(keypoint1.pt[1] - keypoint2.pt[1])**2 )
# Convert distance in pixels to real-world distance
distance /= camera.pixelsPerMillimeter
error = abs(distance - distanceBetweenMarkers)
if minimumError is None or error < minimumError:
minimumError = error
minimumErrorKeypoints = (keypoint1, keypoint2)
# If the minimum error is too great, then we haven't found the robot
maximumAcceptableError = distanceBetweenMarkers # this says that we've got a 100% error
if minimumError is None or minimumError > maximumAcceptableError:
# We've failed to find the robot
robot.location = None
robot.orientation = None
if debug:
print(("Failed to find robot:", robot))
return
# print "minimum error", minimumError
# Calculate the centre of the robot given the selected keypoints
keypoint1, keypoint2 = minimumErrorKeypoints
robot.location = (int((keypoint1.pt[0] + keypoint2.pt[0]) / 2), int((keypoint1.pt[1] + keypoint2.pt[1]) / 2))
# Calculate orientation of robot given selected keypoints
deltaX = keypoint1.pt[0] - keypoint2.pt[0]
deltaY = keypoint1.pt[1] - keypoint2.pt[1]
robot.orientation = math.degrees(math.atan2(deltaX, deltaY))
# Debugging if requested
if debug:
print(("robot:",robot))
def updateLocations(self, frame, camera, debug=False):
'''Update the robots' locations.'''
self.findColouredBlobs(frame, debug)
for robot in self.robots:
#if robot.location is None:
self.findRobot(robot, camera, debug)
if True:
# Add circles around our robots
cv.circle(frame, robot.location, 20, (0,0,0), 2)
# Add a line to indicate the orientation
if robot.location is not None and robot.orientation is not None:
cv.line(frame, robot.location, (int(robot.location[0] + 20*math.sin(math.radians(robot.orientation))), int(robot.location[1] + 20*math.cos(math.radians(robot.orientation)))), (0,0,0), 3)
def getMeanAndStdDevFromColourSamples(colourSamples):
'''From a list of colourSamples, find the mean and standard deviation using a delta from the first sampled
colour'''
assert len(colourSamples) > 0
# Source colour is the first of the colour samples
sourceColour = int(colourSamples[0])
# Go through the samples and delta between the current sample and the source. There are two deltas, one in each
# direction (possibly looping over 360 degrees).
deltaUpColourSamples = [(int(x) - sourceColour) % 256 for x in colourSamples]
deltaDownColourSamples = [-((sourceColour - (int(x))) % 256) for x in colourSamples]
# Find the minimum of the two deltas
deltaColourSamples = []
for i in range(len(deltaDownColourSamples)):
if abs(deltaDownColourSamples[i]) <= abs(deltaUpColourSamples[i]):
deltaColourSamples.append(deltaDownColourSamples[i])
else:
deltaColourSamples.append(deltaUpColourSamples[i])
# Find the mean and standard deviation of the (minimum) deltas
meanDelta = float(sum(deltaColourSamples)) / len(deltaColourSamples)
# Calculate the mean colour.
meanColour = (sourceColour + meanDelta) % 256
sdDelta = math.sqrt(
float(sum([(x - meanDelta) ** 2 for x in deltaColourSamples])) / len(deltaColourSamples))
# Convert the colours (from uint8) to degrees
meanColourDegrees = float(meanColour) / 255 * 360
sdColourDegrees = float(sdDelta) / 255 * 360
return (meanColourDegrees, sdColourDegrees)
class BallDetector:
def __init__(self, numberOfBalls):
self.numberOfBalls = numberOfBalls
# Setup SimpleBlobDetector parameters.
def createDetectorParameters():
params = cv.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 10
params.maxThreshold = 200
# Filter by Colour
params.filterByColor = True
params.blobColor = 255
# Filter by Area.
params.filterByArea = True
params.minArea = 150
# Filter by Circularity
params.filterByCircularity = False
params.minCircularity = 0.1
# Filter by Convexity
params.filterByConvexity = False
params.minConvexity = 0.87
# Filter by Inertia
params.filterByInertia = False
params.minInertiaRatio = 0.01
return params
# Create SimpleBlobDetector (assumes OpenCV version 3)
params = createDetectorParameters()
self.detector = cv.SimpleBlobDetector_create(params)
# To start, we do not know what the balls' colour is
self.colourHueDegrees = None
self.colourHeusBandDegrees = None
# To start, we do not know where any of the balls are
class Ball:
def __init__(self):
self.location = None
def __repr__(self):
return "("+str(self.location[0])+","+str(self.location[1])+")"
self.balls = []
for i in range(self.numberOfBalls):
self.balls.append( Ball() )
def getBallsPointsAsTuples(self):
result = []
for ball in self.balls:
result.append( ball.location )
return result
def calibrateColour(self, camera, bandWidthInStdDevs=3):
self.colourHuesDegrees = []
self.colourHeusBandDegrees = []
print("Calibrate the balls' colour. Click on colour samples, press space when done.")
def mouseCallbackGetColour(event, x, y, flags, param):
if event == cv.EVENT_LBUTTONDOWN:
colourValue = hsvFrame[y, x]
print(colourValue)
colourSamples.append(colourValue[0])
colourSamples = []
while True:
# Get undistorted frame
undistortedFrame = camera.getUndistortedFrame()
# Convert to HSV
hsvFrame = cv.cvtColor(undistortedFrame, cv.COLOR_BGR2HSV_FULL)
windowTitle = "Calibrate Ball Colour"
cv.imshow(windowTitle, undistortedFrame)
cv.setMouseCallback(windowTitle, mouseCallbackGetColour)
key = cv.waitKey(1)
if key == 1048608: # space
# Calculate mean and standard deviation of colour samples
meanColourDegrees, sdColourDegrees = getMeanAndStdDevFromColourSamples(colourSamples)
print(("mean colour (degrees): ", meanColourDegrees, "sd:", sdColourDegrees))
self.colourHueDegrees = meanColourDegrees
self.colourHeuBandDegrees = sdColourDegrees * bandWidthInStdDevs
cv.destroyWindow(windowTitle)
break
def calibrateInitialLocations(self, camera):
print("Calibrate the balls' initial positions. Click on the centres of the balls.")
def mouseCallbackGetColour(event, x, y, flags, param):
if event == cv.EVENT_LBUTTONDOWN:
ballLocation = (x,y)
print(("ball location:", ballLocation))
ballLocations.append(ballLocation)
ballLocations = []
while True:
# Get undistorted frame
undistortedFrame = camera.getUndistortedFrame()
windowTitle = "Calibrate Initial Ball Positions"
cv.imshow(windowTitle, undistortedFrame)
cv.setMouseCallback(windowTitle, mouseCallbackGetColour)
key = cv.waitKey(1)
if len(ballLocations) >= self.numberOfBalls:
break
for i in range(self.numberOfBalls):
self.balls[i].location = ballLocations[i]
pass
def displayCalibration(self):
print(("colourHueDegrees: ", self.colourHueDegrees))
print(("colourHeuBandDegrees: ", self.colourHeuBandDegrees))
def setCalibration(self, colourHueDegrees, colourHeuBandDegrees):
self.colourHueDegrees = colourHueDegrees
self.colourHeuBandDegrees = colourHeuBandDegrees
def findColouredPixels(self, hsvFrame, hueDegrees, hueBandDegrees=20):
if hueDegrees - hueBandDegrees > 0 and hueDegrees + hueBandDegrees < 360:
# The band does not wrap around 0/360 degrees
hueLowerBoundInt = np.uint8((float(hueDegrees - hueBandDegrees) / 360) * 255)
hueUpperBoundInt = np.uint8((float(hueDegrees + hueBandDegrees) / 360) * 255)
lowerBound = np.array([hueLowerBoundInt, 50, 50])
upperBound = np.array([hueUpperBoundInt, 255, 255])
mask = cv.inRange(hsvFrame, lowerBound, upperBound)
# maskedFrame = cv.bitwise_and(frame, frame, mask=mask)
return mask
else:
# The band wraps around zero, so deal with it in two parts
# First deal with the portion of the band near zero degrees
hueZeroBoundInt = np.uint8((float((hueDegrees + hueBandDegrees) % 360) / 360) * 255)
zero = np.array([0, 50, 50])
zeroBound = np.array([hueZeroBoundInt, 255, 255])
maskZero = cv.inRange(hsvFrame, zero, zeroBound)
# Next, deal with the portion of the band zero 360 degrees
hue360BoundInt = np.uint8((float((hueDegrees - hueBandDegrees) % 360) / 360) * 255)
limit360Bound = np.array([hue360BoundInt, 50, 50])
limit360 = np.array([255, 255, 255])
mask360 = cv.inRange(hsvFrame, limit360Bound, limit360)
# The final mask is made of both the near-zero and near-360 masks
mask = cv.bitwise_or(maskZero, mask360)
return mask
def findColouredBlobs(self, frame, debug=False):
# Convert to HSV
# global hsvFrame
hsvFrame = cv.cvtColor(frame, cv.COLOR_BGR2HSV_FULL) #
mask = self.findColouredPixels(hsvFrame, self.colourHueDegrees, self.colourHeuBandDegrees)
self.keypoints = self.detector.detect(mask)
if debug:
frameWithKeypoints = frame
# Show mask
maskWindowName = "Mask"
cv.imshow(maskWindowName, mask)
# cv.moveWindow(maskWindowName, 0, 0)
# Show keypoints
KeyPointColour = [255 * cl for cl in
colorsys.hsv_to_rgb(float(self.colourHueDegrees) / 360, 1, 1)]
KeyPointColour.reverse()
KeyPointColour = tuple(KeyPointColour)
frameWithKeypoints = cv.drawKeypoints(frameWithKeypoints, self.keypoints, np.array([]),
KeyPointColour,
cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv.imshow("Key Points", frameWithKeypoints)
def findBall(self, ball, camera, debug=False):
pass
def findBallWithKnownLocation(self, ball, camera, debug=False):
if debug:
print(("looking for ball with previous location of", ball.location))
# If the minimum delta is too great, then we haven't found the ball
self.maxDelta = 50 # mm per frame
ball.minimumDelta = None
for keypoint in self.keypoints:
# Go through each ball and find the distance between this keypoint and the previous known location
# Calculate distance between the keypoint and the ball
distance = math.sqrt((keypoint.pt[0] - ball.location[0]) ** 2 + \
(keypoint.pt[1] - ball.location[1]) ** 2)
# Convert distance in pixels to real-world distance
distance /= camera.pixelsPerMillimeter
delta = abs(distance)
if ball.minimumDelta is None or delta < ball.minimumDelta:
if delta < self.maxDelta:
ball.minimumDelta = delta
ball.minimumDeltaLocation = keypoint.pt
if ball.minimumDelta is None:
# We've failed to find the ball
if debug:
print(("Failed to find ball at previous location:", ball.location))
return
else:
# We've found the ball, update its current position
if debug:
print(("Found ball at previous location:", ball.location, "change in position of", ball.minimumDelta, "new position:", ball.minimumDeltaLocation))
ball.location = ball.minimumDeltaLocation
def updateLocations(self, frame, camera, debug=False):
'''Update the balls' locations.'''
self.findColouredBlobs(frame, debug)
for ball in self.balls:
if ball.location is None:
print("The balls' initial locations should be specified and not found automatically.")
self.findBall(ball, camera, debug)
else:
self.findBallWithKnownLocation(ball, camera, debug)
if True:
# Add circles around our balls
location = (int(ball.location[0]), int(ball.location[1]))
cv.circle(frame, location, 20, (50, 50, 50), 2)
if __name__ == "__main__":
# TEST BALL DETECTION
fishEyeCamera = FishEyeCamera(0)
ballDetector = BallDetector(4)
robotDetector = RobotDetector(numberOfColours=2)
#ballDetector.calibrateColour(fishEyeCamera)
ballDetector.setCalibration(31.7, 2.6*3)
ballDetector.calibrateInitialLocations(fishEyeCamera)
robotDetector.setCalibration([140.5, 220.9], [12.5, 8.6])
while True:
undistortedFrame = fishEyeCamera.getUndistortedFrame()
ballDetector.updateLocations(undistortedFrame, fishEyeCamera, debug=False)
robotDetector.updateLocations(undistortedFrame, fishEyeCamera, debug=False)
cv.imshow("Processed Frame", undistortedFrame)
wk = cv.waitKey(1)
if wk != -1:
print(wk)
if wk == 1048689: # q
break
cv.destroyAllWindows()
exit()
# TEST BALL DETECTION
fishEyeCamera = FishEyeCamera(1)
ballDetector = BallDetector(4)
#ballDetector.calibrateColour(fishEyeCamera)
ballDetector.setCalibration(31.7, 2.6*3)
ballDetector.calibrateInitialLocations(fishEyeCamera)
while True:
undistortedFrame = fishEyeCamera.getUndistortedFrame()
ballDetector.updateLocations(undistortedFrame, fishEyeCamera, debug=True)
#ballDetector.findColouredBlobs(undistortedFrame, debug=True)
cv.imshow("Processed Frame", undistortedFrame)
wk = cv.waitKey(1)
if wk != -1:
print(wk)
if wk == 1048689: # q
break
cv.destroyAllWindows()
exit()
# TEST ROBOT DETECTION
fishEyeCamera = FishEyeCamera(0)
robotDetector = RobotDetector(numberOfColours=2)
# robotDetector.calibrateColour(fishEyeCamera)
# robotDetector.setCalibration([360.0, 140.5, 220.9], [15.7, 12.5, 8.6])
robotDetector.setCalibration([140.5, 220.9], [12.5, 8.6])
robotDetector.displayCalibration()
# m1, sd1 = robotDetector.getMeanAndStdDevFromColourSamples([2,1,252])
# m2, sd2 = robotDetector.getMeanAndStdDevFromColourSamples([252,2,1])
# assert(m1==m2)
#
# print "sd1:", sd1, "sd2:",sd2
# assert(sd1 == sd2)
# print "mean:", m, "sd:",sd
# cv.destroyAllWindows()
# exit()
while True:
undistortedFrame = fishEyeCamera.getUndistortedFrame()
cv.setMouseCallback("Undistorted Camera", draw_circle)
robotDetector.updateLocations(undistortedFrame, fishEyeCamera, debug=False)
cv.imshow("Processed Frame", undistortedFrame)
# Convert to HSV
# hsvFrame = cv.cvtColor(undistortedFrame,cv.COLOR_BGR2HSV_FULL) #
# robotDetector.findColouredBlobs(undistortedFrame, True)
wk = cv.waitKey(1)
if wk != -1:
print(wk)
if wk == 1048689: # q
break
cv.destroyAllWindows()