| 1 |
import pyopengv |
| 2 |
import numpy as np |
| 3 |
|
| 4 |
|
| 5 |
def normalized(x): |
| 6 |
return x / np.linalg.norm(x) |
| 7 |
|
| 8 |
|
| 9 |
def generateRandomPoint(maximumDepth, minimumDepth): |
| 10 |
cleanPoint = np.random.uniform(-1.0, 1.0, 3) |
| 11 |
direction = normalized(cleanPoint) |
| 12 |
return (maximumDepth - minimumDepth) * cleanPoint + minimumDepth * direction |
| 13 |
|
| 14 |
|
| 15 |
def generateRandomTranslation(maximumParallax): |
| 16 |
return np.random.uniform(-maximumParallax, maximumParallax, 3) |
| 17 |
|
| 18 |
|
| 19 |
def generateRandomRotation(maxAngle): |
| 20 |
rpy = np.random.uniform(-maxAngle, maxAngle, 3) |
| 21 |
|
| 22 |
R1 = np.array([[1.0, 0.0, 0.0], |
| 23 |
[0.0, np.cos(rpy[0]), -np.sin(rpy[0])], |
| 24 |
[0.0, np.sin(rpy[0]), np.cos(rpy[0])]]) |
| 25 |
|
| 26 |
R2 = np.array([[np.cos(rpy[1]), 0.0, np.sin(rpy[1])], |
| 27 |
[0.0, 1.0, 0.0], |
| 28 |
[-np.sin(rpy[1]), 0.0, np.cos(rpy[1])]]) |
| 29 |
|
| 30 |
R3 = np.array([[np.cos(rpy[2]), -np.sin(rpy[2]), 0.0], |
| 31 |
[np.sin(rpy[2]), np.cos(rpy[2]), 0.0], |
| 32 |
[0.0, 0.0, 1.0]]) |
| 33 |
|
| 34 |
return R3.dot(R2.dot(R1)) |
| 35 |
|
| 36 |
|
| 37 |
def addNoise(noiseLevel, cleanPoint): |
| 38 |
noisyPoint = cleanPoint + np.random.uniform(-noiseLevel, noiseLevel, 3) |
| 39 |
return normalized(noisyPoint) |
| 40 |
|
| 41 |
|
| 42 |
def extractRelativePose(position1, position2, rotation1, rotation2): |
| 43 |
relativeRotation = rotation1.T.dot(rotation2) |
| 44 |
relativePosition = rotation1.T.dot(position2 - position1) |
| 45 |
return relativePosition, relativeRotation |
| 46 |
|
| 47 |
|
| 48 |
def essentialMatrix(position, rotation): |
| 49 |
# E transforms vectors from vp 2 to 1: x_1^T * E * x_2 = 0 |
| 50 |
# and E = (t)_skew*R |
| 51 |
t_skew = np.zeros((3, 3)) |
| 52 |
t_skew[0, 1] = -position[2] |
| 53 |
t_skew[0, 2] = position[1] |
| 54 |
t_skew[1, 0] = position[2] |
| 55 |
t_skew[1, 2] = -position[0] |
| 56 |
t_skew[2, 0] = -position[1] |
| 57 |
t_skew[2, 1] = position[0] |
| 58 |
|
| 59 |
E = t_skew.dot(rotation) |
| 60 |
return normalized(E) |
| 61 |
|
| 62 |
|
| 63 |
def getPerturbedPose(position, rotation, amplitude): |
| 64 |
dp = generateRandomTranslation(amplitude) |
| 65 |
dR = generateRandomRotation(amplitude) |
| 66 |
return position + dp, rotation.dot(dR) |
| 67 |
|
| 68 |
|
| 69 |
def proportional(x, y, tol=1e-2): |
| 70 |
xn = normalized(x) |
| 71 |
yn = normalized(y) |
| 72 |
return (np.allclose(xn, yn, rtol=1e20, atol=tol) or |
| 73 |
np.allclose(xn, -yn, rtol=1e20, atol=tol)) |
| 74 |
|
| 75 |
|
| 76 |
def matrix_in_list(a, l): |
| 77 |
for b in l: |
| 78 |
if proportional(a, b): |
| 79 |
return True |
| 80 |
return False |
| 81 |
|
| 82 |
|
| 83 |
def same_transformation(position, rotation, transformation): |
| 84 |
R = transformation[:, :3] |
| 85 |
t = transformation[:, 3] |
| 86 |
return proportional(position, t) and proportional(rotation, R) |
| 87 |
|
| 88 |
|
| 89 |
class RelativePoseDataset: |
| 90 |
|
| 91 |
def __init__(self, num_points, noise, outlier_fraction, rotation_only=False): |
| 92 |
# generate a random pose for viewpoint 1 |
| 93 |
position1 = np.zeros(3) |
| 94 |
rotation1 = np.eye(3) |
| 95 |
|
| 96 |
# generate a random pose for viewpoint 2 |
| 97 |
if rotation_only: |
| 98 |
position2 = np.zeros(3) |
| 99 |
else: |
| 100 |
position2 = generateRandomTranslation(2.0) |
| 101 |
rotation2 = generateRandomRotation(0.5) |
| 102 |
|
| 103 |
# derive correspondences based on random point-cloud |
| 104 |
self.generateCorrespondences( |
| 105 |
position1, rotation1, position2, rotation2, |
| 106 |
num_points, noise, outlier_fraction) |
| 107 |
|
| 108 |
# Extract the relative pose |
| 109 |
self.position, self.rotation = extractRelativePose( |
| 110 |
position1, position2, rotation1, rotation2) |
| 111 |
if not rotation_only: |
| 112 |
self.essential = essentialMatrix(self.position, self.rotation) |
| 113 |
|
| 114 |
def generateCorrespondences(self, |
| 115 |
position1, rotation1, |
| 116 |
position2, rotation2, |
| 117 |
num_points, |
| 118 |
noise, outlier_fraction): |
| 119 |
min_depth = 4 |
| 120 |
max_depth = 8 |
| 121 |
|
| 122 |
# initialize point-cloud |
| 123 |
self.points = np.empty((num_points, 3)) |
| 124 |
for i in range(num_points): |
| 125 |
self.points[i] = generateRandomPoint(max_depth, min_depth) |
| 126 |
|
| 127 |
self.bearing_vectors1 = np.empty((num_points, 3)) |
| 128 |
self.bearing_vectors2 = np.empty((num_points, 3)) |
| 129 |
for i in range(num_points): |
| 130 |
# get the point in viewpoint 1 |
| 131 |
body_point1 = rotation1.T.dot(self.points[i] - position1) |
| 132 |
|
| 133 |
# get the point in viewpoint 2 |
| 134 |
body_point2 = rotation2.T.dot(self.points[i] - position2) |
| 135 |
|
| 136 |
self.bearing_vectors1[i] = normalized(body_point1) |
| 137 |
self.bearing_vectors2[i] = normalized(body_point2) |
| 138 |
|
| 139 |
# add noise |
| 140 |
if noise > 0.0: |
| 141 |
self.bearing_vectors1[i] = addNoise(noise, self.bearing_vectors1[i]) |
| 142 |
self.bearing_vectors2[i] = addNoise(noise, self.bearing_vectors2[i]) |
| 143 |
|
| 144 |
# add outliers |
| 145 |
num_outliers = int(outlier_fraction * num_points) |
| 146 |
for i in range(num_outliers): |
| 147 |
# create random point |
| 148 |
p = generateRandomPoint(max_depth, min_depth) |
| 149 |
|
| 150 |
# project this point into viewpoint 2 |
| 151 |
body_point = rotation2.T.dot(p - position2) |
| 152 |
|
| 153 |
# normalize the bearing vector |
| 154 |
self.bearing_vectors2[i] = normalized(body_point) |
| 155 |
|
| 156 |
|
| 157 |
def test_relative_pose(): |
| 158 |
print("Testing relative pose") |
| 159 |
|
| 160 |
d = RelativePoseDataset(10, 0.0, 0.0) |
| 161 |
|
| 162 |
# running experiments |
| 163 |
twopt_translation = pyopengv.relative_pose_twopt( |
| 164 |
d.bearing_vectors1, d.bearing_vectors2, d.rotation) |
| 165 |
fivept_nister_essentials = pyopengv.relative_pose_fivept_nister( |
| 166 |
d.bearing_vectors1, d.bearing_vectors2) |
| 167 |
fivept_kneip_rotations = pyopengv.relative_pose_fivept_kneip( |
| 168 |
d.bearing_vectors1, d.bearing_vectors2) |
| 169 |
sevenpt_essentials = pyopengv.relative_pose_sevenpt(d.bearing_vectors1, d.bearing_vectors2) |
| 170 |
eightpt_essential = pyopengv.relative_pose_eightpt(d.bearing_vectors1, d.bearing_vectors2) |
| 171 |
t_perturbed, R_perturbed = getPerturbedPose(d.position, d.rotation, 0.01) |
| 172 |
eigensolver_rotation = pyopengv.relative_pose_eigensolver( |
| 173 |
d.bearing_vectors1, d.bearing_vectors2, R_perturbed) |
| 174 |
t_perturbed, R_perturbed = getPerturbedPose(d.position, d.rotation, 0.1) |
| 175 |
nonlinear_transformation = pyopengv.relative_pose_optimize_nonlinear( |
| 176 |
d.bearing_vectors1, d.bearing_vectors2, t_perturbed, R_perturbed) |
| 177 |
|
| 178 |
assert proportional(d.position, twopt_translation) |
| 179 |
assert matrix_in_list(d.essential, fivept_nister_essentials) |
| 180 |
assert matrix_in_list(d.rotation, fivept_kneip_rotations) |
| 181 |
assert matrix_in_list(d.essential, sevenpt_essentials) |
| 182 |
assert proportional(d.essential, eightpt_essential) |
| 183 |
assert proportional(d.rotation, eigensolver_rotation) |
| 184 |
assert same_transformation(d.position, d.rotation, nonlinear_transformation) |
| 185 |
|
| 186 |
print("Done testing relative pose") |
| 187 |
|
| 188 |
|
| 189 |
def test_relative_pose_ransac(): |
| 190 |
print("Testing relative pose ransac") |
| 191 |
|
| 192 |
d = RelativePoseDataset(100, 0.0, 0.3) |
| 193 |
|
| 194 |
ransac_transformation = pyopengv.relative_pose_ransac( |
| 195 |
d.bearing_vectors1, d.bearing_vectors2, "NISTER", 0.01, 1000) |
| 196 |
|
| 197 |
assert same_transformation(d.position, d.rotation, ransac_transformation) |
| 198 |
|
| 199 |
print("Done testing relative pose ransac") |
| 200 |
|
| 201 |
|
| 202 |
def test_relative_pose_ransac_rotation_only(): |
| 203 |
print("Testing relative pose ransac rotation only") |
| 204 |
|
| 205 |
d = RelativePoseDataset(100, 0.0, 0.3, rotation_only=True) |
| 206 |
|
| 207 |
ransac_rotation = pyopengv.relative_pose_ransac_rotation_only( |
| 208 |
d.bearing_vectors1, d.bearing_vectors2, 0.01, 1000) |
| 209 |
|
| 210 |
assert proportional(d.rotation, ransac_rotation) |
| 211 |
|
| 212 |
print("Done testing relative pose ransac rotation only") |
| 213 |
|
| 214 |
|
| 215 |
def test_triangulation(): |
| 216 |
print("Testing triangulation") |
| 217 |
|
| 218 |
d = RelativePoseDataset(10, 0.0, 0.0) |
| 219 |
|
| 220 |
points1 = pyopengv.triangulation_triangulate( |
| 221 |
d.bearing_vectors1, d.bearing_vectors2, d.position, d.rotation) |
| 222 |
|
| 223 |
assert np.allclose(d.points, points1) |
| 224 |
|
| 225 |
points2 = pyopengv.triangulation_triangulate2( |
| 226 |
d.bearing_vectors1, d.bearing_vectors2, d.position, d.rotation) |
| 227 |
|
| 228 |
assert np.allclose(d.points, points2) |
| 229 |
|
| 230 |
print("Done testing triangulation") |
| 231 |
|
| 232 |
|
| 233 |
if __name__ == "__main__": |
| 234 |
test_relative_pose() |
| 235 |
test_relative_pose_ransac() |
| 236 |
test_relative_pose_ransac_rotation_only() |
| 237 |
test_triangulation() |