Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.py @ 929:be28a3538dc9
work in progress on projection
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 12 Jul 2017 18:00:53 -0400 |
| parents | 063d1267585d |
| children | 7db0f2853bfd |
comparison
equal
deleted
inserted
replaced
| 928:063d1267585d | 929:be28a3538dc9 |
|---|---|
| 262 x = [] | 262 x = [] |
| 263 y = [] | 263 y = [] |
| 264 if obj.hasFeatures(): | 264 if obj.hasFeatures(): |
| 265 for f in obj.getFeatures(): | 265 for f in obj.getFeatures(): |
| 266 if f.existsAtInstant(frameNum): | 266 if f.existsAtInstant(frameNum): |
| 267 projectedPosition = f.projectPositions[:, frameNum-f.getFirstInstant()] | 267 p = f.getPositionAtInstant(frameNum) |
| 268 x.append(projectedPosition[0]) | 268 x.append(p.x) |
| 269 y.append(projectedPosition[1]) | 269 y.append(p.y) |
| 270 xmin = min(x) | 270 xmin = min(x) |
| 271 xmax = max(x) | 271 xmax = max(x) |
| 272 ymin = min(y) | 272 ymin = min(y) |
| 273 ymax = max(y) | 273 ymax = max(y) |
| 274 xMm = px * (xmax - xmin) | 274 xMm = px * (xmax - xmin) |
| 519 out.write('\n') | 519 out.write('\n') |
| 520 | 520 |
| 521 def projectArray(homography, points, intrinsicCameraMatrix = None, distortionCoefficients = None): | 521 def projectArray(homography, points, intrinsicCameraMatrix = None, distortionCoefficients = None): |
| 522 '''Returns the coordinates of the projected points through homography | 522 '''Returns the coordinates of the projected points through homography |
| 523 (format: array 2xN points)''' | 523 (format: array 2xN points)''' |
| 524 if points.shape[0] not in [2, 3]: | 524 if points.shape[0] != 2: |
| 525 raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1])) | 525 raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1])) |
| 526 | 526 |
| 527 augmentedPoints = append(points,[[1]*points.shape[1]], 0) | 527 augmentedPoints = append(points,[[1]*points.shape[1]], 0) # 3xN |
| 528 if homography is not None and homography.size>0: | 528 if homography is not None and homography.size>0: |
| 529 prod = dot(homography, augmentedPoints) | 529 prod = dot(homography, augmentedPoints) |
| 530 projected = prod/prod[2] | 530 projected = prod/prod[2] |
| 531 projected[3,:] = 0 | |
| 532 else: | 531 else: |
| 533 projected = augmentedPoints | 532 projected = augmentedPoints |
| 534 | 533 |
| 535 if intrinsicCameraMatrix is not None and distortionCoefficients is not None: | 534 if intrinsicCameraMatrix is not None and distortionCoefficients is not None: |
| 536 projected = cv2.projectPoints(projected, None, None, intrinsicCameraMatrix, distortionCoefficients) | 535 #projected[2,:] = 0 |
| 537 return projected | 536 projected, jacobian = cv2.projectPoints(projected.T, (0,0,0), (0,0,0), intrinsicCameraMatrix, distortionCoefficients) # in: 3xN, out: 2x1xN |
| 538 | 537 projected = projected.reshape(-1,2).T |
| 539 def project(homography, p): | 538 return projected[:2,:] |
| 539 | |
| 540 def project(homography, p, intrinsicCameraMatrix = None, distortionCoefficients = None): | |
| 540 '''Returns the coordinates of the projection of the point p with coordinates p[0], p[1] | 541 '''Returns the coordinates of the projection of the point p with coordinates p[0], p[1] |
| 541 through homography''' | 542 through homography''' |
| 542 return projectArray(homography, array([[p[0]],[p[1]]])) | 543 return projectArray(homography, array([[p[0]],[p[1]]]), intrinsicCameraMatrix, distortionCoefficients) |
| 543 | 544 |
| 544 def projectTrajectory(homography, trajectory): | 545 def projectTrajectory(homography, trajectory, intrinsicCameraMatrix = None, distortionCoefficients = None): |
| 545 '''Projects a series of points in the format | 546 '''Projects a series of points in the format |
| 546 [[x1, x2, ...], | 547 [[x1, x2, ...], |
| 547 [y1, y2, ...]]''' | 548 [y1, y2, ...]]''' |
| 548 return projectArray(homography, array(trajectory)) | 549 return projectArray(homography, array(trajectory), intrinsicCameraMatrix, distortionCoefficients) |
| 549 | 550 |
| 550 def invertHomography(homography): | 551 def invertHomography(homography): |
| 551 '''Returns an inverted homography | 552 '''Returns an inverted homography |
| 552 Unnecessary for reprojection over camera image''' | 553 Unnecessary for reprojection over camera image''' |
| 553 from numpy.linalg import inv | 554 from numpy.linalg import inv |
