Subversion Repositories seema-scanner

Rev

Rev 88 | Rev 111 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 88 Rev 91
Line 117... Line 117...
117
    cal.R1 = R1;
117
    cal.R1 = R1;
118
    cal.T1 = T1;
118
    cal.T1 = T1;
119
    cal.E = E;
119
    cal.E = E;
120
    cal.F = F;
120
    cal.F = F;
121
 
121
 
122
    // calibrate rotation axis 1
122
//    // hand-eye calibration
123
    std::vector<cv::Matx33f> Rc(nValidSets - 1); // rotations/translations of the checkerboard in camera 0 reference frame
123
//    std::vector<cv::Matx33f> Rc(nValidSets - 1); // rotations/translations of the checkerboard in camera 0 reference frame
124
    std::vector<cv::Vec3f> Tc(nValidSets - 1);
124
//    std::vector<cv::Vec3f> Tc(nValidSets - 1);
125
    std::vector<cv::Matx33f> Rr(nValidSets - 1); // in rotation stage reference frame
125
//    std::vector<cv::Matx33f> Rr(nValidSets - 1); // in rotation stage reference frame
126
    std::vector<cv::Vec3f> Tr(nValidSets - 1);
126
//    std::vector<cv::Vec3f> Tr(nValidSets - 1);
127
    for(int i=0; i<nValidSets-1; i++){
127
//    for(int i=0; i<nValidSets-1; i++){
128
        // relative transformations in camera
128
//        // relative transformations in camera
129
        cv::Mat cRw1, cRw2;
129
//        cv::Mat cRw1, cRw2;
130
        cv::Rodrigues(cam_rvecs0[i], cRw1);
130
//        cv::Rodrigues(cam_rvecs0[i], cRw1);
131
        cv::Rodrigues(cam_rvecs0[i+1], cRw2);
131
//        cv::Rodrigues(cam_rvecs0[i+1], cRw2);
132
        cv::Mat cTw1 = cam_tvecs0[i];
132
//        cv::Mat cTw1 = cam_tvecs0[i];
133
        cv::Mat cTw2 = cam_tvecs0[i+1];
133
//        cv::Mat cTw2 = cam_tvecs0[i+1];
134
        cv::Mat w1Rc = cRw1.t();
134
//        cv::Mat w1Rc = cRw1.t();
135
        cv::Mat w1Tc = -cRw1.t()*cTw1;
135
//        cv::Mat w1Tc = -cRw1.t()*cTw1;
136
        Rc[i] = cv::Mat(cRw2*w1Rc);
136
//        Rc[i] = cv::Mat(cRw2*w1Rc);
137
        Tc[i] = cv::Mat(cRw2*w1Tc+cTw2);
137
//        Tc[i] = cv::Mat(cRw2*w1Tc+cTw2);
138
 
138
 
139
        // relative transformations in rotation stage
139
//        // relative transformations in rotation stage
140
        // we define the rotation axis to be in origo, pointing in positive y direction
140
//        // we define the rotation axis to be in origo, pointing in positive y direction
141
        float angleRadians = (angles[i+1]-angles[i])/180.0*M_PI;
141
//        float angleRadians = (angles[i+1]-angles[i])/180.0*M_PI;
142
        cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
142
//        cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
143
        cv::Mat Rri;
143
//        cv::Mat Rri;
144
        cv::Rodrigues(rot_rvec, Rri);
144
//        cv::Rodrigues(rot_rvec, Rri);
145
        Rr[i] = Rri;
145
//        Rr[i] = Rri;
146
        Tr[i] = 0.0;
146
//        Tr[i] = 0.0;
-
 
147
 
-
 
148
////        std::cout << i << std::endl;
-
 
149
////        std::cout << "cTw1" << cTw1 << std::endl;
-
 
150
////        std::cout << "cTw2" << cTw2 << std::endl;
-
 
151
////        std::cout << "w2Rc" << w2Rc << std::endl;
-
 
152
////        std::cout << "w2Tc" << w2Tc << std::endl;
-
 
153
 
-
 
154
////        std::cout << "w2Rc" << w2Rc << std::endl;
-
 
155
////        std::cout << "w2Tc" << w2Tc << std::endl;
-
 
156
 
-
 
157
////        cv::Mat Rci;
-
 
158
////        cv::Rodrigues(Rc[i], Rci);
-
 
159
////        std::cout << "Rci" << Rci << std::endl;
-
 
160
////        std::cout << "Tc[i]" << Tc[i] << std::endl;
-
 
161
 
-
 
162
////        std::cout << "rot_rvec" << rot_rvec << std::endl;
-
 
163
////        std::cout << "Tr[i]" << Tr[i] << std::endl;
-
 
164
////        std::cout << std::endl;
-
 
165
//    }
147
 
166
 
148
//        std::cout << i << std::endl;
-
 
149
//        std::cout << "cTw1" << cTw1 << std::endl;
-
 
150
//        std::cout << "cTw2" << cTw2 << std::endl;
167
//    // determine the transformation from rotation stage to camera 0
151
//        std::cout << "w2Rc" << w2Rc << std::endl;
168
//    cvtools::handEyeCalibrationTsai(Rc, Tc, Rr, Tr, cal.Rr, cal.Tr);
152
//        std::cout << "w2Tc" << w2Tc << std::endl;
-
 
153
 
169
 
154
//        std::cout << "w2Rc" << w2Rc << std::endl;
170
//    for(int i=0; i<nValidSets-1; i++){
155
//        std::cout << "w2Tc" << w2Tc << std::endl;
171
//        std::cout << i << std::endl;
156
 
172
 
157
//        cv::Mat Rci;
173
//        cv::Mat Rci;
158
//        cv::Rodrigues(Rc[i], Rci);
174
//        cv::Rodrigues(Rc[i], Rci);
159
//        std::cout << "Rci" << Rci << std::endl;
175
//        std::cout << "Rc[i]" << Rci << std::endl;
160
//        std::cout << "Tc[i]" << Tc[i] << std::endl;
176
//        std::cout << "Tc[i]" << Tc[i] << std::endl;
161
 
177
 
-
 
178
//        cv::Mat Rri;
-
 
179
//        cv::Rodrigues(Rr[i], Rri);
162
//        std::cout << "rot_rvec" << rot_rvec << std::endl;
180
//        std::cout << "Rr[i]" << Rri << std::endl;
163
//        std::cout << "Tr[i]" << Tr[i] << std::endl;
181
//        std::cout << "Tr[i]" << Tr[i] << std::endl;
164
//        std::cout << std::endl;
-
 
165
    }
-
 
166
 
-
 
167
    // determine the transformation from rotation stage to camera 0
-
 
168
    cvtools::handEyeCalibrationTsai(Rc, Tc, Rr, Tr, cal.Rr, cal.Tr);
-
 
169
 
182
 
-
 
183
//        cv::Mat Rcr = cv::Mat(cal.Rr)*cv::Mat(Rc[i])*cv::Mat(cal.Rr.t());
170
    for(int i=0; i<nValidSets-1; i++){
184
//        cv::Rodrigues(Rcr, Rcr);
-
 
185
//        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);
-
 
186
//        std::cout << "Rcr[i]" << Rcr << std::endl;
-
 
187
//        std::cout << "Tcr[i]" << Tcr << std::endl;
171
        std::cout << i << std::endl;
188
//        std::cout << std::endl;
-
 
189
//    }
172
 
190
 
173
        cv::Mat Rci;
-
 
174
        cv::Rodrigues(Rc[i], Rci);
-
 
175
        std::cout << "Rc[i]" << Rci << std::endl;
-
 
176
        std::cout << "Tc[i]" << Tc[i] << std::endl;
-
 
177
 
-
 
178
        cv::Mat Rri;
-
 
179
        cv::Rodrigues(Rr[i], Rri);
-
 
180
        std::cout << "Rr[i]" << Rri << std::endl;
-
 
181
        std::cout << "Tr[i]" << Tr[i] << std::endl;
-
 
182
 
-
 
183
        cv::Mat Rcr = cv::Mat(cal.Rr)*cv::Mat(Rc[i])*cv::Mat(cal.Rr.t());
-
 
184
        cv::Rodrigues(Rcr, Rcr);
-
 
185
        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);
-
 
186
        std::cout << "Rcr[i]" << Rcr << std::endl;
-
 
187
        std::cout << "Tcr[i]" << Tcr << std::endl;
-
 
188
        std::cout << std::endl;
-
 
189
    }
-
 
190
std::cout << "Rr: " << cal.Rr << std::endl;
-
 
191
std::cout << "Tr: " << cal.Tr << std::endl;
-
 
192
 
191
 
193
//    //* Using rotation axis calibration *//
192
    // Direct rotation axis calibration //
194
//    // full camera matrices
193
    // full camera matrices
195
//    cv::Matx34f P0 = cal.K0*cv::Matx34f::eye();
194
    cv::Matx34f P0 = cv::Matx34f::eye();
196
//    cv::Mat RT1(3, 4, CV_32F);
195
    cv::Mat RT1(3, 4, CV_32F);
197
//    cv::Mat(cal.R1).copyTo(RT1(cv::Range(0, 3), cv::Range(0, 3)));
196
    cv::Mat(cal.R1).copyTo(RT1(cv::Range(0, 3), cv::Range(0, 3)));
198
//    cv::Mat(cal.T1).copyTo(RT1(cv::Range(0, 3), cv::Range(3, 4)));
197
    cv::Mat(cal.T1).copyTo(RT1(cv::Range(0, 3), cv::Range(3, 4)));
199
//    cv::Matx34f P1 = cal.K1*cv::Matx34f(RT1);
198
    cv::Matx34f P1 = cv::Matx34f(RT1);
200
 
199
 
201
//    // calibration points in camera 0 frame
200
    // calibration points in camera 0 frame
202
//    std::vector< std::vector<cv::Point3f> > Qcam;
201
    std::vector< std::vector<cv::Point3f> > Qcam;
203
 
202
 
204
//    for(int i=0; i<nValidSets; i++){
203
    for(int i=0; i<nValidSets; i++){
205
//        std::vector<cv::Point2f> qc0i, qc1i;
204
        std::vector<cv::Point2f> qc0i, qc1i;
206
 
205
 
207
////        cv::undistortPoints(qc0[i], qc0i, cal.K0, cal.k0);
206
        cv::undistortPoints(qc0[i], qc0i, cal.K0, cal.k0);
208
////        cv::undistortPoints(qc1[i], qc1i, cal.K1, cal.k1);
207
        cv::undistortPoints(qc1[i], qc1i, cal.K1, cal.k1);
209
//        qc0i = qc0[i];
208
//        qc0i = qc0[i];
210
//        qc1i = qc1[i];
209
//        qc1i = qc1[i];
211
 
210
 
212
//        cv::Mat Qhom, Qcami;
211
        cv::Mat Qhom, Qcami;
213
//        cv::triangulatePoints(P0, P1, qc0i, qc1i, Qhom);
212
        cv::triangulatePoints(P0, P1, qc0i, qc1i, Qhom);
214
//        cvtools::convertMatFromHomogeneous(Qhom, Qcami);
213
        cvtools::convertMatFromHomogeneous(Qhom, Qcami);
215
//        std::vector<cv::Point3f> QcamiPoints;
214
        std::vector<cv::Point3f> QcamiPoints;
216
//        cvtools::matToPoints3f(Qcami, QcamiPoints);
215
        cvtools::matToPoints3f(Qcami, QcamiPoints);
217
 
216
 
218
//        Qcam.push_back(QcamiPoints);
217
        Qcam.push_back(QcamiPoints);
219
//    }
218
    }
220
 
219
 
221
//    cv::Vec3f axis, point;
220
    cv::Vec3f axis, point;
222
//    cvtools::rotationAxisCalibration(Qcam, Qi, axis, point);
221
    cvtools::rotationAxisCalibration(Qcam, Qi, axis, point);
223
 
222
 
224
//    // construct transformation matrix
223
    // construct transformation matrix
225
//    cv::Vec3f ex = axis.cross(cv::Vec3f(0,0,-1.0));
224
    cv::Vec3f ex = axis.cross(cv::Vec3f(0,0,1.0));
-
 
225
    ex = cv::normalize(ex);
226
//    cv::Vec3f ez = ex.cross(axis);
226
    cv::Vec3f ez = ex.cross(axis);
-
 
227
    ez = cv::normalize(ez);
227
 
228
 
228
//    cv::Mat RrMat(3, 3, CV_32F);
229
    cv::Mat RrMat(3, 3, CV_32F);
229
//    cv::Mat(ex).copyTo(RrMat.col(0));
230
    cv::Mat(ex).copyTo(RrMat.col(0));
230
//    cv::Mat(axis).copyTo(RrMat.col(1));
231
    cv::Mat(axis).copyTo(RrMat.col(1));
231
//    cv::Mat(ez).copyTo(RrMat.col(2));
232
    cv::Mat(ez).copyTo(RrMat.col(2));
232
 
233
 
233
//    cal.Rr = cv::Mat(RrMat.t());
234
    cal.Rr = cv::Matx33f(RrMat).t();
234
//    cal.Tr = point;
235
    cal.Tr = -cv::Matx33f(RrMat).t()*point;
235
 
236
 
236
    // Print to std::cout
237
    // Print to std::cout
237
    cal.print();
238
    cal.print();
238
 
239
 
239
    // save to (reentrant qsettings object)
240
    // save to (reentrant qsettings object)