forked from bailus/tsai-calibration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
155 lines (118 loc) · 4.77 KB
/
plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python
###
#
# Functions to plot the results of calibration.
# Author: Samuel Bailey <[email protected]>
#
###
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib.pyplot import cm
from mpl_toolkits.mplot3d import Axes3D
def plotPoints(points, image, title=''):
fig, (ax) = plt.subplots(1)
fig.set_size_inches(6, 6)
markersize = 6
ax.imshow(image, alpha=0.4)
x, y = zip(*map(lambda point: point.pixel, points))
ax.plot(x, y, 'rx', markersize=markersize / math.sqrt(2), label='actual')
px, py = zip(*map(lambda point: point.projectedPixel[:2], points))
ax.plot(px, py, 'bx', markersize=markersize, label='projected')
ux, uy = zip(*map(lambda point: point.distortedPixel[:2], points))
ax.plot(ux, uy, 'k+', markersize=markersize * 2, label='distorted')
ax.set_title('%s' % title)
ax.set_xlabel('x (px)')
ax.set_ylabel('y (px)')
ax.legend()
return fig
def plotStats(errorss, kappass, resolutions, labels, title=None):
n = len(errorss)
fig, (ax1, ax2) = plt.subplots(2, sharex=True)
fig.set_size_inches(6, 6)
if (title):
ax1.set_title(title)
ax1.set_ylabel('K1')
ax2.set_ylabel('Mean error (pixels)')
ax2.set_xlabel('Iteration (t)')
xaxis = range(-1, len(errorss[0]) - 1)
colors = cm.Dark2(np.linspace(0, 1, n))
for i in range(n):
kappas = kappass[i]
errors = errorss[i]
color = colors[i]
resolution = resolutions[i]
label = labels[i]
ax1.plot(xaxis, kappas, marker='.', c=color, label=label)
# ax2.plot(map(lambda e: e.minmax[1], errors), ':', c=color)
ax2.plot(xaxis, map(lambda e: e.mean, errors), marker='.', c=color, label=label)
# ax2.plot(map(lambda e: e.minmax[0], errors), ':', c=color)
ax2.legend()
return fig
def displayStereo(model):
world = model['world']
points = world['points']
fig = plt.figure()
ax = fig.add_subplot(111)
fig.set_size_inches(6, 6)
markersize = 6
def displayCamera(c, label, color):
params = c['params']
invRT = np.linalg.inv(params['RT'])
p = np.dot(invRT, [0.0, 0.0, 0.0, 1.0])
px = np.dot(invRT, [-10.0, 0.0, 0.0, 1.0])
pz = np.dot(invRT, [0.0, 0.0, -10.0, 1.0])
ax.plot([p[0]], [p[2]], '.', markersize=markersize, color=color, label=label)
dx = px - p
dz = pz - p
ax.arrow(p[0] - dx[0], p[2] - dx[2], dx[0] * 2.0, dx[2] * 2.0, head_width=markersize * 0.3,
head_length=markersize * 0.4, fc=color, ec=color, label=label, linestyle=':')
ax.arrow(p[0] - dz[0], p[2] - dz[2], dz[0] * 2.0, dz[2] * 2.0, head_width=markersize * 0.3,
head_length=markersize * 0.4, fc=color, ec=color, label=label, linestyle='-')
displayCamera(model['left'], 'Left Camera', 'g')
displayCamera(model['right'], 'Right Camera', 'b')
x = [p[0] for p in points]
z = [p[2] for p in points]
ax.plot(x, z, 'r+', markersize=markersize, label='Calibration point')
ax.set_xlabel('x (mm)')
ax.set_ylabel('z (mm)')
ax.set_title('Cameras and Calibration Points\nTop-Down View (In world coordinates)')
ax.legend()
ax.grid()
return fig
def displayStereoSide(model):
world = model['world']
points = world['points']
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
fig.set_size_inches(6, 6)
ax.view_init(30, 45)
markersize = 6
def displayCamera(c, label, color):
params = c['params']
invRT = np.linalg.inv(params['RT'])
p = np.dot(invRT, [0.0, 0.0, 0.0, 1.0])
px = np.dot(invRT, [10.0, 0.0, 0.0, 1.0])
py = np.dot(invRT, [0.0, 10.0, 0.0, 1.0])
pz = np.dot(invRT, [0.0, 0.0, 10.0, 1.0])
ax.plot([p[0]], [p[1]], '.', zs=[p[2]], markersize=markersize, color=color, label=label)
dx = px - p
dy = py - p
dz = pz - p
ax.quiver(p[0], p[1], p[2], dx[0], dx[1], dx[2], color=color)
ax.quiver(p[0], p[1], p[2], dy[0], dy[1], dy[2], color=color)
ax.quiver(p[0], p[1], p[2], dz[0], dz[1], dz[2], color=color)
displayCamera(model['left'], 'Left Camera', 'g')
displayCamera(model['right'], 'Right Camera', 'b')
x = [p[0] for p in points]
y = [p[1] for p in points]
z = [p[2] for p in points]
ax.plot(x, y, 'r+', zs=z, markersize=markersize, label='Calibration point')
ax.set_xlabel('x (mm)')
ax.set_ylabel('y (mm)')
ax.set_zlabel('z (mm)')
ax.grid()
ax.set_title('Cameras and Calibration Points\nOrthographic Projection (In world coordinates)')
ax.legend()
return fig