comparison python/cvutils.py @ 930:7db0f2853bfd

corrected projection back to image space
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 13 Jul 2017 00:31:54 -0400
parents be28a3538dc9
children 8148991b1dab
comparison
equal deleted inserted replaced
929:be28a3538dc9 930:7db0f2853bfd
20 from os import listdir 20 from os import listdir
21 from copy import deepcopy 21 from copy import deepcopy
22 from math import floor, log10, ceil 22 from math import floor, log10, ceil
23 23
24 from numpy import dot, array, append, float32, loadtxt, savetxt, append, zeros, ones, identity, abs as npabs, logical_and, unravel_index, sum as npsum, isnan, mgrid, median, floor as npfloor, ceil as npceil 24 from numpy import dot, array, append, float32, loadtxt, savetxt, append, zeros, ones, identity, abs as npabs, logical_and, unravel_index, sum as npsum, isnan, mgrid, median, floor as npfloor, ceil as npceil
25 from numpy.linalg import inv
25 from matplotlib.mlab import find 26 from matplotlib.mlab import find
26 from matplotlib.pyplot import imread, imsave 27 from matplotlib.pyplot import imread, imsave
27 28
28 29
29 30
516 for i in xrange(cvmat.rows): 517 for i in xrange(cvmat.rows):
517 for j in xrange(cvmat.cols): 518 for j in xrange(cvmat.cols):
518 out.write('{0} '.format(cvmat[i,j])) 519 out.write('{0} '.format(cvmat[i,j]))
519 out.write('\n') 520 out.write('\n')
520 521
521 def projectArray(homography, points, intrinsicCameraMatrix = None, distortionCoefficients = None): 522 def projectArray(homography, points, intrinsicCameraMatrix = None, distortionCoefficients = None, newCameraMatrix = None):
522 '''Returns the coordinates of the projected points through homography 523 '''Returns the coordinates of the projected points through homography
523 (format: array 2xN points)''' 524 (format: array 2xN points)'''
524 if points.shape[0] != 2: 525 if points.shape[0] != 2:
525 raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1])) 526 raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1]))
526 527
530 projected = prod/prod[2] 531 projected = prod/prod[2]
531 else: 532 else:
532 projected = augmentedPoints 533 projected = augmentedPoints
533 534
534 if intrinsicCameraMatrix is not None and distortionCoefficients is not None: 535 if intrinsicCameraMatrix is not None and distortionCoefficients is not None:
535 #projected[2,:] = 0 536 if newCameraMatrix is not None:
536 projected, jacobian = cv2.projectPoints(projected.T, (0,0,0), (0,0,0), intrinsicCameraMatrix, distortionCoefficients) # in: 3xN, out: 2x1xN 537 invNewCameraMatrix = inv(newCameraMatrix)
538 reducedPoints = dot(invNewCameraMatrix, projected)
539 else:
540 reducedPoints = projected
541 projected, jacobian = cv2.projectPoints(reducedPoints.T, (0.,0.,0.), (0.,0.,0.), intrinsicCameraMatrix, distortionCoefficients) # in: 3xN, out: 2x1xN
537 projected = projected.reshape(-1,2).T 542 projected = projected.reshape(-1,2).T
538 return projected[:2,:] 543 return projected[:2,:]
539 544
540 def project(homography, p, intrinsicCameraMatrix = None, distortionCoefficients = None): 545 def project(homography, p, intrinsicCameraMatrix = None, distortionCoefficients = None):
541 '''Returns the coordinates of the projection of the point p with coordinates p[0], p[1] 546 '''Returns the coordinates of the projection of the point p with coordinates p[0], p[1]
542 through homography''' 547 through homography'''
543 return projectArray(homography, array([[p[0]],[p[1]]]), intrinsicCameraMatrix, distortionCoefficients) 548 return projectArray(homography, array([[p[0]],[p[1]]]), intrinsicCameraMatrix, distortionCoefficients)
549 return projectArray(homography, array(trajectory), intrinsicCameraMatrix, distortionCoefficients) 554 return projectArray(homography, array(trajectory), intrinsicCameraMatrix, distortionCoefficients)
550 555
551 def invertHomography(homography): 556 def invertHomography(homography):
552 '''Returns an inverted homography 557 '''Returns an inverted homography
553 Unnecessary for reprojection over camera image''' 558 Unnecessary for reprojection over camera image'''
554 from numpy.linalg import inv
555 invH = inv(homography) 559 invH = inv(homography)
556 invH /= invH[2,2] 560 invH /= invH[2,2]
557 return invH 561 return invH
558 562
559 def undistortTrajectory(invMap1, invMap2, positions): 563 def undistortTrajectory(invMap1, invMap2, positions):