Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/compute-homography.py @ 513:dbf4b83afbb9
pulled in and merged the new functionalities to deal with camera distortion (eg GoPro cameras)
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 04 Jun 2014 10:57:09 -0400 |
| parents | b0dac840c24f |
| children | bd1ad468e928 |
comparison
equal
deleted
inserted
replaced
| 505:35c99776e593 | 513:dbf4b83afbb9 |
|---|---|
| 23 parser.add_argument('-i', dest = 'videoFrameFilename', help = 'filename of the video frame') | 23 parser.add_argument('-i', dest = 'videoFrameFilename', help = 'filename of the video frame') |
| 24 parser.add_argument('-w', dest = 'worldFilename', help = 'filename of the aerial photo/ground map') | 24 parser.add_argument('-w', dest = 'worldFilename', help = 'filename of the aerial photo/ground map') |
| 25 parser.add_argument('-n', dest = 'nPoints', help = 'number of corresponding points to input', default = 4, type = int) | 25 parser.add_argument('-n', dest = 'nPoints', help = 'number of corresponding points to input', default = 4, type = int) |
| 26 parser.add_argument('-u', dest = 'unitsPerPixel', help = 'number of units per pixel', default = 1., type = float) | 26 parser.add_argument('-u', dest = 'unitsPerPixel', help = 'number of units per pixel', default = 1., type = float) |
| 27 parser.add_argument('--display', dest = 'displayPoints', help = 'display original and projected points on both images', action = 'store_true') | 27 parser.add_argument('--display', dest = 'displayPoints', help = 'display original and projected points on both images', action = 'store_true') |
| 28 parser.add_argument('--intrinsic', dest = 'intrinsicCameraMatrixFilename', help = 'name of the intrinsic camera file') | |
| 29 parser.add_argument('--distortion-coefficients', dest = 'distortionCoefficients', help = 'distortion coefficients', nargs = '*', type = float) | |
| 30 parser.add_argument('--undistorted-multiplication', dest = 'undistortedImageMultiplication', help = 'undistorted image multiplication', type = float) | |
| 31 parser.add_argument('--undistort', dest = 'undistort', help = 'undistort the video (because features have been extracted that way)', action = 'store_true') | |
| 28 | 32 |
| 29 args = parser.parse_args() | 33 args = parser.parse_args() |
| 30 | 34 |
| 31 # TODO process camera intrinsic and extrinsic parameters to obtain image to world homography, taking example from Work/src/python/generate-homography.py script | 35 # TODO process camera intrinsic and extrinsic parameters to obtain image to world homography, taking example from Work/src/python/generate-homography.py script |
| 32 # cameraMat = load(videoFilenamePrefix+'-camera.txt'); | 36 # cameraMat = load(videoFilenamePrefix+'-camera.txt'); |
| 74 worldPts, videoPts = cvutils.loadPointCorrespondences(args.pointCorrespondencesFilename) | 78 worldPts, videoPts = cvutils.loadPointCorrespondences(args.pointCorrespondencesFilename) |
| 75 homography, mask = cv2.findHomography(videoPts, worldPts) # method=0, ransacReprojThreshold=3 | 79 homography, mask = cv2.findHomography(videoPts, worldPts) # method=0, ransacReprojThreshold=3 |
| 76 elif args.videoFrameFilename != None and args.worldFilename != None: | 80 elif args.videoFrameFilename != None and args.worldFilename != None: |
| 77 worldImg = plt.imread(args.worldFilename) | 81 worldImg = plt.imread(args.worldFilename) |
| 78 videoImg = plt.imread(args.videoFrameFilename) | 82 videoImg = plt.imread(args.videoFrameFilename) |
| 83 if args.undistort: | |
| 84 [map1, map2] = cvutils.computeUndistortMaps(videoImg.shape[1], videoImg.shape[0], args.undistortedImageMultiplication, np.loadtxt(args.intrinsicCameraMatrixFilename), args.distortionCoefficients) | |
| 85 videoImg = cv2.remap(videoImg, map1, map2, interpolation=cv2.INTER_LINEAR) | |
| 79 print('Click on {0} points in the video frame'.format(args.nPoints)) | 86 print('Click on {0} points in the video frame'.format(args.nPoints)) |
| 80 plt.figure() | 87 plt.figure() |
| 81 plt.imshow(videoImg) | 88 plt.imshow(videoImg) |
| 82 videoPts = np.array(plt.ginput(args.nPoints, timeout=3000)) | 89 videoPts = np.array(plt.ginput(args.nPoints, timeout=3000)) |
| 83 print('Click on {0} points in the world image'.format(args.nPoints)) | 90 print('Click on {0} points in the world image'.format(args.nPoints)) |
| 96 np.savetxt('homography.txt',homography) | 103 np.savetxt('homography.txt',homography) |
| 97 | 104 |
| 98 if args.displayPoints and args.videoFrameFilename != None and args.worldFilename != None and homography.size>0: | 105 if args.displayPoints and args.videoFrameFilename != None and args.worldFilename != None and homography.size>0: |
| 99 worldImg = cv2.imread(args.worldFilename) | 106 worldImg = cv2.imread(args.worldFilename) |
| 100 videoImg = cv2.imread(args.videoFrameFilename) | 107 videoImg = cv2.imread(args.videoFrameFilename) |
| 108 if args.undistort: | |
| 109 [map1, map2] = cvutils.computeUndistortMaps(videoImg.shape[1], videoImg.shape[0], args.undistortedImageMultiplication, np.loadtxt(args.intrinsicCameraMatrixFilename), args.distortionCoefficients) | |
| 110 videoImg = cv2.remap(videoImg, map1, map2, interpolation=cv2.INTER_LINEAR) | |
| 101 invHomography = np.linalg.inv(homography) | 111 invHomography = np.linalg.inv(homography) |
| 102 projectedWorldPts = cvutils.projectArray(invHomography, worldPts.T).T | 112 projectedWorldPts = cvutils.projectArray(invHomography, worldPts.T).T |
| 103 projectedVideoPts = cvutils.projectArray(homography, videoPts.T).T | 113 projectedVideoPts = cvutils.projectArray(homography, videoPts.T).T |
| 104 for i in range(worldPts.shape[0]): | 114 for i in range(worldPts.shape[0]): |
| 105 # world image | 115 # world image |
