changeset 478:d337bffd7283

Display of points in compute homography and step option to replay videos A bug seems to remain with respect to trajectory bounds, to check
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 27 Mar 2014 11:40:28 -0400
parents ed4166b0ba9d
children 7828fec8bbd2
files python/cvutils.py scripts/compute-homography.py scripts/display-trajectories.py
diffstat 3 files changed, 17 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/python/cvutils.py	Tue Mar 25 19:43:47 2014 -0400
+++ b/python/cvutils.py	Thu Mar 27 11:40:28 2014 -0400
@@ -202,7 +202,7 @@
         return imgcrop, yCropMin, yCropMax, xCropMin, xCropMax
 
 
-    def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.):
+    def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1):
         '''Displays the objects overlaid frame by frame over the video '''
         from moving import userTypeNames
 
@@ -246,7 +246,9 @@
                     key = cv2.waitKey()
                     if saveKey(key):
                         cv2.imwrite('image.png', img)
-                    frameNum += 1
+                    frameNum += nFramesStep
+                    if nFramesStep > 1:
+                        capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum)
             cv2.destroyAllWindows()
         else:
             print 'Cannot load file ' + videoFilename
--- a/scripts/compute-homography.py	Tue Mar 25 19:43:47 2014 -0400
+++ b/scripts/compute-homography.py	Thu Mar 27 11:40:28 2014 -0400
@@ -96,16 +96,21 @@
     np.savetxt('homography.txt',homography)
 
 if args.displayPoints and args.videoFrameFilename != None and args.worldFilename != None and homography.size>0:
-    worldImg = plt.imread(args.worldFilename)
-    videoImg = plt.imread(args.videoFrameFilename)
+    worldImg = cv2.imread(args.worldFilename)
+    videoImg = cv2.imread(args.videoFrameFilename)
     invHomography = np.linalg.inv(homography)
     projectedWorldPts = cvutils.projectArray(invHomography, worldPts.T).T
-    projectedVideoPts = cvutils.projectArray(invHomography, videoPts.T).T
+    projectedVideoPts = cvutils.projectArray(homography, videoPts.T).T
     for i in range(worldPts.shape[0]):
-        cv2.circle(worldImg,tuple(np.int32(np.round(worldPts[i]/args.unitsPerPixel))),2,cvutils.cvRed)
+        # world image
+        cv2.circle(worldImg,tuple(np.int32(np.round(worldPts[i]/args.unitsPerPixel))),2,cvutils.cvBlue)
         cv2.circle(worldImg,tuple(np.int32(np.round(projectedVideoPts[i]/args.unitsPerPixel))),2,cvutils.cvRed)
-        # TODO print numbers and in image space
+        cv2.putText(worldImg, str(i+1), tuple(np.int32(np.round(worldPts[i]/args.unitsPerPixel))+5), cv2.FONT_HERSHEY_PLAIN, 2., cvutils.cvBlue, 2)
+        # video image
+        cv2.circle(videoImg,tuple(np.int32(np.round(videoPts[i]))),2,cvutils.cvBlue)
+        cv2.circle(videoImg,tuple(np.int32(np.round(projectedWorldPts[i]))),2,cvutils.cvRed)
+        cv2.putText(videoImg, str(i+1), tuple(np.int32(np.round(videoPts[i])+5)), cv2.FONT_HERSHEY_PLAIN, 2., cvutils.cvBlue, 2)
     cv2.imshow('video frame',videoImg)
-    #cv2.imshow('world image',worldImg)
+    cv2.imshow('world image',worldImg)
     cv2.waitKey()
     cv2.destroyAllWindows()
--- a/scripts/display-trajectories.py	Tue Mar 25 19:43:47 2014 -0400
+++ b/scripts/display-trajectories.py	Thu Mar 27 11:40:28 2014 -0400
@@ -15,6 +15,7 @@
 parser.add_argument('-o', dest = 'homography', help = 'name of the image to world homography')
 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', default = 0, type = int)
 parser.add_argument('-r', dest = 'rescale', help = 'rescaling factor for the displayed image', default = 1., type = float)
+parser.add_argument('-s', dest = 'nFramesStep', help = 'number of frames between each display', default = 1, type = int)
 
 args = parser.parse_args()
 
@@ -38,4 +39,4 @@
 
 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType)
 boundingBoxes = storage.loadBoundingBoxTable(databaseFilename)
-cvutils.displayTrajectories(videoFilename, objects, boundingBoxes, homography, firstFrameNum, rescale = args.rescale)
+cvutils.displayTrajectories(videoFilename, objects, boundingBoxes, homography, firstFrameNum, rescale = args.rescale, nFramesStep = args.nFramesStep)