-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcalibration.cpp
135 lines (117 loc) · 3.79 KB
/
calibration.cpp
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
#include "calibration.h"
py::list Calibration::get_size() {
py::list res;
res.append(width);
res.append(height);
return res;
}
py::array_t<double> Calibration::get_intrinsics_matrix(bool extended=false) {
int msize = 3;
if(extended)
msize = 4;
size_t size = msize*msize;
double *intrinsics = new double[size];
for(unsigned int i=0; i<size; i++)
intrinsics[i] = 0;
if(extended) {
intrinsics[0] = fx;
intrinsics[2] = cx;
intrinsics[5] = fy;
intrinsics[6] = cy;
intrinsics[10] = 1;
intrinsics[15] = 1;
}
else {
intrinsics[0] = fx;
intrinsics[2] = cx;
intrinsics[4] = fy;
intrinsics[5] = cy;
intrinsics[8] = 1;
}
// Python object that frees the allocated memory when destroyed
py::capsule free_when_done(intrinsics, [](void *f) {
double *intrinsics = reinterpret_cast<double *>(f);
delete[] intrinsics;
});
return py::array_t<double>(
{msize, msize}, // shape
{msize*8, 8}, // C-style contiguous strides for double
intrinsics, // the data pointer
free_when_done); // numpy array references this parent
}
py::array_t<double> Calibration::get_distortion_params() {
constexpr size_t size = 1*8;
double *dist_params = new double[size];
dist_params[0] = k1;
dist_params[1] = k2;
dist_params[2] = p1;
dist_params[3] = p2;
dist_params[4] = k3;
dist_params[5] = k4;
dist_params[6] = k5;
dist_params[7] = k6;
// Python object that frees the allocated memory when destroyed
py::capsule free_when_done(dist_params, [](void *f) {
double *dist_params = reinterpret_cast<double *>(f);
delete[] dist_params;
});
return py::array_t<double>(
{1, 8}, // shape
{1*8, 8}, // strides
dist_params, // data pointer
free_when_done); // numpy array references this parent
}
py::array_t<double> Calibration::get_rotation_matrix() {
constexpr size_t size = 3*3;
double *rot = new double[size];
for(unsigned int i=0; i<size; i++)
rot[i] = rotation[i];
// Python object that frees the allocated memory when destroyed
py::capsule free_when_done(rot, [](void *f) {
double *rot = reinterpret_cast<double *>(f);
delete[] rot;
});
return py::array_t<double>(
{3, 3}, // shape
{3*8, 8}, // strides
rot, // data pointer
free_when_done); // numpy array references this parent
}
py::array_t<double> Calibration::get_translation_vector() {
constexpr size_t size = 3*1;
double *t = new double[size];
for(unsigned int i=0; i<size; i++)
t[i] = translation[i];
// Python object that frees the allocated memory when destroyed
py::capsule free_when_done(t, [](void *f) {
double *t = reinterpret_cast<double *>(f);
delete[] t;
});
return py::array_t<double>(
{3, 1}, // shape
{1*8, 8}, // strides
t, // data pointer
free_when_done); // numpy array references this parent
}
py::array_t<double> Calibration::get_camera_pose() {
constexpr size_t size = 4*4;
double *pose = new double[size];
for(unsigned int i=0; i<size; i++)
pose[i] = 0;
for(unsigned int l=0; l<3; l++) {
for(int c=0; c<3; c++)
pose[l*4 + c] = rotation[l*3 + c];
pose[l*4 + 3] = translation[l];
}
pose[15] = 1;
// Python object that frees the allocated memory when destroyed
py::capsule free_when_done(pose, [](void *f) {
double *pose = reinterpret_cast<double *>(f);
delete[] pose;
});
return py::array_t<double>(
{4, 4}, // shape
{4*8, 8}, // strides
pose, // data pointer
free_when_done); // numpy array references this parent
}