comparison python/cvutils.py @ 926:dbd81710d515

new feature tracking in image space with point undistortion
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 10 Jul 2017 18:04:41 -0400
parents a71455bd8367
children 063d1267585d
comparison
equal deleted inserted replaced
925:974077e23804 926:dbd81710d515
120 else: 120 else:
121 cv2.imshow(windowName, img) 121 cv2.imshow(windowName, img)
122 122
123 def computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients): 123 def computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients):
124 newImgSize = (int(round(width*undistortedImageMultiplication)), int(round(height*undistortedImageMultiplication))) 124 newImgSize = (int(round(width*undistortedImageMultiplication)), int(round(height*undistortedImageMultiplication)))
125 #newCameraMatrix = deepcopy(intrinsicCameraMatrix)
126 #newCameraMatrix[0,2] = newImgSize[0]/2.
127 #newCameraMatrix[1,2] = newImgSize[1]/2.
128 newCameraMatrix = cv2.getDefaultNewCameraMatrix(intrinsicCameraMatrix, newImgSize, True) 125 newCameraMatrix = cv2.getDefaultNewCameraMatrix(intrinsicCameraMatrix, newImgSize, True)
129 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), None, newCameraMatrix, newImgSize, cv2.CV_32FC1) 126 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), None, newCameraMatrix, newImgSize, cv2.CV_32FC1)
130 127
131 def playVideo(filenames, windowNames = None, firstFrameNums = None, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1, colorBlind = False): 128 def playVideo(filenames, windowNames = None, firstFrameNums = None, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1, colorBlind = False):
132 '''Plays the video(s)''' 129 '''Plays the video(s)'''
430 if not isnan(res).any(): 427 if not isnan(res).any():
431 invMap1[y,x] = res[0] 428 invMap1[y,x] = res[0]
432 invMap2[y,x] = res[1] 429 invMap2[y,x] = res[1]
433 return invMap1, invMap2 430 return invMap1, invMap2
434 431
435 def cameraIntrinsicCalibration(path, checkerBoardSize=[6,7], secondPassSearch=False, display=False): 432 def intrinsicCameraCalibration(path, checkerBoardSize=[6,7], secondPassSearch=False, display=False, fixK2 = True, fixK3 = True, zeroTangent = True):
436 ''' Camera calibration searches through all the images (jpg or png) located 433 ''' Camera calibration searches through all the images (jpg or png) located
437 in _path_ for matches to a checkerboard pattern of size checkboardSize. 434 in _path_ for matches to a checkerboard pattern of size checkboardSize.
438 These images should all be of the same camera with the same resolution. 435 These images should all be of the same camera with the same resolution.
439 436
440 For best results, use an asymetric board and ensure that the image has 437 For best results, use an asymetric board and ensure that the image has
489 ## Close up image loading and calibrate 486 ## Close up image loading and calibrate
490 cv2.destroyAllWindows() 487 cv2.destroyAllWindows()
491 if len(objpoints) == 0 or len(imgpoints) == 0: 488 if len(objpoints) == 0 or len(imgpoints) == 0:
492 return None 489 return None
493 try: 490 try:
494 ret, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None) 491 flags = 0
492 if fixK2:
493 flags += cv2.cv.CV_CALIB_FIX_K2
494 if fixK3:
495 flags += cv2.cv.CV_CALIB_FIX_K3
496 if zeroTangent:
497 flags += cv2.cv.CV_CALIB_ZERO_TANGENT_DIST
498 ret, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None, flags = flags)
495 except NameError: 499 except NameError:
496 return None 500 return None
497 savetxt('intrinsic-camera.txt', camera_matrix) 501 savetxt('intrinsic-camera.txt', camera_matrix)
502 print 'error: {}'.format(ret)
498 return camera_matrix, dist_coeffs 503 return camera_matrix, dist_coeffs
499 504
500 def undistortImage(img, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., interpolation=cv2.INTER_LINEAR): 505 def undistortImage(img, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., interpolation=cv2.INTER_LINEAR):
501 '''Undistorts the image passed in argument''' 506 '''Undistorts the image passed in argument'''
502 width = img.shape[1] 507 width = img.shape[1]