camera_calibration_show_extrinsics.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/usr/bin/env python
  2. '''
  3. Plot camera calibration extrinsics.
  4. usage:
  5. camera_calibration_show_extrinsics.py [--calibration <input path>] [--cam_width] [--cam_height] [--scale_focal] [--patternCentric ]
  6. default values:
  7. --calibration : left_intrinsics.yml
  8. --cam_width : 0.064/2
  9. --cam_height : 0.048/2
  10. --scale_focal : 40
  11. --patternCentric : True
  12. '''
  13. # Python 2/3 compatibility
  14. from __future__ import print_function
  15. import numpy as np
  16. import cv2 as cv
  17. from numpy import linspace
  18. def inverse_homogeneoux_matrix(M):
  19. R = M[0:3, 0:3]
  20. T = M[0:3, 3]
  21. M_inv = np.identity(4)
  22. M_inv[0:3, 0:3] = R.T
  23. M_inv[0:3, 3] = -(R.T).dot(T)
  24. return M_inv
  25. def transform_to_matplotlib_frame(cMo, X, inverse=False):
  26. M = np.identity(4)
  27. M[1,1] = 0
  28. M[1,2] = 1
  29. M[2,1] = -1
  30. M[2,2] = 0
  31. if inverse:
  32. return M.dot(inverse_homogeneoux_matrix(cMo).dot(X))
  33. else:
  34. return M.dot(cMo.dot(X))
  35. def create_camera_model(camera_matrix, width, height, scale_focal, draw_frame_axis=False):
  36. fx = camera_matrix[0,0]
  37. fy = camera_matrix[1,1]
  38. focal = 2 / (fx + fy)
  39. f_scale = scale_focal * focal
  40. # draw image plane
  41. X_img_plane = np.ones((4,5))
  42. X_img_plane[0:3,0] = [-width, height, f_scale]
  43. X_img_plane[0:3,1] = [width, height, f_scale]
  44. X_img_plane[0:3,2] = [width, -height, f_scale]
  45. X_img_plane[0:3,3] = [-width, -height, f_scale]
  46. X_img_plane[0:3,4] = [-width, height, f_scale]
  47. # draw triangle above the image plane
  48. X_triangle = np.ones((4,3))
  49. X_triangle[0:3,0] = [-width, -height, f_scale]
  50. X_triangle[0:3,1] = [0, -2*height, f_scale]
  51. X_triangle[0:3,2] = [width, -height, f_scale]
  52. # draw camera
  53. X_center1 = np.ones((4,2))
  54. X_center1[0:3,0] = [0, 0, 0]
  55. X_center1[0:3,1] = [-width, height, f_scale]
  56. X_center2 = np.ones((4,2))
  57. X_center2[0:3,0] = [0, 0, 0]
  58. X_center2[0:3,1] = [width, height, f_scale]
  59. X_center3 = np.ones((4,2))
  60. X_center3[0:3,0] = [0, 0, 0]
  61. X_center3[0:3,1] = [width, -height, f_scale]
  62. X_center4 = np.ones((4,2))
  63. X_center4[0:3,0] = [0, 0, 0]
  64. X_center4[0:3,1] = [-width, -height, f_scale]
  65. # draw camera frame axis
  66. X_frame1 = np.ones((4,2))
  67. X_frame1[0:3,0] = [0, 0, 0]
  68. X_frame1[0:3,1] = [f_scale/2, 0, 0]
  69. X_frame2 = np.ones((4,2))
  70. X_frame2[0:3,0] = [0, 0, 0]
  71. X_frame2[0:3,1] = [0, f_scale/2, 0]
  72. X_frame3 = np.ones((4,2))
  73. X_frame3[0:3,0] = [0, 0, 0]
  74. X_frame3[0:3,1] = [0, 0, f_scale/2]
  75. if draw_frame_axis:
  76. return [X_img_plane, X_triangle, X_center1, X_center2, X_center3, X_center4, X_frame1, X_frame2, X_frame3]
  77. else:
  78. return [X_img_plane, X_triangle, X_center1, X_center2, X_center3, X_center4]
  79. def create_board_model(extrinsics, board_width, board_height, square_size, draw_frame_axis=False):
  80. width = board_width*square_size
  81. height = board_height*square_size
  82. # draw calibration board
  83. X_board = np.ones((4,5))
  84. #X_board_cam = np.ones((extrinsics.shape[0],4,5))
  85. X_board[0:3,0] = [0,0,0]
  86. X_board[0:3,1] = [width,0,0]
  87. X_board[0:3,2] = [width,height,0]
  88. X_board[0:3,3] = [0,height,0]
  89. X_board[0:3,4] = [0,0,0]
  90. # draw board frame axis
  91. X_frame1 = np.ones((4,2))
  92. X_frame1[0:3,0] = [0, 0, 0]
  93. X_frame1[0:3,1] = [height/2, 0, 0]
  94. X_frame2 = np.ones((4,2))
  95. X_frame2[0:3,0] = [0, 0, 0]
  96. X_frame2[0:3,1] = [0, height/2, 0]
  97. X_frame3 = np.ones((4,2))
  98. X_frame3[0:3,0] = [0, 0, 0]
  99. X_frame3[0:3,1] = [0, 0, height/2]
  100. if draw_frame_axis:
  101. return [X_board, X_frame1, X_frame2, X_frame3]
  102. else:
  103. return [X_board]
  104. def draw_camera_boards(ax, camera_matrix, cam_width, cam_height, scale_focal,
  105. extrinsics, board_width, board_height, square_size,
  106. patternCentric):
  107. from matplotlib import cm
  108. min_values = np.zeros((3,1))
  109. min_values = np.inf
  110. max_values = np.zeros((3,1))
  111. max_values = -np.inf
  112. if patternCentric:
  113. X_moving = create_camera_model(camera_matrix, cam_width, cam_height, scale_focal)
  114. X_static = create_board_model(extrinsics, board_width, board_height, square_size)
  115. else:
  116. X_static = create_camera_model(camera_matrix, cam_width, cam_height, scale_focal, True)
  117. X_moving = create_board_model(extrinsics, board_width, board_height, square_size)
  118. cm_subsection = linspace(0.0, 1.0, extrinsics.shape[0])
  119. colors = [ cm.jet(x) for x in cm_subsection ]
  120. for i in range(len(X_static)):
  121. X = np.zeros(X_static[i].shape)
  122. for j in range(X_static[i].shape[1]):
  123. X[:,j] = transform_to_matplotlib_frame(np.eye(4), X_static[i][:,j])
  124. ax.plot3D(X[0,:], X[1,:], X[2,:], color='r')
  125. min_values = np.minimum(min_values, X[0:3,:].min(1))
  126. max_values = np.maximum(max_values, X[0:3,:].max(1))
  127. for idx in range(extrinsics.shape[0]):
  128. R, _ = cv.Rodrigues(extrinsics[idx,0:3])
  129. cMo = np.eye(4,4)
  130. cMo[0:3,0:3] = R
  131. cMo[0:3,3] = extrinsics[idx,3:6]
  132. for i in range(len(X_moving)):
  133. X = np.zeros(X_moving[i].shape)
  134. for j in range(X_moving[i].shape[1]):
  135. X[0:4,j] = transform_to_matplotlib_frame(cMo, X_moving[i][0:4,j], patternCentric)
  136. ax.plot3D(X[0,:], X[1,:], X[2,:], color=colors[idx])
  137. min_values = np.minimum(min_values, X[0:3,:].min(1))
  138. max_values = np.maximum(max_values, X[0:3,:].max(1))
  139. return min_values, max_values
  140. def main():
  141. import argparse
  142. parser = argparse.ArgumentParser(description='Plot camera calibration extrinsics.',
  143. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  144. parser.add_argument('--calibration', type=str, default='left_intrinsics.yml',
  145. help='YAML camera calibration file.')
  146. parser.add_argument('--cam_width', type=float, default=0.064/2,
  147. help='Width/2 of the displayed camera.')
  148. parser.add_argument('--cam_height', type=float, default=0.048/2,
  149. help='Height/2 of the displayed camera.')
  150. parser.add_argument('--scale_focal', type=float, default=40,
  151. help='Value to scale the focal length.')
  152. parser.add_argument('--patternCentric', action='store_true',
  153. help='The calibration board is static and the camera is moving.')
  154. args = parser.parse_args()
  155. fs = cv.FileStorage(cv.samples.findFile(args.calibration), cv.FILE_STORAGE_READ)
  156. board_width = int(fs.getNode('board_width').real())
  157. board_height = int(fs.getNode('board_height').real())
  158. square_size = fs.getNode('square_size').real()
  159. camera_matrix = fs.getNode('camera_matrix').mat()
  160. extrinsics = fs.getNode('extrinsic_parameters').mat()
  161. import matplotlib.pyplot as plt
  162. from mpl_toolkits.mplot3d import Axes3D # pylint: disable=unused-variable
  163. fig = plt.figure()
  164. ax = fig.gca(projection='3d')
  165. ax.set_aspect("auto")
  166. cam_width = args.cam_width
  167. cam_height = args.cam_height
  168. scale_focal = args.scale_focal
  169. min_values, max_values = draw_camera_boards(ax, camera_matrix, cam_width, cam_height,
  170. scale_focal, extrinsics, board_width,
  171. board_height, square_size, args.patternCentric)
  172. X_min = min_values[0]
  173. X_max = max_values[0]
  174. Y_min = min_values[1]
  175. Y_max = max_values[1]
  176. Z_min = min_values[2]
  177. Z_max = max_values[2]
  178. max_range = np.array([X_max-X_min, Y_max-Y_min, Z_max-Z_min]).max() / 2.0
  179. mid_x = (X_max+X_min) * 0.5
  180. mid_y = (Y_max+Y_min) * 0.5
  181. mid_z = (Z_max+Z_min) * 0.5
  182. ax.set_xlim(mid_x - max_range, mid_x + max_range)
  183. ax.set_ylim(mid_y - max_range, mid_y + max_range)
  184. ax.set_zlim(mid_z - max_range, mid_z + max_range)
  185. ax.set_xlabel('x')
  186. ax.set_ylabel('z')
  187. ax.set_zlabel('-y')
  188. ax.set_title('Extrinsic Parameters Visualization')
  189. plt.show()
  190. print('Done')
  191. if __name__ == '__main__':
  192. print(__doc__)
  193. main()
  194. cv.destroyAllWindows()