-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathpdi.cpp
376 lines (288 loc) · 10.1 KB
/
pdi.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// This is No Warranty No Copyright Software.
// astar.ai
// May 15, 2019
#include <GL/gl.h>
#include <opencv2/opencv.hpp>
#include "glwindow/scenewindow.hpp"
////////////////////////////////////////////////////////////////////////////////
int ndisp_bar = 1, wsize_bar = 2, thr_bar = 0;
int ndisp_max = 2, wsize_max = 3, thr_max = 30;
int ndisp_now = 32, wsize_now = 9, thr_now = 70;
////////////////////////////////////////////////////////////////////////////////
void OnTrackNdisp(int, void*) {
ndisp_now = 16 + 16 * ndisp_bar;
}
////////////////////////////////////////////////////////////////////////////////
void OnTrackWsize(int, void*) {
wsize_now = 5 + 2 * wsize_bar;
}
////////////////////////////////////////////////////////////////////////////////
void OnTrackThreshold(int, void*) {
thr_now = 70 + thr_bar;
}
////////////////////////////////////////////////////////////////////////////////
int cap_cols, cap_rows, img_width;
cv::Mat T, Kl, Kr, Dl, Dr, xil, xir, Rl, Rr;
cv::Mat lmap[2][2];
// CaliCam stereo is calibrated at 2560x960
// If you want to process image at 1280x480, set half_size to true.
bool half_size = true;
void LoadParameters(std::string file_name) {
cv::FileStorage fs(file_name, cv::FileStorage::READ);
if (!fs.isOpened()) {
std::cout << "Failed to open ini parameters" << std::endl;
exit(-1);
}
cv::Size cap_size;
fs["cap_size" ] >> cap_size;
fs["Kl" ] >> Kl;
fs["Dl" ] >> Dl;
fs["xil" ] >> xil;
fs["Rl" ] >> Rl;
fs["Kr" ] >> Kr;
fs["Dr" ] >> Dr;
fs["xir" ] >> xir;
fs["Rr" ] >> Rr;
fs["T" ] >> T;
fs.release();
if (half_size) {
cap_size = cap_size / 2;
Kl.row(0) = Kl.row(0) / 2.;
Kl.row(1) = Kl.row(1) / 2.;
Kr.row(0) = Kr.row(0) / 2.;
Kr.row(1) = Kr.row(1) / 2.;
}
cap_cols = cap_size.width;
cap_rows = cap_size.height;
img_width = cap_size.width / 2;
}
////////////////////////////////////////////////////////////////////////////////
inline double MatRowMul(cv::Mat m, double x, double y, double z, int r) {
return m.at<double>(r,0) * x + m.at<double>(r,1) * y + m.at<double>(r,2) * z;
}
////////////////////////////////////////////////////////////////////////////////
enum RectMode {
RECT_PERSPECTIVE,
RECT_FISHEYE,
RECT_LONGLAT
};
void InitRectifyMap(cv::Mat K,
cv::Mat D,
cv::Mat R,
cv::Mat Knew,
double xi0,
cv::Size size,
RectMode mode,
cv::Mat& map1,
cv::Mat& map2) {
map1.create(size, CV_32F);
map2.create(size, CV_32F);
double fx = K.at<double>(0,0);
double fy = K.at<double>(1,1);
double cx = K.at<double>(0,2);
double cy = K.at<double>(1,2);
double s = K.at<double>(0,1);
double k1 = D.at<double>(0,0);
double k2 = D.at<double>(0,1);
double p1 = D.at<double>(0,2);
double p2 = D.at<double>(0,3);
cv::Mat Ki = Knew.inv();
cv::Mat Ri = R.inv();
cv::Mat KRi = (Knew * R).inv();
for (int r = 0; r < size.height; ++r) {
for (int c = 0; c < size.width; ++c) {
double xc = 0.;
double yc = 0.;
double zc = 0.;
if (mode == RECT_PERSPECTIVE) {
xc = MatRowMul(KRi, c, r, 1., 0);
yc = MatRowMul(KRi, c, r, 1., 1);
zc = MatRowMul(KRi, c, r, 1., 2);
}
if (mode == RECT_LONGLAT) {
double tt = MatRowMul(Ki, c, r, 1., 0);
double pp = MatRowMul(Ki, c, r, 1., 1);
double xn = -cos(tt);
double yn = -sin(tt) * cos(pp);
double zn = sin(tt) * sin(pp);
xc = MatRowMul(Ri, xn, yn, zn, 0);
yc = MatRowMul(Ri, xn, yn, zn, 1);
zc = MatRowMul(Ri, xn, yn, zn, 2);
}
if (mode == RECT_FISHEYE) {
double ee = MatRowMul(Ki, c, r, 1., 0);
double ff = MatRowMul(Ki, c, r, 1., 1);
double zz = 2. / (ee * ee + ff * ff + 1.);
double xn = zz * ee;
double yn = zz * ff;
double zn = zz - 1.;
xc = MatRowMul(Ri, xn, yn, zn, 0);
yc = MatRowMul(Ri, xn, yn, zn, 1);
zc = MatRowMul(Ri, xn, yn, zn, 2);
}
double rr = sqrt(xc * xc + yc * yc + zc * zc);
double xs = xc / rr;
double ys = yc / rr;
double zs = zc / rr;
double xu = xs / (zs + xi0);
double yu = ys / (zs + xi0);
double r2 = xu * xu + yu * yu;
double r4 = r2 * r2;
double xd = (1+k1*r2+k2*r4)*xu + 2*p1*xu*yu + p2*(r2+2*xu*xu);
double yd = (1+k1*r2+k2*r4)*yu + 2*p2*xu*yu + p1*(r2+2*yu*yu);
double u = fx * xd + s * yd + cx;
double v = fy * yd + cy;
map1.at<float>(r,c) = (float) u;
map2.at<float>(r,c) = (float) v;
}
}
}
////////////////////////////////////////////////////////////////////////////////
int rect_cols = 640, rect_rows = 640;
void InitRectifyMap() {
cv::Size img_size(rect_cols, rect_rows);
cv::Mat Kll = cv::Mat::eye(3, 3, CV_64F);
Kll.at<double>(0,0) = (img_size.width - 1.) / CV_PI;
Kll.at<double>(1,1) = (img_size.height - 1.) / CV_PI;
InitRectifyMap(Kl, Dl, Rl, Kll, xil.at<double>(0,0),
img_size, RECT_LONGLAT, lmap[0][0], lmap[0][1]);
InitRectifyMap(Kr, Dr, Rr, Kll, xir.at<double>(0,0),
img_size, RECT_LONGLAT, lmap[1][0], lmap[1][1]);
}
////////////////////////////////////////////////////////////////////////////////
void DisparityImage(const cv::Mat& recl, const cv::Mat& recr, cv::Mat& dispf) {
cv::Mat disps;
int N = ndisp_now, W = wsize_now, C = recl.channels();
if (1) {
cv::Ptr<cv::StereoSGBM> sgbm =
cv::StereoSGBM::create(0, N, W, 8 * C * W * W, 32 * C * W * W);
sgbm->compute(recl, recr, disps);
} else {
cv::Mat grayl, grayr;
cv::cvtColor(recl, grayl, CV_BGR2GRAY);
cv::cvtColor(recr, grayr, CV_BGR2GRAY);
cv::Ptr<cv::StereoBM> sbm = cv::StereoBM::create(N, W);
sbm->setPreFilterCap(31);
sbm->setMinDisparity(0);
sbm->setTextureThreshold(10);
sbm->setUniquenessRatio(15);
sbm->setSpeckleWindowSize(100);
sbm->setSpeckleRange(32);
sbm->setDisp12MaxDiff(1);
sbm->compute(grayl, grayr, disps);
}
disps.convertTo(dispf, CV_32F, 1.f / 16.f);
}
////////////////////////////////////////////////////////////////////////////////
struct PCL {
cv::Vec3f pts;
cv::Vec3b clr;
};
void PointClouds(const cv::Mat& disp_img,
const cv::Mat& color_img,
std::vector<PCL>& pcl_vec) {
double bl = cv::norm(T);
double pi_w = CV_PI / (rect_cols - 1.);
for (int r = 0; r < color_img.rows; ++r) {
for (int c = 0; c < color_img.cols; ++c) {
float disp = disp_img.at<float>(r, c);
if (disp <= 0.f) {
continue;
}
double tt = (c / (color_img.cols - 1.) - 0.5) * CV_PI;
double pp = (r / (color_img.rows - 1.) - 0.5) * CV_PI;
double cx = std::sin(tt);
double cy = std::cos(tt) * std::sin(pp);
double cz = std::cos(tt) * std::cos(pp);
double cr = hypot(hypot(cx, cy), cz);
double ct = std::acos(cz / cr);
if (ct > (CV_PI / 2.) * (thr_now / 100.)) {
continue;
}
double diff = pi_w * disp;
double mgnt = bl * sin(c * pi_w - diff) / sin(diff);
cv::Vec3b color = color_img.at<cv::Vec3b>(r,c);
PCL pcl;
pcl.pts = cv::Vec3f(cx, cy, cz) * mgnt;
pcl.clr = cv::Vec3b(color(2), color(1), color(0));
pcl_vec.push_back(pcl);
}
}
}
////////////////////////////////////////////////////////////////////////////////
void DrawScene(const std::vector<PCL>& pcl_vec) {
glBegin(GL_POINTS);
for (uint i = 0; i < pcl_vec.size(); ++i) {
PCL pcl = pcl_vec[i];
glColor3ub(pcl.clr(0), pcl.clr(1), pcl.clr(2));
glVertex3f(pcl.pts(0), pcl.pts(1), pcl.pts(2));
}
glEnd();
}
////////////////////////////////////////////////////////////////////////////////
bool live = false; // To run live mode, you need a CaliCam from www.astar.ai
int main(int argc, char** argv) {
std::string param_name = "../astar_calicam.yml";
std::string image_name = "../wm_garden.jpg";
if (argc == 2) {
param_name = argv[1];
}
if (argc == 3) {
param_name = argv[1];
image_name = argv[2];
}
LoadParameters(param_name);
InitRectifyMap();
glwindow::SceneWindow scene(960, 720, "Panorama 3D Scene");
cv::VideoCapture vcapture;
if (live) {
vcapture.open(0);
if (!vcapture.isOpened()) {
std::cout << "Camera doesn't work" << std::endl;
exit(-1);
}
vcapture.set(CV_CAP_PROP_FRAME_WIDTH, cap_cols);
vcapture.set(CV_CAP_PROP_FRAME_HEIGHT, cap_rows);
vcapture.set(CV_CAP_PROP_FPS, 30);
}
std::string win_name = "Fisheye Image";
cv::namedWindow(win_name);
cv::createTrackbar("Num Disp: 16 + 16 *", win_name,
&ndisp_bar, ndisp_max, OnTrackNdisp);
cv::createTrackbar("Blk Size : 5 + 2 * ", win_name,
&wsize_bar, wsize_max, OnTrackWsize);
cv::createTrackbar("Threshold : 70 + ", win_name,
&thr_bar, thr_max, OnTrackThreshold);
cv::Mat raw_img = cv::imread(image_name, cv::IMREAD_COLOR);
cv::Mat raw_imgl, raw_imgr, ll_imgl, ll_imgr;
if (half_size)
cv::resize(raw_img, raw_img, cv::Size(), 0.5, 0.5);
while (1) {
if (live)
vcapture >> raw_img;
if (raw_img.total() == 0) {
std::cout << "Image capture error" << std::endl;
exit(-1);
}
raw_img(cv::Rect( 0, 0, img_width, cap_rows)).copyTo(raw_imgl);
raw_img(cv::Rect(img_width, 0, img_width, cap_rows)).copyTo(raw_imgr);
cv::remap(raw_imgl, ll_imgl, lmap[0][0], lmap[0][1], cv::INTER_LINEAR);
cv::remap(raw_imgr, ll_imgr, lmap[1][0], lmap[1][1], cv::INTER_LINEAR);
cv::Mat disp_img;
std::vector<PCL> pcl_vec;
DisparityImage(ll_imgl, ll_imgr, disp_img);
PointClouds(disp_img, ll_imgl, pcl_vec);
if (scene.win.alive()) {
if (scene.start_draw()) {
DrawScene(pcl_vec);
scene.finish_draw();
}
}
imshow(win_name, raw_imgl);
char key = cv::waitKey(1);
if (key == 'q' || key == 'Q' || key == 27)
break;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////