diff python/cvutils.py @ 928:063d1267585d

work in progress
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 12 Jul 2017 01:24:31 -0400
parents dbd81710d515
children be28a3538dc9
line wrap: on
line diff
--- a/python/cvutils.py	Tue Jul 11 17:56:23 2017 -0400
+++ b/python/cvutils.py	Wed Jul 12 01:24:31 2017 -0400
@@ -257,16 +257,16 @@
             print('Video capture for {} failed'.format(videoFilename))
             return None
         
-    def imageBoxSize(obj, frameNum, homography, width, height, px = 0.2, py = 0.2):
+    def imageBoxSize(obj, frameNum, width, height, px = 0.2, py = 0.2):
         'Computes the bounding box size of object at frameNum'
         x = []
         y = []
         if obj.hasFeatures():
             for f in obj.getFeatures():
                 if f.existsAtInstant(frameNum):
-                    projectedPosition = f.getPositionAtInstant(frameNum).project(homography)
-                    x.append(projectedPosition.x)
-                    y.append(projectedPosition.y)
+                    projectedPosition = f.projectPositions[:, frameNum-f.getFirstInstant()]
+                    x.append(projectedPosition[0])
+                    y.append(projectedPosition[1])
         xmin = min(x)
         xmax = max(x)
         ymin = min(y)
@@ -280,9 +280,9 @@
         xCropMax = int(min(width - 1, .5 * (xmin + xmax + a)))
         return yCropMin, yCropMax, xCropMin, xCropMax
         
-    def imageBox(img, obj, frameNum, homography, width, height, px = 0.2, py = 0.2, minNPixels = 800):
+    def imageBox(img, obj, frameNum, width, height, px = 0.2, py = 0.2, minNPixels = 800):
         'Computes the bounding box of object at frameNum'
-        yCropMin, yCropMax, xCropMin, xCropMax = imageBoxSize(obj, frameNum, homography, width, height, px, py)
+        yCropMin, yCropMax, xCropMin, xCropMax = imageBoxSize(obj, frameNum, width, height, px, py)
         if yCropMax != yCropMin and xCropMax != xCropMin and (yCropMax - yCropMin) * (xCropMax - xCropMin) > minNPixels:
             return img[yCropMin : yCropMax, xCropMin : xCropMax]
         else:
@@ -518,19 +518,23 @@
             out.write('{0} '.format(cvmat[i,j]))
         out.write('\n')
 
-def projectArray(homography, points):
+def projectArray(homography, points, intrinsicCameraMatrix = None, distortionCoefficients = None):
     '''Returns the coordinates of the projected points through homography
     (format: array 2xN points)'''
-    if points.shape[0] != 2:
+    if points.shape[0] not in [2, 3]:
         raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1]))
 
-    if (homography is not None) and homography.size>0:
-        #alternatively, on could use cv2.convertpointstohomogeneous and other conversion to/from homogeneous coordinates
-        augmentedPoints = append(points,[[1]*points.shape[1]], 0)
+    augmentedPoints = append(points,[[1]*points.shape[1]], 0)
+    if homography is not None and homography.size>0:
         prod = dot(homography, augmentedPoints)
-        return prod[0:2]/prod[2]
+        projected = prod/prod[2]
+        projected[3,:] = 0
     else:
-        return points
+        projected = augmentedPoints
+
+    if intrinsicCameraMatrix is not None and distortionCoefficients is not None:
+        projected = cv2.projectPoints(projected, None, None, intrinsicCameraMatrix, distortionCoefficients)
+    return projected
 
 def project(homography, p):
     '''Returns the coordinates of the projection of the point p with coordinates p[0], p[1]