diff python/cvutils.py @ 385:1917db662aa7

added rescaling options to scripts play-video and display-trajectories
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 22 Jul 2013 18:33:47 -0400
parents 6da9cf5609aa
children eaf7765221d9
line wrap: on
line diff
--- a/python/cvutils.py	Mon Jul 22 18:11:01 2013 -0400
+++ b/python/cvutils.py	Mon Jul 22 18:33:47 2013 -0400
@@ -100,7 +100,17 @@
         for i in range(0, last-1):
             cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color)
 
-    def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None):
+    def cvImshow(windowName, img, rescale = 1.0):
+        'Rescales the image (in particular if too large)'
+        from cv2 import resize
+        if rescale != 1.:
+            size = (int(round(img.shape[1]*rescale)), int(round(img.shape[0]*rescale)))
+            resizedImg = resize(img, size)
+            cv2.imshow(windowName, resizedImg)
+        else:
+            cv2.imshow(windowName, img)
+
+    def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1.):
         '''Plays the video'''
         wait = 5
         if frameRate > 0:
@@ -121,7 +131,7 @@
                     frameNum+=1
                     if text != None:
                        cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) 
-                    cv2.imshow('frame', img)
+                    cvImshow('frame', img, rescale)
                     key = cv2.waitKey(wait)
             cv2.destroyAllWindows()
 
@@ -146,7 +156,7 @@
                         images.append(img)
         return images
 
-    def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True):
+    def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.):
         '''Displays the objects overlaid frame by frame over the video '''
         capture = cv2.VideoCapture(videoFilename)
         if capture.isOpened():
@@ -173,7 +183,7 @@
                                     obj.projectedPositions = obj.positions
                             draw(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant())
                             cv2.putText(img, '{0}'.format(obj.num), obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed)
-                    cv2.imshow('frame', img)
+                    cvImshow('frame', img, rescale)
                     key = cv2.waitKey()
                     if saveKey(key):
                         cv2.imwrite('image.png', img)