comparison python/cvutils.py @ 933:8ac7f61c6e4f

major rework of homography calibration, no in ideal points if correcting for distortion
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 14 Jul 2017 02:11:21 -0400
parents 66f382852e61
children 0e63a918a1ca
comparison
equal deleted inserted replaced
932:66f382852e61 933:8ac7f61c6e4f
122 cv2.imshow(windowName, img) 122 cv2.imshow(windowName, img)
123 123
124 def computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients): 124 def computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients):
125 newImgSize = (int(round(width*undistortedImageMultiplication)), int(round(height*undistortedImageMultiplication))) 125 newImgSize = (int(round(width*undistortedImageMultiplication)), int(round(height*undistortedImageMultiplication)))
126 newCameraMatrix = cv2.getDefaultNewCameraMatrix(intrinsicCameraMatrix, newImgSize, True) 126 newCameraMatrix = cv2.getDefaultNewCameraMatrix(intrinsicCameraMatrix, newImgSize, True)
127 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), None, newCameraMatrix, newImgSize, cv2.CV_32FC1) 127 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), None, newCameraMatrix, newImgSize, cv2.CV_32FC1), newCameraMatrix
128 128
129 def playVideo(filenames, windowNames = None, firstFrameNums = None, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1, colorBlind = False): 129 def playVideo(filenames, windowNames = None, firstFrameNums = None, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1, colorBlind = False):
130 '''Plays the video(s)''' 130 '''Plays the video(s)'''
131 if colorBlind: 131 if colorBlind:
132 colorType = 'colorblind' 132 colorType = 'colorblind'
303 windowName = 'frame' 303 windowName = 'frame'
304 if rescale == 1.: 304 if rescale == 1.:
305 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) 305 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
306 306
307 if undistort: # setup undistortion 307 if undistort: # setup undistortion
308 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) 308 [map1, map2], newCameraMatrix = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients)
309 if capture.isOpened(): 309 if capture.isOpened():
310 key = -1 310 key = -1
311 ret = True 311 ret = True
312 frameNum = firstFrameNum 312 frameNum = firstFrameNum
313 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) 313 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum)
328 print('frame {0}'.format(frameNum)) 328 print('frame {0}'.format(frameNum))
329 # plot objects 329 # plot objects
330 for obj in objects: 330 for obj in objects:
331 if obj.existsAtInstant(frameNum): 331 if obj.existsAtInstant(frameNum):
332 if not hasattr(obj, 'projectedPositions'): 332 if not hasattr(obj, 'projectedPositions'):
333 if homography is not None: 333 obj.projectedPositions = obj.getPositions().homographyProject(homography)
334 obj.projectedPositions = obj.positions.project(homography) 334 if undistort:
335 else: 335 obj.projectedPositions = obj.projectedPositions.newCameraProject(newCameraMatrix)
336 obj.projectedPositions = obj.positions 336 #obj.projectedPositions = obj.positions
337 cvPlot(img, obj.projectedPositions, cvColors[colorType][obj.getNum()], frameNum-obj.getFirstInstant()) 337 cvPlot(img, obj.projectedPositions, cvColors[colorType][obj.getNum()], frameNum-obj.getFirstInstant())
338 if frameNum not in boundingBoxes.keys() and obj.hasFeatures(): 338 if frameNum not in boundingBoxes.keys() and obj.hasFeatures():
339 yCropMin, yCropMax, xCropMin, xCropMax = imageBoxSize(obj, frameNum, homography, width, height) 339 yCropMin, yCropMax, xCropMin, xCropMax = imageBoxSize(obj, frameNum, homography, width, height)
340 cv2.rectangle(img, (xCropMin, yCropMin), (xCropMax, yCropMax), cvBlue[colorType], 1) 340 cv2.rectangle(img, (xCropMin, yCropMin), (xCropMax, yCropMax), cvBlue[colorType], 1)
341 objDescription = '{} '.format(obj.num) 341 objDescription = '{} '.format(obj.num)