comparison python/cvutils.py @ 895:739acd338cc0

added script to extract camera info from tacal file by Lund University (T analyst)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 01 Jun 2017 14:54:11 -0400
parents 0c1fed9e8862
children f5a49b603e8b
comparison
equal deleted inserted replaced
894:0c1fed9e8862 895:739acd338cc0
386 projected = camera.image_to_world(tuple(srcPoint)) 386 projected = camera.image_to_world(tuple(srcPoint))
387 dstPoints.append([projected[0], projected[1]]) 387 dstPoints.append([projected[0], projected[1]])
388 H, mask = cv2.findHomography(array(srcPoints), array(dstPoints), method = 0) # No need for different methods for finding homography 388 H, mask = cv2.findHomography(array(srcPoints), array(dstPoints), method = 0) # No need for different methods for finding homography
389 return H 389 return H
390 390
391 def getIntrinsicCameraMatrix(cameraData):
392 return array([[cameraData['f']*cameraData['Sx']/cameraData['dx'], 0, cameraData['Cx']],
393 [0, cameraData['f']/cameraData['dy'], cameraData['Cy']],
394 [0, 0, 1.]])
395
396 def getDistortionCoefficients(cameraData):
397 return array([cameraData['k']]+4*[0])
398
391 def undistortedCoordinates(map1, map2, x, y, maxDistance = 1.): 399 def undistortedCoordinates(map1, map2, x, y, maxDistance = 1.):
392 '''Returns the coordinates of a point in undistorted image 400 '''Returns the coordinates of a point in undistorted image
393 map1 and map2 are the mapping functions from undistorted image 401 map1 and map2 are the mapping functions from undistorted image
394 to distorted (original image) 402 to distorted (original image)
395 map1(x,y) = originalx, originaly''' 403 map1(x,y) = originalx, originaly'''
478 else: 486 else:
479 print('Pattern not found in '+fname) 487 print('Pattern not found in '+fname)
480 ## Close up image loading and calibrate 488 ## Close up image loading and calibrate
481 cv2.destroyAllWindows() 489 cv2.destroyAllWindows()
482 if len(objpoints) == 0 or len(imgpoints) == 0: 490 if len(objpoints) == 0 or len(imgpoints) == 0:
483 return False 491 return None
484 try: 492 try:
485 ret, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None) 493 ret, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
486 except NameError: 494 except NameError:
487 return False 495 return None
488 savetxt('intrinsic-camera.txt', camera_matrix) 496 savetxt('intrinsic-camera.txt', camera_matrix)
489 return camera_matrix, dist_coeffs 497 return camera_matrix, dist_coeffs
490 498
491 def undistortImage(img, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., interpolation=cv2.INTER_LINEAR): 499 def undistortImage(img, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., interpolation=cv2.INTER_LINEAR):
492 '''Undistorts the image passed in argument''' 500 '''Undistorts the image passed in argument'''