diff python/cvutils.py @ 203:e2f31813ade6

added code to display trajectories on videa
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 06 Mar 2012 18:10:19 -0500
parents 8e7b354666ec
children 966c2cd2bd9f
line wrap: on
line diff
--- a/python/cvutils.py	Mon Mar 05 02:55:19 2012 -0500
+++ b/python/cvutils.py	Tue Mar 06 18:10:19 2012 -0500
@@ -85,12 +85,19 @@
                 cvmat[i,j] = a[i,j]
         return cvmat
 
+    def draw(img, positions, color, lastCoordinate = None):
+        last = lastCoordinate+1
+        if lastCoordinate != None and lastCoordinate >=0:
+            last = min(positions.length()-1, lastCoordinate)
+        for i in range(0, last-1):
+            cv2.line(img, positions[i].astuple(), positions[i+1].astuple(), color)
+
     def playVideo(filename):
         '''Plays the video'''
         capture = cv2.VideoCapture(filename)
         if capture.isOpened():
             key = -1
-            while key!= 1048689: # 'q'
+            while key!= 113: # 'q'
                 ret, img = capture.read()
                 if ret:
                     cv2.imshow('frame', img)
@@ -110,6 +117,26 @@
                     images.append(img)
         return images
 
+    def displayTrajectories(videoFilename, objects, homography = None):
+        '''Displays the objects overlaid frame by frame over the video '''
+        capture = cv2.VideoCapture(videoFilename)
+        if capture.isOpened():
+            key = -1
+            frameNum = 1
+            while key!= 113: # 'q'
+                ret, img = capture.read()
+                if ret:
+                    print(frameNum)
+                    for obj in objects:
+                        if obj.existsAtInstant(frameNum):
+                            #obj.getTimeInterval()
+                            if homography != None and obj.getFirstInstant() == frameNum:
+                                obj.projectedPositions = obj.positions.project(homography)
+                            draw(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant())
+                    cv2.imshow('frame', img)
+                    key = cv2.waitKey(50)
+                    frameNum += 1
+    
 def printCvMat(cvmat, out = stdout):
     '''Prints the cvmat to out'''
     for i in xrange(cvmat.rows):