1 |
#include "SMCalibrationWorker.h"
|
1 |
#include "SMCalibrationWorker.h"
|
2 |
#include "SMCalibrationParameters.h"
|
2 |
#include "SMCalibrationParameters.h"
|
3 |
|
3 |
|
4 |
#include "cvtools.h"
|
4 |
#include "cvtools.h"
|
5 |
|
5 |
|
6 |
#include <QSettings>
|
6 |
#include <QSettings>
|
7 |
|
7 |
|
8 |
void SMCalibrationWorker::performCalibration(std::vector<SMCalibrationSet> calibrationData){
|
8 |
void SMCalibrationWorker::performCalibration(std::vector<SMCalibrationSet> calibrationData){
|
9 |
|
9 |
|
10 |
QSettings settings;
|
10 |
QSettings settings;
|
11 |
|
11 |
|
12 |
// Number of saddle points on calibration pattern
|
12 |
// Number of saddle points on calibration pattern
|
13 |
int checkerCountX = settings.value("calibration/checkerCountX", 22).toInt();
|
13 |
int checkerCountX = settings.value("calibration/checkerCountX", 22).toInt();
|
14 |
int checkerCountY = settings.value("calibration/checkerCountY", 13).toInt();
|
14 |
int checkerCountY = settings.value("calibration/checkerCountY", 13).toInt();
|
15 |
cv::Size checkerCount(checkerCountX, checkerCountY);
|
15 |
cv::Size checkerCount(checkerCountX, checkerCountY);
|
16 |
|
16 |
|
17 |
int nSets = calibrationData.size();
|
17 |
int nSets = calibrationData.size();
|
18 |
|
18 |
|
19 |
std::vector< std::vector<cv::Point2f> > qc0, qc1;
|
19 |
std::vector< std::vector<cv::Point2f> > qc0, qc1;
|
- |
|
20 |
std::vector< std::vector<cv::Point2f> > qc0Stereo, qc1Stereo;
|
- |
|
21 |
|
20 |
std::vector<float> angles;
|
22 |
std::vector<float> angles;
|
21 |
|
23 |
|
22 |
// Loop through calibration sets
|
24 |
// Loop through calibration sets
|
23 |
for(int i=0; i<nSets; i++){
|
25 |
for(int i=0; i<nSets; i++){
|
24 |
|
26 |
|
25 |
SMCalibrationSet SMCalibrationSetI = calibrationData[i];
|
27 |
SMCalibrationSet SMCalibrationSetI = calibrationData[i];
|
26 |
|
28 |
|
27 |
if(!SMCalibrationSetI.checked)
|
29 |
if(!SMCalibrationSetI.checked)
|
28 |
continue;
|
30 |
continue;
|
29 |
|
31 |
|
30 |
// Camera 0
|
32 |
// Camera 0
|
31 |
std::vector<cv::Point2f> qci0;
|
33 |
std::vector<cv::Point2f> qci0;
|
32 |
|
34 |
|
33 |
// Convert to grayscale
|
35 |
// Convert to grayscale
|
34 |
cv::Mat gray;
|
36 |
cv::Mat gray;
|
35 |
if(SMCalibrationSetI.frame0.channels() == 1)
|
37 |
if(SMCalibrationSetI.frame0.channels() == 1)
|
36 |
cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_BayerBG2GRAY);
|
38 |
cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_BayerBG2GRAY);
|
37 |
else
|
39 |
else
|
38 |
cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_RGB2GRAY);
|
40 |
cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_RGB2GRAY);
|
39 |
|
41 |
|
40 |
// Extract checker corners
|
42 |
// Extract checker corners
|
41 |
bool success0 = cv::findChessboardCorners(gray, checkerCount, qci0, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
|
43 |
bool success0 = cv::findChessboardCorners(gray, checkerCount, qci0, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
|
42 |
if(success0){
|
44 |
if(success0){
|
43 |
cv::cornerSubPix(gray, qci0, cv::Size(6, 6), cv::Size(1, 1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
|
45 |
cv::cornerSubPix(gray, qci0, cv::Size(6, 6), cv::Size(1, 1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
|
44 |
// Draw colored chessboard
|
46 |
// Draw colored chessboard
|
45 |
cv::Mat color;
|
47 |
cv::Mat color;
|
46 |
if(SMCalibrationSetI.frame0.channels() == 1)
|
48 |
if(SMCalibrationSetI.frame0.channels() == 1)
|
47 |
cv::cvtColor(SMCalibrationSetI.frame0, color, CV_BayerBG2RGB);
|
49 |
cv::cvtColor(SMCalibrationSetI.frame0, color, CV_BayerBG2RGB);
|
48 |
else
|
50 |
else
|
49 |
color = SMCalibrationSetI.frame0.clone();
|
51 |
color = SMCalibrationSetI.frame0.clone();
|
50 |
|
52 |
|
51 |
cvtools::drawChessboardCorners(color, checkerCount, qci0, success0, 10);
|
53 |
cvtools::drawChessboardCorners(color, checkerCount, qci0, success0, 10);
|
52 |
SMCalibrationSetI.frame0Result = color;
|
54 |
SMCalibrationSetI.frame0Result = color;
|
53 |
}
|
55 |
}
|
54 |
|
56 |
|
55 |
emit newFrameResult(i, 0, success0, SMCalibrationSetI.frame0Result);
|
57 |
emit newFrameResult(i, 0, success0, SMCalibrationSetI.frame0Result);
|
56 |
|
58 |
|
57 |
// Camera 1
|
59 |
// Camera 1
|
58 |
std::vector<cv::Point2f> qci1;
|
60 |
std::vector<cv::Point2f> qci1;
|
59 |
|
61 |
|
60 |
// Convert to grayscale
|
62 |
// Convert to grayscale
|
61 |
if(SMCalibrationSetI.frame1.channels() == 1)
|
63 |
if(SMCalibrationSetI.frame1.channels() == 1)
|
62 |
cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_BayerBG2GRAY);
|
64 |
cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_BayerBG2GRAY);
|
63 |
else
|
65 |
else
|
64 |
cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_RGB2GRAY);
|
66 |
cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_RGB2GRAY);
|
65 |
|
67 |
|
66 |
// Extract checker corners
|
68 |
// Extract checker corners
|
67 |
bool success1 = cv::findChessboardCorners(gray, checkerCount, qci1, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
|
69 |
bool success1 = cv::findChessboardCorners(gray, checkerCount, qci1, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
|
68 |
if(success1){
|
70 |
if(success1){
|
69 |
cv::cornerSubPix(gray, qci1, cv::Size(6, 6), cv::Size(1, 1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
|
71 |
cv::cornerSubPix(gray, qci1, cv::Size(6, 6), cv::Size(1, 1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
|
70 |
// Draw colored chessboard
|
72 |
// Draw colored chessboard
|
71 |
cv::Mat color;
|
73 |
cv::Mat color;
|
72 |
if(SMCalibrationSetI.frame1.channels() == 1)
|
74 |
if(SMCalibrationSetI.frame1.channels() == 1)
|
73 |
cv::cvtColor(SMCalibrationSetI.frame1, color, CV_BayerBG2RGB);
|
75 |
cv::cvtColor(SMCalibrationSetI.frame1, color, CV_BayerBG2RGB);
|
74 |
else
|
76 |
else
|
75 |
color = SMCalibrationSetI.frame1.clone();
|
77 |
color = SMCalibrationSetI.frame1.clone();
|
76 |
|
78 |
|
77 |
cvtools::drawChessboardCorners(color, checkerCount, qci1, success1, 10);
|
79 |
cvtools::drawChessboardCorners(color, checkerCount, qci1, success1, 10);
|
78 |
SMCalibrationSetI.frame1Result = color;
|
80 |
SMCalibrationSetI.frame1Result = color;
|
79 |
}
|
81 |
}
|
80 |
|
82 |
|
81 |
emit newFrameResult(i, 1, success1, SMCalibrationSetI.frame1Result);
|
83 |
emit newFrameResult(i, 1, success1, SMCalibrationSetI.frame1Result);
|
82 |
|
84 |
|
83 |
SMCalibrationSetI.success = success0 && success1;
|
- |
|
84 |
|
- |
|
85 |
// Add to whole set
|
- |
|
86 |
if(SMCalibrationSetI.success){
|
85 |
if(success0)
|
87 |
qc0.push_back(qci0);
|
86 |
qc0.push_back(qci0);
|
- |
|
87 |
|
- |
|
88 |
if(success1)
|
88 |
qc1.push_back(qci1);
|
89 |
qc1.push_back(qci1);
|
- |
|
90 |
|
- |
|
91 |
if(success0 && success1){
|
- |
|
92 |
qc0Stereo.push_back(qci0);
|
- |
|
93 |
qc1Stereo.push_back(qci1);
|
89 |
angles.push_back(SMCalibrationSetI.rotationAngle);
|
94 |
angles.push_back(SMCalibrationSetI.rotationAngle);
|
90 |
}
|
95 |
}
|
91 |
|
96 |
|
92 |
// Show progress
|
97 |
// Show progress
|
93 |
emit newSetProcessed(i);
|
98 |
emit newSetProcessed(i);
|
94 |
}
|
99 |
}
|
95 |
|
100 |
|
96 |
int nValidSets = qc0.size();
|
101 |
int nValidSets = angles.size();
|
97 |
if(nValidSets < 2){
|
102 |
if(nValidSets < 2){
|
98 |
std::cerr << "Not enough valid calibration sequences!" << std::endl;
|
103 |
std::cerr << "Not enough valid calibration sequences!" << std::endl;
|
99 |
emit done();
|
104 |
emit done();
|
100 |
return;
|
105 |
return;
|
101 |
}
|
106 |
}
|
102 |
|
107 |
|
103 |
// Generate world object coordinates [mm]
|
108 |
// Generate world object coordinates [mm]
|
104 |
float checkerSize = settings.value("calibration/checkerSize", 15.0).toFloat(); // width and height of one field in mm
|
109 |
float checkerSize = settings.value("calibration/checkerSize", 15.0).toFloat(); // width and height of one field in mm
|
105 |
std::vector<cv::Point3f> Qi;
|
110 |
std::vector<cv::Point3f> Qi;
|
106 |
for (int h=0; h<checkerCount.height; h++)
|
111 |
for (int h=0; h<checkerCount.height; h++)
|
107 |
for (int w=0; w<checkerCount.width; w++)
|
112 |
for (int w=0; w<checkerCount.width; w++)
|
108 |
Qi.push_back(cv::Point3f(checkerSize * w, checkerSize* h, 0.0));
|
113 |
Qi.push_back(cv::Point3f(checkerSize * w, checkerSize* h, 0.0));
|
- |
|
114 |
|
109 |
std::vector< std::vector<cv::Point3f> > Q;
|
115 |
std::vector< std::vector<cv::Point3f> > Q0, Q1, QStereo;
|
110 |
for(int i=0; i<qc0.size(); i++)
|
116 |
for(int i=0; i<qc0.size(); i++)
|
111 |
Q.push_back(Qi);
|
117 |
Q0.push_back(Qi);
|
- |
|
118 |
for(int i=0; i<qc1.size(); i++)
|
- |
|
119 |
Q1.push_back(Qi);
|
- |
|
120 |
for(int i=0; i<nValidSets; i++)
|
- |
|
121 |
QStereo.push_back(Qi);
|
112 |
|
122 |
|
113 |
// calibrate the cameras
|
123 |
// calibrate the cameras
|
114 |
SMCalibrationParameters cal;
|
124 |
SMCalibrationParameters cal;
|
115 |
cal.frameWidth = calibrationData[0].frame0.cols;
|
125 |
cal.frameWidth = calibrationData[0].frame0.cols;
|
116 |
cal.frameHeight = calibrationData[0].frame0.rows;
|
126 |
cal.frameHeight = calibrationData[0].frame0.rows;
|
117 |
cv::Size frameSize(cal.frameWidth, cal.frameHeight);
|
127 |
cv::Size frameSize(cal.frameWidth, cal.frameHeight);
|
118 |
|
128 |
|
119 |
// determine only k1, k2 for lens distortion
|
129 |
// determine only k1, k2 for lens distortion
|
120 |
int flags = cv::CALIB_FIX_K3 + cv::CALIB_ZERO_TANGENT_DIST + cv::CALIB_FIX_PRINCIPAL_POINT + cv::CALIB_FIX_ASPECT_RATIO;
|
130 |
int flags = cv::CALIB_FIX_K3 + cv::CALIB_ZERO_TANGENT_DIST + cv::CALIB_FIX_PRINCIPAL_POINT + cv::CALIB_FIX_ASPECT_RATIO;
|
121 |
// Note: several of the output arguments below must be cv::Mat, otherwise segfault
|
131 |
// Note: several of the output arguments below must be cv::Mat, otherwise segfault
|
122 |
std::vector<cv::Mat> cam_rvecs0, cam_tvecs0;
|
132 |
std::vector<cv::Mat> cam_rvecs0, cam_tvecs0;
|
123 |
cal.cam0_error = cv::calibrateCamera(Q, qc0, frameSize, cal.K0, cal.k0, cam_rvecs0, cam_tvecs0, flags,
|
133 |
cal.cam0_error = cv::calibrateCamera(Q0, qc0, frameSize, cal.K0, cal.k0, cam_rvecs0, cam_tvecs0, flags,
|
124 |
cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
|
134 |
cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
|
125 |
//std::cout << cal.k0 << std::endl;
|
135 |
//std::cout << cal.k0 << std::endl;
|
126 |
// // refine extrinsics for camera 0
|
136 |
// // refine extrinsics for camera 0
|
127 |
// for(int i=0; i<Q.size(); i++)
|
137 |
// for(int i=0; i<Q.size(); i++)
|
128 |
// cv::solvePnPRansac(Q[i], qc0[i], cal.K0, cal.k0, cam_rvecs0[i], cam_tvecs0[i], true, 100, 0.05, 100, cv::noArray(), CV_ITERATIVE);
|
138 |
// cv::solvePnPRansac(Q[i], qc0[i], cal.K0, cal.k0, cam_rvecs0[i], cam_tvecs0[i], true, 100, 0.05, 100, cv::noArray(), CV_ITERATIVE);
|
129 |
|
139 |
|
130 |
std::vector<cv::Mat> cam_rvecs1, cam_tvecs1;
|
140 |
std::vector<cv::Mat> cam_rvecs1, cam_tvecs1;
|
131 |
cal.cam1_error = cv::calibrateCamera(Q, qc1, frameSize, cal.K1, cal.k1, cam_rvecs1, cam_tvecs1, flags,
|
141 |
cal.cam1_error = cv::calibrateCamera(Q1, qc1, frameSize, cal.K1, cal.k1, cam_rvecs1, cam_tvecs1, flags,
|
132 |
cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
|
142 |
cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
|
133 |
//std::cout << cal.k1 << std::endl;
|
143 |
//std::cout << cal.k1 << std::endl;
|
134 |
// stereo calibration
|
144 |
// stereo calibration
|
135 |
int flags_stereo = cv::CALIB_FIX_INTRINSIC;// + cv::CALIB_FIX_K2 + cv::CALIB_FIX_K3 + cv::CALIB_ZERO_TANGENT_DIST + cv::CALIB_FIX_PRINCIPAL_POINT + cv::CALIB_FIX_ASPECT_RATIO;
|
145 |
int flags_stereo = cv::CALIB_FIX_INTRINSIC;// + cv::CALIB_FIX_K2 + cv::CALIB_FIX_K3 + cv::CALIB_ZERO_TANGENT_DIST + cv::CALIB_FIX_PRINCIPAL_POINT + cv::CALIB_FIX_ASPECT_RATIO;
|
136 |
cv::Mat E, F, R1, T1;
|
146 |
cv::Mat E, F, R1, T1;
|
137 |
cal.stereo_error = cv::stereoCalibrate(Q, qc0, qc1, cal.K0, cal.k0, cal.K1, cal.k1,
|
147 |
cal.stereo_error = cv::stereoCalibrate(QStereo, qc0Stereo, qc1Stereo, cal.K0, cal.k0, cal.K1, cal.k1,
|
138 |
frameSize, R1, T1, E, F,
|
148 |
frameSize, R1, T1, E, F,
|
139 |
cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 200, DBL_EPSILON),
|
149 |
cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 200, DBL_EPSILON),
|
140 |
flags_stereo);
|
150 |
flags_stereo);
|
141 |
|
151 |
|
142 |
cal.R1 = R1;
|
152 |
cal.R1 = R1;
|
143 |
cal.T1 = T1;
|
153 |
cal.T1 = T1;
|
144 |
cal.E = E;
|
154 |
cal.E = E;
|
145 |
cal.F = F;
|
155 |
cal.F = F;
|
146 |
|
156 |
|
147 |
// // hand-eye calibration
|
157 |
// // hand-eye calibration
|
148 |
// std::vector<cv::Matx33f> Rc(nValidSets - 1); // rotations/translations of the checkerboard in camera 0 reference frame
|
158 |
// std::vector<cv::Matx33f> Rc(nValidSets - 1); // rotations/translations of the checkerboard in camera 0 reference frame
|
149 |
// std::vector<cv::Vec3f> Tc(nValidSets - 1);
|
159 |
// std::vector<cv::Vec3f> Tc(nValidSets - 1);
|
150 |
// std::vector<cv::Matx33f> Rr(nValidSets - 1); // in rotation stage reference frame
|
160 |
// std::vector<cv::Matx33f> Rr(nValidSets - 1); // in rotation stage reference frame
|
151 |
// std::vector<cv::Vec3f> Tr(nValidSets - 1);
|
161 |
// std::vector<cv::Vec3f> Tr(nValidSets - 1);
|
152 |
// for(int i=0; i<nValidSets-1; i++){
|
162 |
// for(int i=0; i<nValidSets-1; i++){
|
153 |
// // relative transformations in camera
|
163 |
// // relative transformations in camera
|
154 |
// cv::Mat cRw1, cRw2;
|
164 |
// cv::Mat cRw1, cRw2;
|
155 |
// cv::Rodrigues(cam_rvecs0[i], cRw1);
|
165 |
// cv::Rodrigues(cam_rvecs0[i], cRw1);
|
156 |
// cv::Rodrigues(cam_rvecs0[i+1], cRw2);
|
166 |
// cv::Rodrigues(cam_rvecs0[i+1], cRw2);
|
157 |
// cv::Mat cTw1 = cam_tvecs0[i];
|
167 |
// cv::Mat cTw1 = cam_tvecs0[i];
|
158 |
// cv::Mat cTw2 = cam_tvecs0[i+1];
|
168 |
// cv::Mat cTw2 = cam_tvecs0[i+1];
|
159 |
// cv::Mat w1Rc = cRw1.t();
|
169 |
// cv::Mat w1Rc = cRw1.t();
|
160 |
// cv::Mat w1Tc = -cRw1.t()*cTw1;
|
170 |
// cv::Mat w1Tc = -cRw1.t()*cTw1;
|
161 |
// Rc[i] = cv::Mat(cRw2*w1Rc);
|
171 |
// Rc[i] = cv::Mat(cRw2*w1Rc);
|
162 |
// Tc[i] = cv::Mat(cRw2*w1Tc+cTw2);
|
172 |
// Tc[i] = cv::Mat(cRw2*w1Tc+cTw2);
|
163 |
|
173 |
|
164 |
// // relative transformations in rotation stage
|
174 |
// // relative transformations in rotation stage
|
165 |
// // we define the rotation axis to be in origo, pointing in positive y direction
|
175 |
// // we define the rotation axis to be in origo, pointing in positive y direction
|
166 |
// float angleRadians = (angles[i+1]-angles[i])/180.0*M_PI;
|
176 |
// float angleRadians = (angles[i+1]-angles[i])/180.0*M_PI;
|
167 |
// cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
|
177 |
// cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
|
168 |
// cv::Mat Rri;
|
178 |
// cv::Mat Rri;
|
169 |
// cv::Rodrigues(rot_rvec, Rri);
|
179 |
// cv::Rodrigues(rot_rvec, Rri);
|
170 |
// Rr[i] = Rri;
|
180 |
// Rr[i] = Rri;
|
171 |
// Tr[i] = 0.0;
|
181 |
// Tr[i] = 0.0;
|
172 |
|
182 |
|
173 |
//// std::cout << i << std::endl;
|
183 |
//// std::cout << i << std::endl;
|
174 |
//// std::cout << "cTw1" << cTw1 << std::endl;
|
184 |
//// std::cout << "cTw1" << cTw1 << std::endl;
|
175 |
//// std::cout << "cTw2" << cTw2 << std::endl;
|
185 |
//// std::cout << "cTw2" << cTw2 << std::endl;
|
176 |
//// std::cout << "w2Rc" << w2Rc << std::endl;
|
186 |
//// std::cout << "w2Rc" << w2Rc << std::endl;
|
177 |
//// std::cout << "w2Tc" << w2Tc << std::endl;
|
187 |
//// std::cout << "w2Tc" << w2Tc << std::endl;
|
178 |
|
188 |
|
179 |
//// std::cout << "w2Rc" << w2Rc << std::endl;
|
189 |
//// std::cout << "w2Rc" << w2Rc << std::endl;
|
180 |
//// std::cout << "w2Tc" << w2Tc << std::endl;
|
190 |
//// std::cout << "w2Tc" << w2Tc << std::endl;
|
181 |
|
191 |
|
182 |
//// cv::Mat Rci;
|
192 |
//// cv::Mat Rci;
|
183 |
//// cv::Rodrigues(Rc[i], Rci);
|
193 |
//// cv::Rodrigues(Rc[i], Rci);
|
184 |
//// std::cout << "Rci" << Rci << std::endl;
|
194 |
//// std::cout << "Rci" << Rci << std::endl;
|
185 |
//// std::cout << "Tc[i]" << Tc[i] << std::endl;
|
195 |
//// std::cout << "Tc[i]" << Tc[i] << std::endl;
|
186 |
|
196 |
|
187 |
//// std::cout << "rot_rvec" << rot_rvec << std::endl;
|
197 |
//// std::cout << "rot_rvec" << rot_rvec << std::endl;
|
188 |
//// std::cout << "Tr[i]" << Tr[i] << std::endl;
|
198 |
//// std::cout << "Tr[i]" << Tr[i] << std::endl;
|
189 |
//// std::cout << std::endl;
|
199 |
//// std::cout << std::endl;
|
190 |
// }
|
200 |
// }
|
191 |
|
201 |
|
192 |
// // determine the transformation from rotation stage to camera 0
|
202 |
// // determine the transformation from rotation stage to camera 0
|
193 |
// cvtools::handEyeCalibrationTsai(Rc, Tc, Rr, Tr, cal.Rr, cal.Tr);
|
203 |
// cvtools::handEyeCalibrationTsai(Rc, Tc, Rr, Tr, cal.Rr, cal.Tr);
|
194 |
|
204 |
|
195 |
// for(int i=0; i<nValidSets-1; i++){
|
205 |
// for(int i=0; i<nValidSets-1; i++){
|
196 |
// std::cout << i << std::endl;
|
206 |
// std::cout << i << std::endl;
|
197 |
|
207 |
|
198 |
// cv::Mat Rci;
|
208 |
// cv::Mat Rci;
|
199 |
// cv::Rodrigues(Rc[i], Rci);
|
209 |
// cv::Rodrigues(Rc[i], Rci);
|
200 |
// std::cout << "Rc[i]" << Rci << std::endl;
|
210 |
// std::cout << "Rc[i]" << Rci << std::endl;
|
201 |
// std::cout << "Tc[i]" << Tc[i] << std::endl;
|
211 |
// std::cout << "Tc[i]" << Tc[i] << std::endl;
|
202 |
|
212 |
|
203 |
// cv::Mat Rri;
|
213 |
// cv::Mat Rri;
|
204 |
// cv::Rodrigues(Rr[i], Rri);
|
214 |
// cv::Rodrigues(Rr[i], Rri);
|
205 |
// std::cout << "Rr[i]" << Rri << std::endl;
|
215 |
// std::cout << "Rr[i]" << Rri << std::endl;
|
206 |
// std::cout << "Tr[i]" << Tr[i] << std::endl;
|
216 |
// std::cout << "Tr[i]" << Tr[i] << std::endl;
|
207 |
|
217 |
|
208 |
// cv::Mat Rcr = cv::Mat(cal.Rr)*cv::Mat(Rc[i])*cv::Mat(cal.Rr.t());
|
218 |
// cv::Mat Rcr = cv::Mat(cal.Rr)*cv::Mat(Rc[i])*cv::Mat(cal.Rr.t());
|
209 |
// cv::Rodrigues(Rcr, Rcr);
|
219 |
// cv::Rodrigues(Rcr, Rcr);
|
210 |
// cv::Mat Tcr = -cv::Mat(cal.Rr)*cv::Mat(Rc[i])*cv::Mat(cal.Rr.t())*cv::Mat(cal.Tr) + cv::Mat(cal.Rr)*cv::Mat(Tc[i]) + cv::Mat(cal.Tr);
|
220 |
// cv::Mat Tcr = -cv::Mat(cal.Rr)*cv::Mat(Rc[i])*cv::Mat(cal.Rr.t())*cv::Mat(cal.Tr) + cv::Mat(cal.Rr)*cv::Mat(Tc[i]) + cv::Mat(cal.Tr);
|
211 |
// std::cout << "Rcr[i]" << Rcr << std::endl;
|
221 |
// std::cout << "Rcr[i]" << Rcr << std::endl;
|
212 |
// std::cout << "Tcr[i]" << Tcr << std::endl;
|
222 |
// std::cout << "Tcr[i]" << Tcr << std::endl;
|
213 |
// std::cout << std::endl;
|
223 |
// std::cout << std::endl;
|
214 |
// }
|
224 |
// }
|
215 |
|
225 |
|
216 |
|
226 |
|
217 |
// Direct rotation axis calibration //
|
227 |
// Direct rotation axis calibration //
|
218 |
// full camera matrices
|
228 |
// full camera matrices
|
219 |
cv::Matx34f P0 = cv::Matx34f::eye();
|
229 |
cv::Matx34f P0 = cv::Matx34f::eye();
|
220 |
cv::Mat RT1(3, 4, CV_32F);
|
230 |
cv::Mat RT1(3, 4, CV_32F);
|
221 |
cv::Mat(cal.R1).copyTo(RT1(cv::Range(0, 3), cv::Range(0, 3)));
|
231 |
cv::Mat(cal.R1).copyTo(RT1(cv::Range(0, 3), cv::Range(0, 3)));
|
222 |
cv::Mat(cal.T1).copyTo(RT1(cv::Range(0, 3), cv::Range(3, 4)));
|
232 |
cv::Mat(cal.T1).copyTo(RT1(cv::Range(0, 3), cv::Range(3, 4)));
|
223 |
cv::Matx34f P1 = cv::Matx34f(RT1);
|
233 |
cv::Matx34f P1 = cv::Matx34f(RT1);
|
224 |
|
234 |
|
225 |
// calibration points in camera 0 frame
|
235 |
// calibration points in camera 0 frame
|
226 |
std::vector< std::vector<cv::Point3f> > Qcam;
|
236 |
std::vector< std::vector<cv::Point3f> > Qcam;
|
227 |
|
237 |
|
228 |
for(int i=0; i<nValidSets; i++){
|
238 |
for(int i=0; i<nValidSets; i++){
|
229 |
std::vector<cv::Point2f> qc0i, qc1i;
|
239 |
std::vector<cv::Point2f> qc0i, qc1i;
|
230 |
|
240 |
|
231 |
cv::undistortPoints(qc0[i], qc0i, cal.K0, cal.k0);
|
241 |
cv::undistortPoints(qc0[i], qc0i, cal.K0, cal.k0);
|
232 |
cv::undistortPoints(qc1[i], qc1i, cal.K1, cal.k1);
|
242 |
cv::undistortPoints(qc1[i], qc1i, cal.K1, cal.k1);
|
233 |
// qc0i = qc0[i];
|
243 |
// qc0i = qc0[i];
|
234 |
// qc1i = qc1[i];
|
244 |
// qc1i = qc1[i];
|
235 |
|
245 |
|
236 |
cv::Mat Qhom, Qcami;
|
246 |
cv::Mat Qhom, Qcami;
|
237 |
cv::triangulatePoints(P0, P1, qc0i, qc1i, Qhom);
|
247 |
cv::triangulatePoints(P0, P1, qc0i, qc1i, Qhom);
|
238 |
cvtools::convertMatFromHomogeneous(Qhom, Qcami);
|
248 |
cvtools::convertMatFromHomogeneous(Qhom, Qcami);
|
239 |
std::vector<cv::Point3f> QcamiPoints;
|
249 |
std::vector<cv::Point3f> QcamiPoints;
|
240 |
cvtools::matToPoints3f(Qcami, QcamiPoints);
|
250 |
cvtools::matToPoints3f(Qcami, QcamiPoints);
|
241 |
|
251 |
|
242 |
Qcam.push_back(QcamiPoints);
|
252 |
Qcam.push_back(QcamiPoints);
|
243 |
}
|
253 |
}
|
244 |
|
254 |
|
245 |
cv::Vec3f axis, point;
|
255 |
cv::Vec3f axis, point;
|
246 |
cvtools::rotationAxisCalibration(Qcam, Qi, axis, point);
|
256 |
cvtools::rotationAxisCalibration(Qcam, Qi, axis, point);
|
247 |
|
257 |
|
248 |
// construct transformation matrix
|
258 |
// construct transformation matrix
|
249 |
cv::Vec3f ex = axis.cross(cv::Vec3f(0,0,1.0));
|
259 |
cv::Vec3f ex = axis.cross(cv::Vec3f(0,0,1.0));
|
250 |
ex = cv::normalize(ex);
|
260 |
ex = cv::normalize(ex);
|
251 |
cv::Vec3f ez = ex.cross(axis);
|
261 |
cv::Vec3f ez = ex.cross(axis);
|
252 |
ez = cv::normalize(ez);
|
262 |
ez = cv::normalize(ez);
|
253 |
|
263 |
|
254 |
cv::Mat RrMat(3, 3, CV_32F);
|
264 |
cv::Mat RrMat(3, 3, CV_32F);
|
255 |
cv::Mat(ex).copyTo(RrMat.col(0));
|
265 |
cv::Mat(ex).copyTo(RrMat.col(0));
|
256 |
cv::Mat(axis).copyTo(RrMat.col(1));
|
266 |
cv::Mat(axis).copyTo(RrMat.col(1));
|
257 |
cv::Mat(ez).copyTo(RrMat.col(2));
|
267 |
cv::Mat(ez).copyTo(RrMat.col(2));
|
258 |
|
268 |
|
259 |
cal.Rr = cv::Matx33f(RrMat).t();
|
269 |
cal.Rr = cv::Matx33f(RrMat).t();
|
260 |
cal.Tr = -cv::Matx33f(RrMat).t()*point;
|
270 |
cal.Tr = -cv::Matx33f(RrMat).t()*point;
|
261 |
|
271 |
|
262 |
// Print to std::cout
|
272 |
// Print to std::cout
|
263 |
cal.print();
|
273 |
cal.print();
|
264 |
|
274 |
|
265 |
// save to (reentrant qsettings object)
|
275 |
// save to (reentrant qsettings object)
|
266 |
settings.setValue("calibration/parameters", QVariant::fromValue(cal));
|
276 |
settings.setValue("calibration/parameters", QVariant::fromValue(cal));
|
267 |
|
277 |
|
268 |
emit done();
|
278 |
emit done();
|
269 |
|
279 |
|
270 |
}
|
280 |
}
|
271 |
|
281 |
|