-
Notifications
You must be signed in to change notification settings - Fork 788
Expand file tree
/
Copy pathTrackClass.py
More file actions
937 lines (676 loc) · 24.2 KB
/
Copy pathTrackClass.py
File metadata and controls
937 lines (676 loc) · 24.2 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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
from SimpleCV.Color import Color
from SimpleCV.base import time, np
from SimpleCV.Features.Features import Feature, FeatureSet
try:
import cv2
except ImportError:
pass
class Track(Feature):
"""
**SUMMARY**
Track class is the base of tracking. All different tracking algorithm
return different classes but they all belong to Track class. All the
common attributes are kept in this class
"""
def __init__(self, img, bb):
"""
**SUMMARY**
Initializes all the required parameters and attributes of the class.
**PARAMETERS**
* *img* - SimpleCV.ImageClass.Image
* *bb* - A tuple consisting of (x, y, w, h) of the bounding box
**RETURNS**
SimpleCV.Tracking.TrackClass.Track object
**EXAMPLE**
>>> track = Track(image, bb)
"""
self.bb = bb
self.image = img
self.bb_x, self.bb_y, self.w, self.h = self.bb
self.x, self.y = self.center = self.getCenter()
self.sizeRatio = 1
self.vel = (0,0)
self.rt_vel = (0,0)
self.area = self.getArea()
self.time = time.time()
self.cv2numpy = self.image.getNumpyCv2()
return self
def getCenter(self):
"""
**SUMMARY**
Get the center of the bounding box
**RETURNS**
* *tuple* - center of the bounding box (x, y)
**EXAMPLE**
>>> track = Track(img, bb)
>>> cen = track.getCenter()
"""
return (self.bb_x+self.w/2,self.bb_y+self.h/2)
def getArea(self):
"""
**SUMMARY**
Get the area of the bounding box
**RETURNS**
Area of the bounding box
**EXAMPLE**
>>> track = Track(img, bb)
>>> area = track.getArea()
"""
return self.w*self.h
def getImage(self):
"""
**SUMMARY**
Get the Image
**RETURNS**
SimpleCV.ImageClass.Image
**EXAMPLE**
>>> track = Track(img, bb)
>>> i = track.getImage()
"""
return self.image
def getBB(self):
"""
**SUMMARY**
Get the bounding box
**RETURNS**
A tuple - (x, y, w, h)
**EXAMPLE**
>>> track = Track(img, bb)
>>> print track.getBB()
"""
return self.bb
def draw(self, color=Color.GREEN, rad=1, thickness=1):
"""
**SUMMARY**
Draw the center of the object on the image.
**PARAMETERS**
* *color* - The color to draw the object. Either an BGR tuple or a member of the :py:class:`Color` class.
* *rad* - Radius of the circle to be plotted on the center of the object.
* *thickness* - Thickness of the boundary of the center circle.
**RETURNS**
Nada. Nothing. Zilch.
**EXAMPLE**
>>> track = Track(img, bb)
>>> track.draw()
>>> img.show()
"""
f = self
f.image.drawCircle(f.center, rad, color, thickness)
def drawBB(self, color=Color.GREEN, thickness=3):
"""
**SUMMARY**
Draw the bounding box over the object on the image.
**PARAMETERS**
* *color* - The color to draw the object. Either an BGR tuple or a member of the :py:class:`Color` class.
* *thickness* - Thickness of the boundary of the bounding box.
**RETURNS**
Nada. Nothing. Zilch.
**EXAMPLE**
>>> track = Track(img, bb)
>>> track.drawBB()
>>> img.show()
"""
f = self
f.image.drawRectangle(f.bb_x, f.bb_y, f.w, f.h, color, thickness)
def showCoordinates(self, pos=None, color=Color.GREEN, size=None):
"""
**SUMMARY**
Show the co-ordinates of the object in text on the Image.
**PARAMETERS**
* *pos* - A tuple consisting of x, y values. where to put to the text
* *color* - The color to draw the object. Either an BGR tuple or a member of the :py:class:`Color` class.
* *size* - Fontsize of the text
**RETURNS**
Nada. Nothing. Zilch.
**EXAMPLE**
>>> track = Track(img, bb)
>>> track.showCoordinates()
>>> img.show()
"""
f = self
img = f.image
if not pos:
imgsize = img.size()
pos = (imgsize[0]-120, 10)
if not size:
size = 16
text = "x = %d y = %d" % (f.x, f.y)
img.drawText(text, pos[0], pos[1], color, size)
def showSizeRatio(self, pos=None, color=Color.GREEN, size=None):
"""
**SUMMARY**
Show the sizeRatio of the object in text on the image.
**PARAMETERS**
* *pos* - A tuple consisting of x, y values. where to put to the text
* *color* - The color to draw the object. Either an BGR tuple or a member of the :py:class:`Color` class.
* *size* - Fontsize of the text
**RETURNS**
Nada. Nothing. Zilch.
**EXAMPLE**
>>> while True:
... img1 = cam.getImage()
... ts = img1.track("camshift", ts1, img, bb)
... ts[-1].showSizeRatio() # For continuous bounding box
... img = img1
"""
f = self
img = f.image
if not pos:
imgsize = img.size()
pos = (imgsize[0]-120, 30)
if not size:
size = 16
text = "size = %f" % (f.sizeRatio)
img.drawText(text, pos[0], pos[1], color, size)
def showPixelVelocity(self, pos=None, color=Color.GREEN, size=None):
"""
**SUMMARY**
Show the Pixel Veloctiy (pixel/frame) of the object in text on the image.
**PARAMETERS**
* *pos* - A tuple consisting of x, y values. where to put to the text
* *color* - The color to draw the object. Either an BGR tuple or a member of the :py:class:`Color` class.
* *size* - Fontsize of the text
**RETURNS**
Nada. Nothing. Zilch.
**EXAMPLE**
>>> while True:
... img1 = cam.getImage()
... ts = img1.track("camshift", ts1, img, bb)
... ts[-1].showPixelVelocity() # For continuous bounding box
... img = img1
"""
f = self
img = f.image
vel = f.vel
if not pos:
imgsize = img.size()
pos = (imgsize[0]-120, 90)
if not size:
size = 16
text = "Vx = %.2f Vy = %.2f" % (vel[0], vel[1])
img.drawText(text, pos[0], pos[1], color, size)
img.drawText("in pixels/frame", pos[0], pos[1]+size, color, size)
def showPixelVelocityRT(self, pos=None, color=Color.GREEN, size=None):
"""
**SUMMARY**
Show the Pixel Veloctiy (pixels/second) of the object in text on the image.
**PARAMETERS**
* *pos* - A tuple consisting of x, y values. where to put to the text
* *color* - The color to draw the object. Either an BGR tuple or a member of the :py:class:`Color` class.
* *size* - Fontsize of the text
**RETURNS**
Nada. Nothing. Zilch.
**EXAMPLE**
>>> while True:
... img1 = cam.getImage()
... ts = img1.track("camshift", ts1, img, bb)
... ts[-1].showPixelVelocityRT() # For continuous bounding box
... img = img1
"""
f = self
img = f.image
vel_rt = f.vel_rt
if not pos:
imgsize = img.size()
pos = (imgsize[0]-120, 50)
if not size:
size = 16
text = "Vx = %.2f Vy = %.2f" % (vel_rt[0], vel_rt[1])
img.drawText(text, pos[0], pos[1], color, size)
img.drawText("in pixels/second", pos[0], pos[1]+size, color, size)
def processTrack(self, func):
"""
**SUMMARY**
This method lets you use your own function on the current image.
**PARAMETERS**
* *func* - some user defined function for SimpleCV.ImageClass.Image object
**RETURNS**
the value returned by the user defined function
**EXAMPLE**
>>> def foo(img):
... return img.meanColor()
>>> mean_color = ts[-1].processTrack(foo)
"""
return func(self.image)
def getPredictionPoints(self):
"""
**SUMMARY**
get predicted Co-ordinates of the center of the object
**PARAMETERS**
None
**RETURNS**
* *tuple*
**EXAMPLE**
>>> track = Track(img, bb)
>>> track.getPredictedCoordinates()
"""
return self.predict_pt
def drawPredicted(self, color=Color.GREEN, rad=1, thickness=1):
"""
**SUMMARY**
Draw the center of the object on the image.
**PARAMETERS**
* *color* - The color to draw the object. Either an BGR tuple or a member of the :py:class:`Color` class.
* *rad* - Radius of the circle to be plotted on the center of the object.
* *thickness* - Thickness of the boundary of the center circle.
**RETURNS**
Nada. Nothing. Zilch.
**EXAMPLE**
>>> track = Track(img, bb)
>>> track.drawPredicted()
>>> img.show()
"""
f = self
f.image.drawCircle(f.predict_pt, rad, color, thickness)
def showPredictedCoordinates(self, pos=None, color=Color.GREEN, size=None):
"""
**SUMMARY**
Show the co-ordinates of the object in text on the Image.
**PARAMETERS**
* *pos* - A tuple consisting of x, y values. where to put to the text
* *color* - The color to draw the object. Either an BGR tuple or a member of the :py:class:`Color` class.
* *size* - Fontsize of the text
**RETURNS**
Nada. Nothing. Zilch.
**EXAMPLE**
>>> track = Track(img, bb)
>>> track.showPredictedCoordinates()
>>> img.show()
"""
f = self
img = f.image
if not pos:
imgsize = img.size()
pos = (5, 10)
if not size:
size = 16
text = "Predicted: x = %d y = %d" % (f.predict_pt[0], f.predict_pt[1])
img.drawText(text, pos[0], pos[1], color, size)
def getCorrectedPoints(self):
"""
**SUMMARY**
Corrected Co-ordinates of the center of the object
**PARAMETERS**
None
**RETURNS**
* *tuple*
**EXAMPLE**
>>> track = Track(img, bb)
>>> track.getCorrectedCoordinates()
"""
return self.state_pt
def showCorrectedCoordinates(self, pos=None, color=Color.GREEN, size=None):
"""
**SUMMARY**
Show the co-ordinates of the object in text on the Image.
**PARAMETERS**
* *pos* - A tuple consisting of x, y values. where to put to the text
* *color* - The color to draw the object. Either an BGR tuple or a member of the :py:class:`Color` class.
* *size* - Fontsize of the text
**RETURNS**
Nada. Nothing. Zilch.
**EXAMPLE**
>>> track = Track(img, bb)
>>> track.showCorrectedCoordinates()
>>> img.show()
"""
f = self
img = f.image
if not pos:
imgsize = img.size()
pos = (5, 40)
if not size:
size = 16
text = "Corrected: x = %d y = %d" % (f.state_pt[0], f.state_pt[1])
img.drawText(text, pos[0], pos[1], color, size)
def drawCorrected(self, color=Color.GREEN, rad=1, thickness=1):
"""
**SUMMARY**
Draw the center of the object on the image.
**PARAMETERS**
* *color* - The color to draw the object. Either an BGR tuple or a member of the :py:class:`Color` class.
* *rad* - Radius of the circle to be plotted on the center of the object.
* *thickness* - Thickness of the boundary of the center circle.
**RETURNS**
Nada. Nothing. Zilch.
**EXAMPLE**
>>> track = Track(img, bb)
>>> track.drawCorrected()
>>> img.show()
"""
f = self
f.image.drawCircle(f.state_pt, rad, color, thickness)
class CAMShiftTrack(Track):
"""
**SUMMARY**
CAMShift Class is returned by track when CAMShift tracking is required.
This class is a superset of Track Class. And all of Track class'
attributes can be accessed.
CAMShift class has "ellipse" attribute which is not present in Track
"""
def __init__(self, img, bb, ellipse):
"""
**SUMMARY**
Initializes all the required parameters and attributes of the CAMShift class.
**PARAMETERS**
* *img* - SimpleCV.ImageClass.Image
* *bb* - A tuple consisting of (x, y, w, h) of the bounding box
* ellipse* - A tuple
**RETURNS**
SimpleCV.Tracking.TrackClass.CAMShiftTrack object
**EXAMPLE**
>>> track = CAMShiftTrack(image, bb, ellipse)
"""
self = Track.__init__(self, img, bb)
self.ellipse = ellipse
def getEllipse(self):
"""
**SUMMARY**
Returns the ellipse.
**RETURNS**
A tuple
**EXAMPLE**
>>> track = CAMShiftTrack(image, bb, ellipse)
>>> e = track.getEllipse()
"""
return self.ellipse
class LKTrack(Track):
"""
**SUMMARY**
LK Tracking class is used for Lucas-Kanade Track algorithm. It's
derived from Track Class. Apart from all the properties of Track class,
LK has few other properties. Since in LK tracking method, we obtain tracking
points, we have functionalities to draw those points on the image.
"""
def __init__(self, img, bb, pts):
"""
**SUMMARY**
Initializes all the required parameters and attributes of the class.
**PARAMETERS**
* *img* - SimpleCV.ImageClass.Image
* *bb* - A tuple consisting of (x, y, w, h) of the bounding box
* *pts* - List of all the tracking points
**RETURNS**
SimpleCV.Tracking.TrackClass.LKTrack object
**EXAMPLE**
>>> track = LKTrack(image, bb, pts)
"""
self = Track.__init__(self, img, bb)
self.pts = pts
def getTrackedPoints(self):
"""
**SUMMARY**
Returns all the points which are being tracked.
**RETURNS**
A list
**EXAMPLE**
>>> track = LKTrack(image, bb, pts)
>>> pts = track.getTrackedPoints()
"""
return self.pts
def drawTrackerPoints(self, color=Color.GREEN, radius=1, thickness=1):
"""
**SUMMARY**
Draw all the points which are being tracked.
**PARAMETERS**
* *color* - Color of the point
* *radius* - Radius of the point
*thickness* - thickness of the circle point
**RETURNS**
Nothing
**EXAMPLE**
>>> track = LKTrack(image, bb, pts)
>>> track.drawTrackerPoints()
"""
if type(self.pts) is not type(None):
for pt in self.pts:
self.image.drawCircle(ctr=pt, rad=radius, thickness=thickness, color=color)
class SURFTrack(Track):
"""
**SUMMARY**
SURFTracker class is used for SURF Based keypoints matching tracking algorithm.
It's derived from Track Class. Apart from all the properties of Track class SURFTracker
has few other properties.
Matches keypoints from the template image and the current frame.
flann based matcher is used to match the keypoints.
Density based clustering is used classify points as in-region (of bounding box)
and out-region points. Using in-region points, new bounding box is predicted using
k-means.
"""
def __init__(self, img, new_pts, detector, descriptor, templateImg, skp, sd, tkp, td):
"""
**SUMMARY**
Initializes all the required parameters and attributes of the class.
**PARAMETERS**
* *img* - SimpleCV.Image
* *new_pts* - List of all the tracking points found in the image. - list of cv2.KeyPoint
* *detector* - SURF detector - cv2.FeatureDetector
* *descriptor* - SURF descriptor - cv2.DescriptorExtractor
* *templateImg* - Template Image (First image) - SimpleCV.Image
* *skp* - image keypoints - list of cv2.KeyPoint
* *sd* - image descriptor - numpy.ndarray
* *tkp* - Template Imaeg keypoints - list of cv2.KeyPoint
* *td* - Template image descriptor - numpy.ndarray
**RETURNS**
SimpleCV.Tracking.TrackClass.SURFTrack object
**EXAMPLE**
>>> track = SURFTracker(image, pts, detector, descriptor, temp, skp, sd, tkp, td)
"""
if td is None:
bb = (1, 1, 1, 1)
self = Track.__init__(self, img, bb)
return
if len(new_pts) < 1:
bb = (1, 1, 1, 1)
self = Track.__init__(self, img, bb)
self.pts = None
self.templateImg = templateImg
self.skp = skp
self.sd = sd
self.tkp = tkp
self.td = td
self.detector = detector
self.descriptor = descriptor
return
if sd is None:
bb = (1, 1, 1, 1)
self = Track.__init__(self, img, bb)
self.pts = None
self.templateImg = templateImg
self.skp = skp
self.sd = sd
self.tkp = tkp
self.td = td
self.detector = detector
self.descriptor = descriptor
return
np_pts = np.asarray([kp.pt for kp in new_pts])
t, pts, center = cv2.kmeans(np.asarray(np_pts, dtype=np.float32), K=1, bestLabels=None,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 1, 10), attempts=1,
flags=cv2.KMEANS_RANDOM_CENTERS)
max_x = int(max(np_pts[:, 0]))
min_x = int(min(np_pts[:, 0]))
max_y = int(max(np_pts[:, 1]))
min_y = int(min(np_pts[:, 1]))
bb = (min_x-5, min_y-5, max_x-min_x+5, max_y-min_y+5)
self = Track.__init__(self, img, bb)
self.templateImg = templateImg
self.skp = skp
self.sd = sd
self.tkp = tkp
self.td = td
self.pts = np_pts
self.detector = detector
self.descriptor = descriptor
def getTrackedPoints(self):
"""
**SUMMARY**
Returns all the points which are being tracked.
**RETURNS**
A list of points.
**EXAMPLE**
>>> track = SURFTrack(image, pts, detector, descriptor, temp, skp, sd, tkp, td)
>>> pts = track.getTrackedPoints()
"""
return self.pts
def drawTrackerPoints(self, color=Color.GREEN, radius=1, thickness=1):
"""
**SUMMARY**
Draw all the points which are being tracked.
**PARAMETERS**
* *color* - Color of the point
* *radius* - Radius of the point
*thickness* - thickness of the circle point
**RETURNS**
Nothing
**EXAMPLE**
>>> track = SURFTrack(image, pts, detector, descriptor, temp, skp, sd, tkp, td)
>>> track.drawTrackerPoints()
"""
if type(self.pts) is not type(None):
for pt in self.pts:
self.image.drawCircle(ctr=pt, rad=radius, thickness=thickness, color=color)
def getDetector(self):
"""
**SUMMARY**
Returns SURF detector which is being used.
**RETURNS**
detector - cv2.Detctor
**EXAMPLE**
>>> track = SURFTrack(image, pts, detector, descriptor, temp, skp, sd, tkp, td)
>>> detector = track.getDetector()
"""
return self.detector
def getDescriptor(self):
"""
**SUMMARY**
Returns SURF descriptor extractor which is being used.
**RETURNS**
detector - cv2.DescriptorExtractor
**EXAMPLE**
>>> track = SURFTrack(image, pts, detector, descriptor, temp, skp, sd, tkp, td)
>>> descriptor= track.getDescriptor()
"""
return self.descriptor
def getImageKeyPoints(self):
"""
**SUMMARY**
Returns all the keypoints which are found on the image.
**RETURNS**
A list of points.
**EXAMPLE**
>>> track = SURFTrack(image, pts, detector, descriptor, temp, skp, sd, tkp, td)
>>> skp = track.getImageKeyPoints()
"""
return self.skp
def getImageDescriptor(self):
"""
**SUMMARY**
Returns the image descriptor.
**RETURNS**
Image descriptor - numpy.ndarray
**EXAMPLE**
>>> track = SURFTrack(image, pts, detector, descriptor, temp, skp, sd, tkp, td)
>>> sd = track.getImageDescriptor()
"""
return self.sd
def getTemplateKeyPoints(self):
"""
**SUMMARY**
Returns all the keypoints which are found on the template Image.
**RETURNS**
A list of points.
**EXAMPLE**
>>> track = SURFTrack(image, pts, detector, descriptor, temp, skp, sd, tkp, td)
>>> tkp = track.getTemplateKeyPoints()
"""
return self.tkp
def getTemplateDescriptor(self):
"""
**SUMMARY**
Returns the template image descriptor.
**RETURNS**
Image descriptor - numpy.ndarray
**EXAMPLE**
>>> track = SURFTrack(image, pts, detector, descriptor, temp, skp, sd, tkp, td)
>>> td = track.getTemplateDescriptor()
"""
return self.td
def getTemplateImage(self):
"""
**SUMMARY**
Returns Template Image.
**RETURNS**
Template Image - SimpleCV.Image
**EXAMPLE**
>>> track = SURFTrack(image, pts, detector, descriptor, temp, skp, sd, tkp, td)
>>> templateImg = track.getTemplateImage()
"""
return self.templateImg
class MFTrack(Track):
"""
**SUMMARY**
MFTracker class is used for Median Flow Tracking algorithm. It's
derived from Track Class. Apart from all the properties of Track class,
MFTracker has few other properties.
Media Flow Tracker is the base tracker that is used in OpenTLD. It is based on
Optical Flow. It calculates optical flow of the points in the bounding box from
frame 1 to frame 2 and from frame 2 to frame 1 and using back track error, removes
false positives. As the name suggests, it takes the median of the flow, and eliminates
points.
"""
def __init__(self, img, bb, shift):
"""
**SUMMARY**
Initializes all the required parameters and attributes of the class.
**PARAMETERS**
* *img* - SimpleCV.ImageClass.Image
* *bb* - A tuple consisting of (x, y, w, h) of the bounding box
* *shift* - Object Shift calcluated in Median Flow
**RETURNS**
SimpleCV.Tracking.TrackClass.MFTrack object
**EXAMPLE**
>>> track = MFTrack(image, bb, shift)
"""
self = Track.__init__(self, img, bb)
self.shift = shift
def getShift(self):
"""
**SUMMARY**
Returns object shift that was calcluated in Median Flow.
**RETURNS**
float
**EXAMPLE**
>>> track = MFTrack(image, bb, pts)
>>> pts = track.getShift()
"""
return self.shift
def showShift(self, pos=None, color=Color.GREEN, size=None):
"""
**SUMMARY**
Show the Pixel Veloctiy (pixels/second) of the object in text on the image.
**PARAMETERS**
* *pos* - A tuple consisting of x, y values. where to put to the text
* *color* - The color to draw the object. Either an BGR tuple or a member of the :py:class:`Color` class.
* *size* - Fontsize of the text
**RETURNS**
Nada. Nothing. Zilch.
**EXAMPLE**
>>> ts = []
>>> while True:
... img1 = cam.getImage()
... ts = img1.track("mftrack", ts, img, bb)
... ts[-1].showShift()
... img1.show()
"""
f = self
img = f.image
shift = f.shift
if not pos:
imgsize = img.size()
pos = (imgsize[0]-120, 50)
if not size:
size = 16
text = "Shift = %.2f" % (shift)
img.drawText(text, pos[0], pos[1], color, size)
img.drawText("in pixels/second", pos[0], pos[1]+size, color, size)