forked from hku-mars/livox_camera_calib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
244 lines (218 loc) · 6.6 KB
/
common.h
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#ifndef LIDAR_CAMERA_COMMON_H
#define LIDAR_CAMERA_COMMON_H
#include <Eigen/Core>
#include <cv_bridge/cv_bridge.h>
#include <pcl/common/io.h>
#include <stdio.h>
#include <string>
#include <unordered_map>
struct PnPData {
double x, y, z, u, v;
};
struct VPnPData {
double x, y, z, u, v;
Eigen::Vector2d direction;
Eigen::Vector2d direction_lidar;
int number;
};
typedef Eigen::Matrix<double, 6, 1> Vector6d;
//存放每一个点的索引值和其对应的曲率
typedef struct PCURVATURE {
// POINT3F cPoint;
int index;
float curvature;
} PCURVATURE;
typedef struct Plane {
pcl::PointCloud<pcl::PointXYZI> cloud;
pcl::PointXYZ p_center;
Eigen::Vector3d normal;
int index;
} Plane;
class VOXEL_LOC {
public:
int64_t x, y, z;
VOXEL_LOC(int64_t vx = 0, int64_t vy = 0, int64_t vz = 0)
: x(vx), y(vy), z(vz) {}
bool operator==(const VOXEL_LOC &other) const {
return (x == other.x && y == other.y && z == other.z);
}
};
// Hash value
namespace std {
template <> struct hash<VOXEL_LOC> {
size_t operator()(const VOXEL_LOC &s) const {
using std::size_t;
using std::hash;
return ((hash<int64_t>()(s.x) ^ (hash<int64_t>()(s.y) << 1)) >> 1) ^
(hash<int64_t>()(s.z) << 1);
}
};
}
struct M_POINT {
float xyz[3];
float intensity;
int count = 0;
};
using namespace std;
template <class T> void input(T matrix[4][5]) {
cout << "please input matrix element's data" << endl;
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 5; j++) {
cin >> matrix[i][j];
}
}
cout << "input ok";
}
template <class T> void calc(T matrix[4][5], Eigen::Vector3d &solution) {
T base_D = matrix[1][1] * matrix[2][2] * matrix[3][3] +
matrix[2][1] * matrix[3][2] * matrix[1][3] +
matrix[3][1] * matrix[1][2] * matrix[2][3]; //计算行列式
base_D = base_D - (matrix[1][3] * matrix[2][2] * matrix[3][1] +
matrix[1][1] * matrix[2][3] * matrix[3][2] +
matrix[1][2] * matrix[2][1] * matrix[3][3]);
if (base_D != 0) {
T x_D = matrix[1][4] * matrix[2][2] * matrix[3][3] +
matrix[2][4] * matrix[3][2] * matrix[1][3] +
matrix[3][4] * matrix[1][2] * matrix[2][3];
x_D = x_D - (matrix[1][3] * matrix[2][2] * matrix[3][4] +
matrix[1][4] * matrix[2][3] * matrix[3][2] +
matrix[1][2] * matrix[2][4] * matrix[3][3]);
T y_D = matrix[1][1] * matrix[2][4] * matrix[3][3] +
matrix[2][1] * matrix[3][4] * matrix[1][3] +
matrix[3][1] * matrix[1][4] * matrix[2][3];
y_D = y_D - (matrix[1][3] * matrix[2][4] * matrix[3][1] +
matrix[1][1] * matrix[2][3] * matrix[3][4] +
matrix[1][4] * matrix[2][1] * matrix[3][3]);
T z_D = matrix[1][1] * matrix[2][2] * matrix[3][4] +
matrix[2][1] * matrix[3][2] * matrix[1][4] +
matrix[3][1] * matrix[1][2] * matrix[2][4];
z_D = z_D - (matrix[1][4] * matrix[2][2] * matrix[3][1] +
matrix[1][1] * matrix[2][4] * matrix[3][2] +
matrix[1][2] * matrix[2][1] * matrix[3][4]);
T x = x_D / base_D;
T y = y_D / base_D;
T z = z_D / base_D;
// cout << "[ x:" << x << "; y:" << y << "; z:" << z << " ]" << endl;
solution[0] = x;
solution[1] = y;
solution[2] = z;
} else {
cout << "【无解】";
solution[0] = 0;
solution[1] = 0;
solution[2] = 0;
// return DBL_MIN;
}
}
// Similar with PCL voxelgrid filter
void down_sampling_voxel(pcl::PointCloud<pcl::PointXYZI> &pl_feat,
double voxel_size) {
int intensity = rand() % 255;
if (voxel_size < 0.01) {
return;
}
std::unordered_map<VOXEL_LOC, M_POINT> feat_map;
uint plsize = pl_feat.size();
for (uint i = 0; i < plsize; i++) {
pcl::PointXYZI &p_c = pl_feat[i];
float loc_xyz[3];
for (int j = 0; j < 3; j++) {
loc_xyz[j] = p_c.data[j] / voxel_size;
if (loc_xyz[j] < 0) {
loc_xyz[j] -= 1.0;
}
}
VOXEL_LOC position((int64_t)loc_xyz[0], (int64_t)loc_xyz[1],
(int64_t)loc_xyz[2]);
auto iter = feat_map.find(position);
if (iter != feat_map.end()) {
iter->second.xyz[0] += p_c.x;
iter->second.xyz[1] += p_c.y;
iter->second.xyz[2] += p_c.z;
iter->second.intensity += p_c.intensity;
iter->second.count++;
} else {
M_POINT anp;
anp.xyz[0] = p_c.x;
anp.xyz[1] = p_c.y;
anp.xyz[2] = p_c.z;
anp.intensity = p_c.intensity;
anp.count = 1;
feat_map[position] = anp;
}
}
plsize = feat_map.size();
pl_feat.clear();
pl_feat.resize(plsize);
uint i = 0;
for (auto iter = feat_map.begin(); iter != feat_map.end(); ++iter) {
pl_feat[i].x = iter->second.xyz[0] / iter->second.count;
pl_feat[i].y = iter->second.xyz[1] / iter->second.count;
pl_feat[i].z = iter->second.xyz[2] / iter->second.count;
pl_feat[i].intensity = iter->second.intensity / iter->second.count;
i++;
}
}
void rgb2grey(const cv::Mat &rgb_image, cv::Mat &grey_img) {
for (int x = 0; x < rgb_image.cols; x++) {
for (int y = 0; y < rgb_image.rows; y++) {
grey_img.at<uchar>(y, x) = 1.0 / 3.0 * rgb_image.at<cv::Vec3b>(y, x)[0] +
1.0 / 3.0 * rgb_image.at<cv::Vec3b>(y, x)[1] +
1.0 / 3.0 * rgb_image.at<cv::Vec3b>(y, x)[2];
}
}
}
void mapJet(double v, double vmin, double vmax, uint8_t &r, uint8_t &g,
uint8_t &b) {
r = 255;
g = 255;
b = 255;
if (v < vmin) {
v = vmin;
}
if (v > vmax) {
v = vmax;
}
double dr, dg, db;
if (v < 0.1242) {
db = 0.504 + ((1. - 0.504) / 0.1242) * v;
dg = dr = 0.;
} else if (v < 0.3747) {
db = 1.;
dr = 0.;
dg = (v - 0.1242) * (1. / (0.3747 - 0.1242));
} else if (v < 0.6253) {
db = (0.6253 - v) * (1. / (0.6253 - 0.3747));
dg = 1.;
dr = (v - 0.3747) * (1. / (0.6253 - 0.3747));
} else if (v < 0.8758) {
db = 0.;
dr = 1.;
dg = (0.8758 - v) * (1. / (0.8758 - 0.6253));
} else {
db = 0.;
dg = 0.;
dr = 1. - (v - 0.8758) * ((1. - 0.504) / (1. - 0.8758));
}
r = (uint8_t)(255 * dr);
g = (uint8_t)(255 * dg);
b = (uint8_t)(255 * db);
}
typedef struct VoxelGrid {
float size = 0.5;
int index;
Eigen::Vector3d origin;
pcl::PointCloud<pcl::PointXYZI> cloud;
} VoxelGrid;
typedef struct Voxel {
float size;
Eigen::Vector3d voxel_origin;
Eigen::Vector3d voxel_color;
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud;
Voxel(float _size) : size(_size) {
voxel_origin << 0, 0, 0;
cloud = pcl::PointCloud<pcl::PointXYZI>::Ptr(
new pcl::PointCloud<pcl::PointXYZI>);
};
} Voxel;
#endif