Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.py @ 509:935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 23 May 2014 16:27:26 -0400 |
| parents | 343cfd185ca6 |
| children | b0dac840c24f |
comparison
equal
deleted
inserted
replaced
| 508:6f7fa0093162 | 509:935430b1d408 |
|---|---|
| 200 else: | 200 else: |
| 201 imgcrop = [] | 201 imgcrop = [] |
| 202 return imgcrop, yCropMin, yCropMax, xCropMin, xCropMax | 202 return imgcrop, yCropMin, yCropMax, xCropMin, xCropMax |
| 203 | 203 |
| 204 | 204 |
| 205 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False): | 205 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 ''' | 206 '''Displays the objects overlaid frame by frame over the video ''' |
| 207 from moving import userTypeNames | 207 from moving import userTypeNames |
| 208 from math import ceil, log10 | 208 from math import ceil, log10 |
| 209 from numpy import identity, array | |
| 210 | |
| 209 capture = cv2.VideoCapture(videoFilename) | 211 capture = cv2.VideoCapture(videoFilename) |
| 210 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) | 212 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) |
| 211 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) | 213 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) |
| 214 | |
| 215 if undistort: # setup undistortion | |
| 216 newImgSize = (int(round(width*undistortedImageMultiplication)), int(round(height*undistortedImageMultiplication))) | |
| 217 from copy import deepcopy | |
| 218 newCameraMatrix = deepcopy(intrinsicCameraMatrix) | |
| 219 newCameraMatrix[0,2] = newImgSize[0]/2. | |
| 220 newCameraMatrix[1,2] = newImgSize[1]/2. | |
| 221 [map1, map2] = cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1) | |
| 222 | |
| 212 if capture.isOpened(): | 223 if capture.isOpened(): |
| 213 key = -1 | 224 key = -1 |
| 214 ret = True | 225 ret = True |
| 215 frameNum = firstFrameNum | 226 frameNum = firstFrameNum |
| 216 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) | 227 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) |
| 221 lastFrameNum = lastFrameNumArg | 232 lastFrameNum = lastFrameNumArg |
| 222 nZerosFilename = int(ceil(log10(lastFrameNum))) | 233 nZerosFilename = int(ceil(log10(lastFrameNum))) |
| 223 while ret and not quitKey(key) and frameNum < lastFrameNum: | 234 while ret and not quitKey(key) and frameNum < lastFrameNum: |
| 224 ret, img = capture.read() | 235 ret, img = capture.read() |
| 225 if ret: | 236 if ret: |
| 237 if undistort: | |
| 238 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) | |
| 226 if printFrames: | 239 if printFrames: |
| 227 print('frame {0}'.format(frameNum)) | 240 print('frame {0}'.format(frameNum)) |
| 228 for obj in objects: | 241 for obj in objects: |
| 229 if obj.existsAtInstant(frameNum): | 242 if obj.existsAtInstant(frameNum): |
| 230 if not hasattr(obj, 'projectedPositions'): | 243 if not hasattr(obj, 'projectedPositions'): |
