Mercurial Hosting > traffic-intelligence
comparison python/cvutils.py @ 932:66f382852e61
added new projection functions
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 14 Jul 2017 00:12:03 -0400 |
parents | 8148991b1dab |
children | 8ac7f61c6e4f |
comparison
equal
deleted
inserted
replaced
931:8148991b1dab | 932:66f382852e61 |
---|---|
517 for i in xrange(cvmat.rows): | 517 for i in xrange(cvmat.rows): |
518 for j in xrange(cvmat.cols): | 518 for j in xrange(cvmat.cols): |
519 out.write('{0} '.format(cvmat[i,j])) | 519 out.write('{0} '.format(cvmat[i,j])) |
520 out.write('\n') | 520 out.write('\n') |
521 | 521 |
522 def homographyProject(points, homography, output3D = False): | |
523 '''Returns the coordinates of the points (2xN array) projected through homography''' | |
524 if points.shape[0] != 2: | |
525 raise Exception('points of dimension {}'.format(points.shape)) | |
526 | |
527 if homography is not None and homography.size>0: | |
528 if output3D: | |
529 outputDim = 3 | |
530 else: | |
531 outputDim = 2 | |
532 augmentedPoints = append(points,[[1]*points.shape[1]], 0) # 3xN | |
533 prod = dot(homography, augmentedPoints) | |
534 return prod[:outputDim,:]/prod[2] | |
535 elif output3D: | |
536 return append(points,[[1]*points.shape[1]], 0) # 3xN | |
537 else: | |
538 return points | |
539 | |
540 def imageToWorldProject(points, intrinsicCameraMatrix = None, distortionCoefficients = None, homography = None): | |
541 '''Projects points (2xN array) from image (video) space to world space | |
542 1. through undistorting if provided by intrinsic camera matrix and distortion coefficients | |
543 2. through homograph projection (from ideal point (no camera) to world)''' | |
544 if points.shape[0] != 2: | |
545 raise Exception('points of dimension {}'.format(points.shape)) | |
546 | |
547 if intrinsicCameraMatrix is not None and distortionCoefficients is not None: | |
548 undistortedPoints = cv2.undistortPoints(points.T.reshape(1,points.shape[1], 2), intrinsicCameraMatrix, distortionCoefficients).reshape(-1,2) | |
549 return homographyProject(undistortedPoints.T, homography) | |
550 else: | |
551 return homographyProject(points, homography) | |
552 | |
553 def worldToImageProject(points, intrinsicCameraMatrix = None, distortionCoefficients = None, homography = None): | |
554 '''Projects points (2xN array) from image (video) space to world space | |
555 1. through undistorting if provided by intrinsic camera matrix and distortion coefficients | |
556 2. through homograph projection (from ideal point (no camera) to world)''' | |
557 if points.shape[0] != 2: | |
558 raise Exception('points of dimension {}'.format(points.shape)) | |
559 | |
560 if intrinsicCameraMatrix is not None and distortionCoefficients is not None: | |
561 projected3D = homographyProject(points, homography, True) | |
562 projected, jacobian = cv2.projectPoints(projected3D.T, (0.,0.,0.), (0.,0.,0.), intrinsicCameraMatrix, distortionCoefficients) # in: 3xN, out: 2x1xN | |
563 return projected.reshape(-1,2).T | |
564 else: | |
565 return homographyProject(points, homography) | |
566 | |
567 def newCameraProject(points, newCameraMatrix): | |
568 '''Projects points (2xN array) as if seen by camera | |
569 (or reverse by inverting the camera matrix)''' | |
570 if points.shape[0] != 2: | |
571 raise Exception('points of dimension {}'.format(points.shape)) | |
572 | |
573 if newCameraMatrix is not None: | |
574 augmentedPoints = append(points,[[1]*points.shape[1]], 0) # 3xN | |
575 projected = dot(newCameraMatrix, augmentedPoints) | |
576 return projected[:2,:] | |
577 else: | |
578 return points | |
579 | |
522 def projectArray(homography, points, intrinsicCameraMatrix = None, distortionCoefficients = None, newCameraMatrix = None): | 580 def projectArray(homography, points, intrinsicCameraMatrix = None, distortionCoefficients = None, newCameraMatrix = None): |
523 '''Returns the coordinates of the projected points through homography | 581 '''Returns the coordinates of the projected points through homography |
524 (format: array 2xN points)''' | 582 (format: array 2xN points)''' |
525 if points.shape[0] != 2: | 583 if points.shape[0] != 2: |
526 raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1])) | 584 raise Exception('points of dimension {}'.format(points.shape)) |
527 | 585 |
528 augmentedPoints = append(points,[[1]*points.shape[1]], 0) # 3xN | 586 augmentedPoints = append(points,[[1]*points.shape[1]], 0) # 3xN |
529 if homography is not None and homography.size>0: | 587 if homography is not None and homography.size>0: |
530 prod = dot(homography, augmentedPoints) | 588 prod = dot(homography, augmentedPoints) |
531 projected = prod/prod[2] | 589 projected = prod/prod[2] |