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