changeset 936:56cc8a1f7082

removed all old versions of projection methods
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 14 Jul 2017 16:48:42 -0400
parents 0e63a918a1ca
children b67a784beb69
files python/cvutils.py python/moving.py scripts/display-synced-trajectories.py scripts/rescale-homography.py scripts/test-compute-object-position-from-features.py
diffstat 5 files changed, 8 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/python/cvutils.py	Fri Jul 14 16:30:57 2017 -0400
+++ b/python/cvutils.py	Fri Jul 14 16:48:42 2017 -0400
@@ -333,7 +333,6 @@
                                 obj.projectedPositions = obj.getPositions().homographyProject(homography)
                                 if undistort:
                                     obj.projectedPositions = obj.projectedPositions.newCameraProject(newCameraMatrix)
-                                #obj.projectedPositions = obj.positions
                             cvPlot(img, obj.projectedPositions, cvColors[colorType][obj.getNum()], frameNum-obj.getFirstInstant())
                             if frameNum not in boundingBoxes.keys() and obj.hasFeatures():
                                 yCropMin, yCropMax, xCropMin, xCropMax = imageBoxSize(obj, frameNum, homography, width, height)
@@ -577,70 +576,6 @@
     else:
         return points
 
-def projectArray(homography, points, intrinsicCameraMatrix = None, distortionCoefficients = None, newCameraMatrix = None):
-    '''Returns the coordinates of the projected points through homography
-    (format: array 2xN points)'''
-    if points.shape[0] != 2:
-        raise Exception('points of dimension {}'.format(points.shape))
-
-    augmentedPoints = append(points,[[1]*points.shape[1]], 0) # 3xN
-    if homography is not None and homography.size>0:
-        prod = dot(homography, augmentedPoints)
-        projected = prod/prod[2]
-    else:
-        projected = augmentedPoints
-
-    if intrinsicCameraMatrix is not None and distortionCoefficients is not None:
-        if newCameraMatrix is not None:
-            invNewCameraMatrix = inv(newCameraMatrix)
-            reducedPoints = dot(invNewCameraMatrix, projected)
-        else:
-            reducedPoints = projected
-        projected, jacobian = cv2.projectPoints(reducedPoints.T, (0.,0.,0.), (0.,0.,0.), intrinsicCameraMatrix, distortionCoefficients) # in: 3xN, out: 2x1xN
-        projected = projected.reshape(-1,2).T
-    return projected[:2,:]
-
-def project(homography, p, intrinsicCameraMatrix = None, distortionCoefficients = None, newCameraMatrix = None):
-    '''Returns the coordinates of the projection of the point p with coordinates p[0], p[1]
-    through homography'''
-    return projectArray(homography, array([[p[0]],[p[1]]]), intrinsicCameraMatrix, distortionCoefficients, newCameraMatrix)
-
-def projectTrajectory(homography, trajectory, intrinsicCameraMatrix = None, distortionCoefficients = None, newCameraMatrix = None):
-    '''Projects a series of points in the format
-    [[x1, x2, ...],
-    [y1, y2, ...]]'''
-    return projectArray(homography, array(trajectory), intrinsicCameraMatrix, distortionCoefficients, newCameraMatrix)
-
-def invertHomography(homography):
-    '''Returns an inverted homography
-    Unnecessary for reprojection over camera image'''
-    invH = inv(homography)
-    invH /= invH[2,2]
-    return invH
-
-def undistortTrajectory(invMap1, invMap2, positions):
-    floorPositions = npfloor(positions)
-    #ceilPositions = npceil(positions)
-    undistortedTrajectory = [[],[]]
-    for i in xrange(len(positions[0])):
-        x,y = None, None
-        if positions[0][i]+1 < invMap1.shape[1] and positions[1][i]+1 < invMap1.shape[0]:
-            floorX = invMap1[floorPositions[1][i], floorPositions[0][i]]
-            floorY = invMap2[floorPositions[1][i], floorPositions[0][i]]
-            ceilX = invMap1[floorPositions[1][i]+1, floorPositions[0][i]+1]
-            ceilY = invMap2[floorPositions[1][i]+1, floorPositions[0][i]+1]
-            #ceilX = invMap1[ceilPositions[1][i], ceilPositions[0][i]]
-            #ceilY = invMap2[ceilPositions[1][i], ceilPositions[0][i]]
-            if floorX >=0 and floorY >=0 and ceilX >=0 and ceilY >=0:
-                x = floorX+(positions[0][i]-floorPositions[0][i])*(ceilX-floorX)
-                y = floorY+(positions[1][i]-floorPositions[1][i])*(ceilY-floorY)
-        undistortedTrajectory[0].append(x)
-        undistortedTrajectory[1].append(y)
-    return undistortedTrajectory
-
-def projectGInputPoints(homography, points):
-    return projectTrajectory(homography, array(points+[points[0]]).T)
-
 if opencvAvailable:
     def computeTranslation(img1, img2, img1Points, maxTranslation2, minNMatches, windowSize = (5,5), level = 5, criteria = (cv2.TERM_CRITERIA_EPS, 0, 0.01)):
         '''Computes the translation of img2 with respect to img1
--- a/python/moving.py	Fri Jul 14 16:30:57 2017 -0400
+++ b/python/moving.py	Fri Jul 14 16:48:42 2017 -0400
@@ -263,8 +263,8 @@
         def asShapely(self):
             return shapelyPoint(self.x, self.y)
 
-    def project(self, homography):
-        projected = cvutils.projectArray(homography, array([[self.x], [self.y]]))
+    def homographyProject(self, homography):
+        projected = cvutils.homographyProject(array([[self.x], [self.y]]), homography)
         return Point(projected[0], projected[1])
 
     def inPolygon(self, polygon):
@@ -1173,7 +1173,7 @@
         '''Returns new objects made of the positions in the mask
         mask is in the destination of the homography space'''
         if homography is not None:
-            self.projectedPositions = self.positions.project(homography)
+            self.projectedPositions = self.positions.homographyProject(homography)
         else:
             self.projectedPositions = self.positions
         def inMask(positions, i, mask):
@@ -1644,7 +1644,7 @@
         print('not implemented/tested yet')
         if not hasattr(self, projectedPositions):
             if homography is not None:
-                self.projectedPositions = obj.positions.project(homography)
+                self.projectedPositions = obj.positions.homographyProject(homography)
             else:
                 self.projectedPositions = obj.positions
         possibleUserTypes = {userType: 0 for userType in range(len(userTypenames))}
@@ -1716,7 +1716,7 @@
     def computeCentroidTrajectory(self, homography = None):
         self.positions = self.topLeftPositions.add(self.bottomRightPositions).multiply(0.5)
         if homography is not None:
-            self.positions = self.positions.project(homography)
+            self.positions = self.positions.homographyProject(homography)
 
     def matches(self, obj, instant, matchingDistance):
         '''Indicates if the annotation matches obj (MovingObject)
--- a/scripts/display-synced-trajectories.py	Fri Jul 14 16:30:57 2017 -0400
+++ b/scripts/display-synced-trajectories.py	Fri Jul 14 16:48:42 2017 -0400
@@ -98,7 +98,7 @@
                             #print obj.num, obj.timeInterval, mergedFirstFrameNum, nFramesShown
                             if i not in obj.projectedPositions:
                                 if homographies[i] is not None:
-                                    obj.projectedPositions[i] = obj.positions.project(homographies[i])
+                                    obj.projectedPositions[i] = obj.positions.homographyProject(homographies[i])
                                 else:
                                     obj.projectedPositions[i] = obj.positions
                             cvutils.cvPlot(images[i], obj.projectedPositions[i], cvutils.cvColors['default'][obj.getNum()], int(mergedFirstFrameNum+nFramesShown)-obj.getFirstInstant())
--- a/scripts/rescale-homography.py	Fri Jul 14 16:30:57 2017 -0400
+++ b/scripts/rescale-homography.py	Fri Jul 14 16:48:42 2017 -0400
@@ -20,7 +20,7 @@
                       [20,20],
                       [20,10]])
 
-wldPoints = cvutils.projectArray(homography, imgPoints.T).T
+wldPoints = cvutils.homographyProject(imgPoints.T, homography).T
 
 newSize = float(sys.argv[3])
 originalSize = float(sys.argv[2])
--- a/scripts/test-compute-object-position-from-features.py	Fri Jul 14 16:30:57 2017 -0400
+++ b/scripts/test-compute-object-position-from-features.py	Fri Jul 14 16:48:42 2017 -0400
@@ -41,7 +41,7 @@
     yCoordinates = -np.ones((len(features),int(timeInterval.length())))
     for i,f in enumerate(features):
         traj = f.getPositions().asArray()
-        imgTraj = cvutils.projectArray(homography, traj)
+        imgTraj = cvutils.homographyProject(traj, homography)
         yCoordinates[i,f.getFirstInstant()-timeInterval.first:f.getLastInstant()+1-timeInterval.first] = imgTraj[1,:]
 
     indices = np.argmax(yCoordinates,0)