|
| 1 | +import numpy as np |
| 2 | +from matplotlib import pyplot as plt |
| 3 | + |
| 4 | +from utils.plot import plot_3d_vector_arrow, plot_triangle, set_equal_3d_axis |
| 5 | + |
| 6 | +show_animation = True |
| 7 | + |
| 8 | + |
| 9 | +def calc_normal_vector(p1, p2, p3): |
| 10 | + """Calculate normal vector of triangle |
| 11 | +
|
| 12 | + Parameters |
| 13 | + ---------- |
| 14 | + p1 : np.array |
| 15 | + 3D point |
| 16 | + p2 : np.array |
| 17 | + 3D point |
| 18 | + p3 : np.array |
| 19 | + 3D point |
| 20 | +
|
| 21 | + Returns |
| 22 | + ------- |
| 23 | + normal_vector : np.array |
| 24 | + normal vector (3,) |
| 25 | +
|
| 26 | + """ |
| 27 | + # calculate two vectors of triangle |
| 28 | + v1 = p2 - p1 |
| 29 | + v2 = p3 - p1 |
| 30 | + |
| 31 | + # calculate normal vector |
| 32 | + normal_vector = np.cross(v1, v2) |
| 33 | + |
| 34 | + # normalize vector |
| 35 | + normal_vector = normal_vector / np.linalg.norm(normal_vector) |
| 36 | + |
| 37 | + return normal_vector |
| 38 | + |
| 39 | + |
| 40 | +def sample_3d_points_from_a_plane(num_samples, normal): |
| 41 | + points_2d = np.random.normal(size=(num_samples, 2)) # 2D points on a plane |
| 42 | + d = 0 |
| 43 | + for i in range(len(points_2d)): |
| 44 | + point_3d = np.append(points_2d[i], 0) |
| 45 | + d += normal @ point_3d |
| 46 | + d /= len(points_2d) |
| 47 | + |
| 48 | + points_3d = np.zeros((len(points_2d), 3)) |
| 49 | + for i in range(len(points_2d)): |
| 50 | + point_2d = np.append(points_2d[i], 0) |
| 51 | + projection_length = (d - normal @ point_2d) / np.linalg.norm(normal) |
| 52 | + points_3d[i] = point_2d + projection_length * normal |
| 53 | + |
| 54 | + return points_3d |
| 55 | + |
| 56 | + |
| 57 | +def distance_to_plane(point, normal, origin): |
| 58 | + dot_product = np.dot(normal, point) - np.dot(normal, origin) |
| 59 | + if np.isclose(dot_product, 0): |
| 60 | + return 0.0 |
| 61 | + else: |
| 62 | + distance = abs(dot_product) / np.linalg.norm(normal) |
| 63 | + return distance |
| 64 | + |
| 65 | + |
| 66 | +def ransac_normal_vector_estimation(points_3d, inlier_radio_th=0.7, |
| 67 | + inlier_dist=0.1, p=0.99): |
| 68 | + """ |
| 69 | + RANSAC based normal vector estimation |
| 70 | +
|
| 71 | + Parameters |
| 72 | + ---------- |
| 73 | + points_3d : np.array |
| 74 | + 3D points (N, 3) |
| 75 | + inlier_radio_th : float |
| 76 | + Inlier ratio threshold. If inlier ratio is larger than this value, |
| 77 | + the iteration is stopped. Default is 0.7. |
| 78 | + inlier_dist : float |
| 79 | + Inlier distance threshold. If distance between points and estimated |
| 80 | + plane is smaller than this value, the point is inlier. Default is 0.1. |
| 81 | + p : float |
| 82 | + Probability that at least one of the sets of random samples does not |
| 83 | + include an outlier. If this probability is near 1, the iteration |
| 84 | + number is large. Default is 0.99. |
| 85 | +
|
| 86 | + Returns |
| 87 | + ------- |
| 88 | + center_vector : np.array |
| 89 | + Center of estimated plane. (3,) |
| 90 | + normal_vector : np.array |
| 91 | + Normal vector of estimated plane. (3,) |
| 92 | +
|
| 93 | + """ |
| 94 | + center = np.mean(points_3d, axis=0) |
| 95 | + |
| 96 | + max_iter = int(np.floor(np.log(1.0-p)/np.log(1.0-(1.0-inlier_radio_th)**3))) |
| 97 | + |
| 98 | + for ite in range(max_iter): |
| 99 | + # Random sampling |
| 100 | + sampled_ids = np.random.choice(points_3d.shape[0], size=3, |
| 101 | + replace=False) |
| 102 | + sampled_points = points_3d[sampled_ids, :] |
| 103 | + p1 = sampled_points[0, :] |
| 104 | + p2 = sampled_points[1, :] |
| 105 | + p3 = sampled_points[2, :] |
| 106 | + normal_vector = calc_normal_vector(p1, p2, p3) |
| 107 | + |
| 108 | + # calc inlier ratio |
| 109 | + n_inliner = 0 |
| 110 | + for i in range(points_3d.shape[0]): |
| 111 | + p = points_3d[i, :] |
| 112 | + if distance_to_plane(p, normal_vector, center) <= inlier_dist: |
| 113 | + n_inliner += 1 |
| 114 | + inlier_ratio = n_inliner / points_3d.shape[0] |
| 115 | + print(f"Iter:{ite}, {inlier_ratio=}") |
| 116 | + if inlier_ratio > inlier_radio_th: |
| 117 | + return center, normal_vector |
| 118 | + |
| 119 | + return center, None |
| 120 | + |
| 121 | + |
| 122 | +def main1(): |
| 123 | + p1 = np.array([0.0, 0.0, 1.0]) |
| 124 | + p2 = np.array([1.0, 1.0, 0.0]) |
| 125 | + p3 = np.array([0.0, 1.0, 0.0]) |
| 126 | + |
| 127 | + center = np.mean([p1, p2, p3], axis=0) |
| 128 | + normal_vector = calc_normal_vector(p1, p2, p3) |
| 129 | + print(f"{center=}") |
| 130 | + print(f"{normal_vector=}") |
| 131 | + |
| 132 | + if show_animation: |
| 133 | + fig = plt.figure() |
| 134 | + ax = fig.add_subplot(projection='3d') |
| 135 | + set_equal_3d_axis(ax, [0.0, 2.5], [0.0, 2.5], [0.0, 3.0]) |
| 136 | + plot_triangle(p1, p2, p3, ax) |
| 137 | + ax.plot(center[0], center[1], center[2], "ro") |
| 138 | + plot_3d_vector_arrow(ax, center, center + normal_vector) |
| 139 | + plt.show() |
| 140 | + |
| 141 | + |
| 142 | +def main2(rng=None): |
| 143 | + true_normal = np.array([0, 1, 1]) |
| 144 | + true_normal = true_normal / np.linalg.norm(true_normal) |
| 145 | + num_samples = 100 |
| 146 | + noise_scale = 0.1 |
| 147 | + |
| 148 | + points_3d = sample_3d_points_from_a_plane(num_samples, true_normal) |
| 149 | + # add random noise |
| 150 | + points_3d += np.random.normal(size=points_3d.shape, scale=noise_scale) |
| 151 | + |
| 152 | + print(f"{points_3d.shape=}") |
| 153 | + |
| 154 | + center, estimated_normal = ransac_normal_vector_estimation( |
| 155 | + points_3d, inlier_dist=noise_scale) |
| 156 | + |
| 157 | + if estimated_normal is None: |
| 158 | + print("Failed to estimate normal vector") |
| 159 | + return |
| 160 | + |
| 161 | + print(f"{true_normal=}") |
| 162 | + print(f"{estimated_normal=}") |
| 163 | + |
| 164 | + if show_animation: |
| 165 | + fig = plt.figure() |
| 166 | + ax = fig.add_subplot(projection='3d') |
| 167 | + ax.plot(points_3d[:, 0], points_3d[:, 1], points_3d[:, 2], ".r") |
| 168 | + plot_3d_vector_arrow(ax, center, center + true_normal) |
| 169 | + plot_3d_vector_arrow(ax, center, center + estimated_normal) |
| 170 | + set_equal_3d_axis(ax, [-3.0, 3.0], [-3.0, 3.0], [-3.0, 3.0]) |
| 171 | + plt.title("RANSAC based Normal vector estimation") |
| 172 | + plt.show() |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == '__main__': |
| 176 | + # main1() |
| 177 | + main2() |
0 commit comments