|
| 1 | +from typing import List, Tuple |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | + |
| 6 | +__epnp_alpha__ = np.array([[4, -1, -1, -1], |
| 7 | + [2, -1, -1, 1], |
| 8 | + [2, -1, 1, -1], |
| 9 | + [0, -1, 1, 1], |
| 10 | + [2, 1, -1, -1], |
| 11 | + [0, 1, -1, 1], |
| 12 | + [0, 1, 1, -1], |
| 13 | + [-2, 1, 1, 1]]) |
| 14 | + |
| 15 | + |
| 16 | +def get_default_camera_matrix(): |
| 17 | + return np.array([[1, 0, 0.5], |
| 18 | + [0, 1, 0.5], |
| 19 | + [0, 0, 1]]) |
| 20 | + |
| 21 | + |
| 22 | +def project_3d_points(points: np.array, camera_matrix: np.array): |
| 23 | + assert len(points.shape) == 2 |
| 24 | + projection = np.matmul(camera_matrix, points.T).T |
| 25 | + projection /= -projection[:,2].reshape(-1, 1) |
| 26 | + return projection[:, :-1] |
| 27 | + |
| 28 | + |
| 29 | +def convert_camera_matrix_2_ndc(matrix: np.array, img_shape: Tuple[int, int]=(1, 1)): |
| 30 | + ndc_mat = np.copy(matrix) |
| 31 | + ndc_mat[0, 0] *= 2.0 / img_shape[0] |
| 32 | + ndc_mat[1, 1] *= 2.0 / img_shape[1] |
| 33 | + |
| 34 | + ndc_mat[0, 2] = -ndc_mat[0, 2] * 2.0 / img_shape[0] + 1.0 |
| 35 | + ndc_mat[1, 2] = -ndc_mat[1, 2] * 2.0 / img_shape[1] + 1.0 |
| 36 | + |
| 37 | + return ndc_mat |
| 38 | + |
| 39 | + |
| 40 | +def convert_2d_to_ndc(points: np.array, portrait: bool=False): |
| 41 | + converted_points = np.zeros_like(points) |
| 42 | + if portrait: |
| 43 | + converted_points[:, 0] = points[:, 1] * 2 - 1 |
| 44 | + converted_points[:, 1] = points[:, 0] * 2 - 1 |
| 45 | + else: |
| 46 | + converted_points[:, 0] = points[:, 0] * 2 - 1 |
| 47 | + converted_points[:, 1] = 1 - points[:, 1] * 2 |
| 48 | + return converted_points |
| 49 | + |
| 50 | + |
| 51 | +def lift_2d(keypoint_sets: List[np.array], |
| 52 | + camera_matrix: np.array=get_default_camera_matrix(), |
| 53 | + portrait: bool=False) -> List[np.array]: |
| 54 | + """ |
| 55 | + Function takes normalized 2d coordinates of 2d keypoints on the image plane, |
| 56 | + camera matrix in normalized image space and outputs lifted 3d points in camera coordinates, |
| 57 | + which are defined up to an unknown scale factor |
| 58 | + """ |
| 59 | + ndc_cam_mat = convert_camera_matrix_2_ndc(camera_matrix) |
| 60 | + fx = ndc_cam_mat[0, 0] |
| 61 | + fy = ndc_cam_mat[1, 1] |
| 62 | + cx = ndc_cam_mat[0, 2] |
| 63 | + cy = ndc_cam_mat[1, 2] |
| 64 | + |
| 65 | + lifted_keypoint_sets = [] |
| 66 | + |
| 67 | + for kp_set in keypoint_sets: |
| 68 | + m = np.zeros((16, 12)) |
| 69 | + assert len(kp_set) == 9 |
| 70 | + |
| 71 | + for i in range(8): |
| 72 | + kp = kp_set[i + 1] |
| 73 | + # Convert 2d point from normalized screen coordinates [0, 1] to NDC coordinates([-1, 1]). |
| 74 | + if portrait: |
| 75 | + u = kp[1] * 2 - 1 |
| 76 | + v = kp[0] * 2 - 1 |
| 77 | + else: |
| 78 | + u = kp[0] * 2 - 1 |
| 79 | + v = 1 - kp[1] * 2 |
| 80 | + |
| 81 | + for j in range(4): |
| 82 | + # For each of the 4 control points, formulate two rows of the |
| 83 | + # m matrix (two equations). |
| 84 | + control_alpha = __epnp_alpha__[i, j] |
| 85 | + m[i * 2, j * 3] = fx * control_alpha |
| 86 | + m[i * 2, j * 3 + 2] = (cx + u) * control_alpha |
| 87 | + m[i * 2 + 1, j * 3 + 1] = fy * control_alpha |
| 88 | + m[i * 2 + 1, j * 3 + 2] = (cy + v) * control_alpha |
| 89 | + |
| 90 | + mt_m = np.matmul(m.T, m) |
| 91 | + w, v = np.linalg.eigh(mt_m) |
| 92 | + assert w.shape[0] == 12 |
| 93 | + control_matrix = v[:, 0].reshape(4, 3) |
| 94 | + # All 3d points should be in front of camera (z < 0). |
| 95 | + if control_matrix[0, 2] > 0: |
| 96 | + control_matrix = -control_matrix |
| 97 | + |
| 98 | + lifted_kp_set = [] |
| 99 | + lifted_kp_set.append(control_matrix[0, :]) |
| 100 | + vertices = np.matmul(__epnp_alpha__, control_matrix) |
| 101 | + |
| 102 | + for i in range(8): |
| 103 | + lifted_kp_set.append(vertices[i, :]) |
| 104 | + |
| 105 | + lifted_kp_set = np.array(lifted_kp_set) |
| 106 | + lifted_keypoint_sets.append(lifted_kp_set) |
| 107 | + |
| 108 | + return lifted_keypoint_sets |
| 109 | + |
| 110 | + |
| 111 | +def draw_boxes(boxes=[], clips=[], colors=['r', 'b', 'g', 'k']): |
| 112 | + """Draw a list of boxes. |
| 113 | + The boxes are defined as a list of vertices |
| 114 | + """ |
| 115 | + import matplotlib.pyplot as plt |
| 116 | + from objectron.dataset import box |
| 117 | + |
| 118 | + fig = plt.figure(figsize=(10, 10)) |
| 119 | + ax = fig.add_subplot(111, projection='3d') |
| 120 | + for i, b in enumerate(boxes): |
| 121 | + x, y, z = b[:, 0], b[:, 1], b[:, 2] |
| 122 | + ax.scatter(x, y, z, c='r') |
| 123 | + for e in box.EDGES: |
| 124 | + ax.plot(x[e], y[e], z[e], linewidth=2, c=colors[i % len(colors)]) |
| 125 | + |
| 126 | + if clips: |
| 127 | + points = np.array(clips) |
| 128 | + ax.scatter(points[:, 0], points[:, 1], points[:, 2], s=100, c='k') |
| 129 | + |
| 130 | + plt.gca().patch.set_facecolor('white') |
| 131 | + ax.w_xaxis.set_pane_color((0.8, 0.8, 0.8, 1.0)) |
| 132 | + ax.w_yaxis.set_pane_color((0.8, 0.8, 0.8, 1.0)) |
| 133 | + ax.w_zaxis.set_pane_color((0.8, 0.8, 0.8, 1.0)) |
| 134 | + |
| 135 | + # rotate the axes and update |
| 136 | + ax.view_init(30, 12) |
| 137 | + plt.draw() |
| 138 | + plt.savefig('3d_boxes.png') |
0 commit comments