Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.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 | ad518f0c3218 |
| children | 727e3c529519 |
comparison
equal
deleted
inserted
replaced
| 505:35c99776e593 | 513:dbf4b83afbb9 |
|---|---|
| 107 size = (int(round(img.shape[1]*rescale)), int(round(img.shape[0]*rescale))) | 107 size = (int(round(img.shape[1]*rescale)), int(round(img.shape[0]*rescale))) |
| 108 resizedImg = resize(img, size) | 108 resizedImg = resize(img, size) |
| 109 cv2.imshow(windowName, resizedImg) | 109 cv2.imshow(windowName, resizedImg) |
| 110 else: | 110 else: |
| 111 cv2.imshow(windowName, img) | 111 cv2.imshow(windowName, img) |
| 112 | |
| 113 def computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients): | |
| 114 from copy import deepcopy | |
| 115 from numpy import identity, array | |
| 116 newImgSize = (int(round(width*undistortedImageMultiplication)), int(round(height*undistortedImageMultiplication))) | |
| 117 newCameraMatrix = deepcopy(intrinsicCameraMatrix) | |
| 118 newCameraMatrix[0,2] = newImgSize[0]/2. | |
| 119 newCameraMatrix[1,2] = newImgSize[1]/2. | |
| 120 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1) | |
| 112 | 121 |
| 113 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1.): | 122 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1.): |
| 114 '''Plays the video''' | 123 '''Plays the video''' |
| 115 wait = 5 | 124 wait = 5 |
| 116 if frameRate > 0: | 125 if frameRate > 0: |
| 200 else: | 209 else: |
| 201 imgcrop = [] | 210 imgcrop = [] |
| 202 return imgcrop, yCropMin, yCropMax, xCropMin, xCropMax | 211 return imgcrop, yCropMin, yCropMax, xCropMin, xCropMax |
| 203 | 212 |
| 204 | 213 |
| 205 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False): | 214 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1.): |
| 206 '''Displays the objects overlaid frame by frame over the video ''' | 215 '''Displays the objects overlaid frame by frame over the video ''' |
| 207 from moving import userTypeNames | 216 from moving import userTypeNames |
| 208 from math import ceil, log10 | 217 from math import ceil, log10 |
| 218 | |
| 209 capture = cv2.VideoCapture(videoFilename) | 219 capture = cv2.VideoCapture(videoFilename) |
| 210 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) | 220 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) |
| 211 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) | 221 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) |
| 222 | |
| 223 if undistort: # setup undistortion | |
| 224 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) | |
| 212 if capture.isOpened(): | 225 if capture.isOpened(): |
| 213 key = -1 | 226 key = -1 |
| 214 ret = True | 227 ret = True |
| 215 frameNum = firstFrameNum | 228 frameNum = firstFrameNum |
| 216 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) | 229 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) |
| 221 lastFrameNum = lastFrameNumArg | 234 lastFrameNum = lastFrameNumArg |
| 222 nZerosFilename = int(ceil(log10(lastFrameNum))) | 235 nZerosFilename = int(ceil(log10(lastFrameNum))) |
| 223 while ret and not quitKey(key) and frameNum < lastFrameNum: | 236 while ret and not quitKey(key) and frameNum < lastFrameNum: |
| 224 ret, img = capture.read() | 237 ret, img = capture.read() |
| 225 if ret: | 238 if ret: |
| 239 if undistort: | |
| 240 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) | |
| 226 if printFrames: | 241 if printFrames: |
| 227 print('frame {0}'.format(frameNum)) | 242 print('frame {0}'.format(frameNum)) |
| 228 for obj in objects: | 243 for obj in objects: |
| 229 if obj.existsAtInstant(frameNum): | 244 if obj.existsAtInstant(frameNum): |
| 230 if not hasattr(obj, 'projectedPositions'): | 245 if not hasattr(obj, 'projectedPositions'): |
