This repository has been archived by the owner on Jun 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParallelCapture.cpp
270 lines (234 loc) · 7.17 KB
/
ParallelCapture.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
#include "ParallelCapture.h"
#include "IPUtils.h"
ParallelCapture::ParallelCapture()
{
}
ParallelCapture::~ParallelCapture()
{
}
void ParallelCapture::initCalibration(int x_dims, int y_dims, float square_size, int target_type, cv::Size image_size, int min_req_samples)
{
this->target_type = target_type;
this->image_size = image_size;
this->min_req_samples = min_req_samples;
tg_x = x_dims;
tg_y = y_dims;
tg_n = x_dims * y_dims;
tg_square_size = square_size;
samples = 0;
calibration_initialised = true;
calibration_completed = false;
}
bool ParallelCapture::addCalibrationImage(cv::Mat cam_frame, cv::Mat ir_frame)
{
if (!calibration_initialised || calibration_completed)
return false;
bool ret_bool = false;
//Mat ir_frame_exp = IPUtils::getExponential(ir_frame);
//Mat ir_frame_exp = ir_frame;
cv::Mat ir_frame_filtered;
bilateralFilter(ir_frame, ir_frame_filtered, 3, 20, 20);
cv::Mat image_with_corners[2] = { ir_frame_filtered, cam_frame };
cv::Mat image[2] = { ir_frame_filtered, cam_frame };
// Use grayscale image for finding chessboard corners
cvtColor(image[1], image[1], CV_BGR2GRAY);
cv::Size tg_size = cv::Size(tg_x, tg_y);
// Vector of 2d corner coords
vector<cv::Point2f> corners[2];
bool found_corners[2];
for (int i = 0; i < 2; i++)
{
if (target_type == TARGET_CHESSBOARD)
{
found_corners[i] = findChessboardCorners(image[i], tg_size, corners[i],
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE | CV_CALIB_CB_FAST_CHECK);
}
else if (target_type == TARGET_CIRCLES)
{
found_corners[i] = findCirclesGrid(image[i], tg_size, corners[i],
cv::CALIB_CB_SYMMETRIC_GRID);
}
else if (target_type == TARGET_ASYMMETRIC_CIRCLES)
{
found_corners[i] = findCirclesGrid(image[i], tg_size, corners[i],
cv::CALIB_CB_ASYMMETRIC_GRID);
}
}
if (found_corners[0])
std::cout << "rs" << std::endl;
if (found_corners[1])
std::cout << "cam" << std::endl;
// If we found the chessboard in both images, refine and store the data
if (found_corners[0] && found_corners[1])
{
for (int i = 0; i < 2; i++)
{
if (target_type == TARGET_CHESSBOARD)
{
// For further accuracy, parameters taken from OPENCV calibration docs
cornerSubPix(image[i], corners[i], cv::Size(11, 11), cv::Size(-1, -1),
cvTermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 30, 0.01));
}
// Add the corner points to the calibration points array
image_points_vec[i].push_back(corners[i]);
drawChessboardCorners(image_with_corners[i], tg_size, (cv::Mat)corners[i], true);
}
char name[40];
sprintf(name, "D:/calib_images/calibration%02dIR.png", samples);
imwrite(name, image_with_corners[0]);
sprintf(name, "D:/calib_images/calibration%02dcolour.png", samples);
imwrite(name, image_with_corners[1]);
samples++;
ret_bool = true;
}
return ret_bool;
}
bool ParallelCapture::performCalibration()
{
if((!calibration_initialised) || calibration_completed)
return false;
bool ret_bool = false;
if (samples >= min_req_samples)
{
calibration_initialised = false;
// Compile vector of chessboard positions for each sample
// (the top level elements should all be identical if same board used)
tg_points_vec.resize(samples, vector<cv::Point3f>(tg_n));
if (target_type != TARGET_ASYMMETRIC_CIRCLES)
{
for (int i = 0; i < samples; i++)
{
for (int j = 0; j < tg_y; j++)
{
for (int k = 0; k < tg_x; k++)
{
tg_points_vec[i][j*tg_x + k] = cv::Point3f(k * tg_square_size, j * tg_square_size, 0);
}
}
}
}
else
{
for (int i = 0; i < samples; i++)
{
bool odd = false;
float ofset = 0.0;
for (int j = 0; j < tg_y; j++)
{
for (int k = 0; k < tg_x; k++)
{
if (odd)
ofset = tg_square_size;
else
ofset = 0.0;
tg_points_vec[i][j*tg_x + k] = cv::Point3f((2*k)+ofset, (j * tg_square_size), 0);
}
odd = !odd;
}
}
}
// variables for calibration
cv::Mat cam_mat0, cam_mat1, dist_mat0, dist_mat1;
vector<cv::Mat> rot_mat0, rot_mat1, tran_mat0, tran_mat1;
int flags = 0;
calibrateCamera(tg_points_vec, image_points_vec[0], image_size,
cam_mat0, dist_mat0,
rot_mat0, tran_mat0, flags,
cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 100, 0.001));
calibrateCamera(tg_points_vec, image_points_vec[1], image_size,
cam_mat1, dist_mat1,
rot_mat1, tran_mat1, flags,
cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 100, 0.001));
cv::Mat R, T, E, F;
flags = CV_CALIB_USE_INTRINSIC_GUESS | CV_CALIB_FIX_INTRINSIC;
double temp;
temp = stereoCalibrate(tg_points_vec, image_points_vec[0], image_points_vec[1],
cam_mat0, dist_mat0,
cam_mat1, dist_mat1,
image_size, R, T, E, F, flags,
cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 100, 0.001));
cout << temp << endl;
// Vairables for rectification
cv::Mat rot0, rot1, proj0, proj1, q;
stereoRectify(cam_mat0, dist_mat0, cam_mat1, dist_mat1, image_size,
R, T, rot0, rot1, proj0, proj1, q, 0, -1);
// Get remap matrices for both sides of the camera
initUndistortRectifyMap(cam_mat0, dist_mat0, rot0, proj0, image_size, CV_32FC1, map0x, map0y);
initUndistortRectifyMap(cam_mat1, dist_mat1, rot1, proj1, image_size, CV_32FC1, map1x, map1y);
calibration_completed = true;
ret_bool = true;
}
return ret_bool;
}
bool ParallelCapture::remapImages(cv::Mat cam_frame, cv::Mat depthcam_ir, cv::Mat depthcam_depth)
{
bool ret_bool = false;
if (!calibration_completed)
return false;
int check = 0;
if (!cam_frame.empty())
{
remap(cam_frame, cam_remapped, map1x, map1y, cv::INTER_LINEAR);
check++;
}
if (!depthcam_ir.empty())
{
remap(depthcam_ir, ir_remapped, map0x, map0y, cv::INTER_LINEAR);
check++;
}
if (!depthcam_depth.empty())
{
remap(depthcam_depth, depth_remapped, map0x, map0y, cv::INTER_LINEAR);
check++;
}
if(check>0)
ret_bool = true;
return ret_bool;
}
bool ParallelCapture::saveCalibration(string filename)
{
if (!calibration_completed)
return false;
const char * filename_c = filename.c_str();
FILE* f = fopen(filename_c, "wb");
try
{
if (!f) return false;
if (!fwrite(map0x.data, sizeof(float), map0x.rows*map0x.cols, f)) return false;
if (!fwrite(map0y.data, sizeof(float), map0y.rows*map0y.cols, f)) return false;
if (!fwrite(map1x.data, sizeof(float), map1x.rows*map1x.cols, f)) return false;
if (!fwrite(map1y.data, sizeof(float), map1y.rows*map1y.cols, f)) return false;
fclose(f);
}
catch (...)
{
fclose(f);
throw;
}
return true;
}
bool ParallelCapture::loadCalibration(string path)
{
const char * path_c = path.c_str();
map0x = cv::Mat(image_size, CV_32FC1);
map0y = cv::Mat(image_size, CV_32FC1);
map1x = cv::Mat(image_size, CV_32FC1);
map1y = cv::Mat(image_size, CV_32FC1);
FILE* f = fopen(path_c, "rb");
try
{
if (!f) return false;
if (!fread(map0x.data, sizeof(float), map0x.rows*map0x.cols, f)) return false;
if (!fread(map0y.data, sizeof(float), map0y.rows*map0y.cols, f)) return false;
if (!fread(map1x.data, sizeof(float), map1x.rows*map1x.cols, f)) return false;
if (!fread(map1y.data, sizeof(float), map1y.rows*map1y.cols, f)) return false;
fclose(f);
}
catch (...)
{
fclose(f);
throw;
}
calibration_completed = true;
return true;
}