Subversion Repositories seema-scanner

Rev

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

Rev 206 Rev 207
Line 7... Line 7...
7
 
7
 
8
#include <ceres/ceres.h>
8
#include <ceres/ceres.h>
9
 
9
 
10
 
10
 
11
struct CircleResidual {
11
struct CircleResidual {
12
  CircleResidual(std::vector<cv::Point3f> _pointsOnArc)
12
    CircleResidual(std::vector<cv::Point3f> _pointsOnArc)
13
      : pointsOnArc(_pointsOnArc) {}
13
        : pointsOnArc(_pointsOnArc) {}
14
 
14
 
15
  template <typename T>
15
    template <typename T>
16
  bool operator()(const T* point, const T* axis, T* residual) const {
16
    bool operator()(const T* point, const T* axis, T* residual) const {
17
 
17
 
18
    T axisSqNorm = axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2];
18
        T axisSqNorm = axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2];
19
 
19
 
20
    unsigned int l = pointsOnArc.size();
20
        unsigned int l = pointsOnArc.size();
21
    std::vector<T> dI(l);
21
        std::vector<T> dI(l);
22
 
22
 
23
    // note, this is automatically initialized to 0
23
        // note, this is automatically initialized to 0
24
    T sum(0.0);
24
        T sum(0.0);
25
 
25
 
26
    for(unsigned int i=0; i<l; i++){
26
        for(unsigned int i=0; i<l; i++){
27
      cv::Point3d p = pointsOnArc[i];
27
            cv::Point3d p = pointsOnArc[i];
28
      //T p[3] = {pointsOnArc[i].x, pointsOnArc[i].y, pointsOnArc[i].z};
28
            //T p[3] = {pointsOnArc[i].x, pointsOnArc[i].y, pointsOnArc[i].z};
29
 
29
 
30
      // point to line distance
30
            // point to line distance
31
      T dotProd = (point[0]-p.x)*axis[0] + (point[1]-p.y)*axis[1] + (point[2]-p.z)*axis[2];
31
            T dotProd = (point[0]-p.x)*axis[0] + (point[1]-p.y)*axis[1] + (point[2]-p.z)*axis[2];
32
      T dIx = point[0] - p.x - (dotProd*axis[0]/axisSqNorm);
32
            T dIx = point[0] - p.x - (dotProd*axis[0]/axisSqNorm);
33
      T dIy = point[1] - p.y - (dotProd*axis[1]/axisSqNorm);
33
            T dIy = point[1] - p.y - (dotProd*axis[1]/axisSqNorm);
34
      T dIz = point[2] - p.z - (dotProd*axis[2]/axisSqNorm);
34
            T dIz = point[2] - p.z - (dotProd*axis[2]/axisSqNorm);
35
      dI[i] = ceres::sqrt(dIx*dIx + dIy*dIy + dIz*dIz);
35
            dI[i] = ceres::sqrt(dIx*dIx + dIy*dIy + dIz*dIz);
36
 
36
 
37
      sum += dI[i];
37
            sum += dI[i];
38
    }
38
        }
39
 
39
 
40
    T mean = sum / double(l);
40
        T mean = sum / double(l);
41
 
41
 
42
    for(unsigned int i=0; i<l; i++){
42
        for(unsigned int i=0; i<l; i++){
43
        residual[i] = dI[i] - mean;
43
            residual[i] = dI[i] - mean;
44
    }
44
        }
45
 
45
 
46
    return true;
46
        return true;
47
  }
47
    }
48
 
48
 
49
 private:
49
private:
50
 
50
 
51
  // Observations for one checkerboard corner.
51
    // Observations for one checkerboard corner.
52
  const std::vector<cv::Point3f> pointsOnArc;
52
    const std::vector<cv::Point3f> pointsOnArc;
53
};
53
};
54
 
54
 
55
 
55
 
56
// Closed form solution to solve for the rotation axis from sets of 3D point coordinates of flat pattern feature points
56
// Closed form solution to solve for the rotation axis from sets of 3D point coordinates of flat pattern feature points
57
// Algorithm according to Chen et al., Rotation axis calibration of a turntable using constrained global optimization, Optik 2014
57
// Algorithm according to Chen et al., Rotation axis calibration of a turntable using constrained global optimization, Optik 2014
58
// DTU, 2014, Jakob Wilm
58
// DTU, 2014, Jakob Wilm
59
static void rotationAxisCalibration(const std::vector< std::vector<cv::Point3f> > Qcam,
59
static void rotationAxisCalibration(const std::vector< std::vector<cv::Point3f> > Qcam,
60
                                    const std::vector<cv::Point3f> Qobj,
60
                                    const std::vector<cv::Point3f> Qobj,
61
                                    cv::Vec3f &axis, cv::Vec3f &point, float &error){
61
                                    cv::Vec3f &axis, cv::Vec3f &point, float &error){
-
 
62
    assert(Qobj.size() == Qcam[0].size());
62
 
63
 
63
    // number of frames (points on each arch)
64
    // number of frames (points on each arch)
64
    int l = Qcam.size();
65
    int l = Qcam.size();
65
 
66
 
66
    // number of points in each frame
67
    // number of points in each frame
67
    size_t mn = Qobj.size();
68
    size_t mn = Qobj.size();
68
 
69
 
69
    assert(mn == Qcam[0].size());
-
 
70
 
-
 
71
    // construct matrix for axis determination
70
    // construct matrix for axis determination
72
    cv::Mat M(6, 6, CV_32F, cv::Scalar(0));
71
    cv::Mat M(6, 6, CV_32F, cv::Scalar(0));
73
 
72
 
74
    for(int k=0; k<l; k++){
73
    for(int k=0; k<l; k++){
75
        for(unsigned int idx=0; idx<mn; idx++){
74
        for(unsigned int idx=0; idx<mn; idx++){
76
 
75
 
77
//            float i = Qobj[idx].x+4;
76
            //            float i = Qobj[idx].x+4;
78
//            float j = Qobj[idx].y+4;
77
            //            float j = Qobj[idx].y+4;
79
            float i = Qobj[idx].x;
78
            float i = Qobj[idx].x;
80
            float j = Qobj[idx].y;
79
            float j = Qobj[idx].y;
81
 
80
 
82
            float x = Qcam[k][idx].x;
81
            float x = Qcam[k][idx].x;
83
            float y = Qcam[k][idx].y;
82
            float y = Qcam[k][idx].y;
84
            float z = Qcam[k][idx].z;
83
            float z = Qcam[k][idx].z;
85
 
84
 
86
            M += (cv::Mat_<float>(6,6) << x*x, x*y, x*z, x, i*x, j*x,
85
            M += (cv::Mat_<float>(6,6) << x*x, x*y, x*z, x, i*x, j*x,
87
                                            0, y*y, y*z, y, i*y, j*y,
86
                  0, y*y, y*z, y, i*y, j*y,
88
                                            0,   0, z*z, z, i*z, j*z,
87
                  0,   0, z*z, z, i*z, j*z,
89
                                            0,   0,   0, 1,   i,   j,
88
                  0,   0,   0, 1,   i,   j,
90
                                            0,   0,   0, 0, i*i, i*j,
89
                  0,   0,   0, 0, i*i, i*j,
91
                                            0,   0,   0, 0,   0, j*j);
90
                  0,   0,   0, 0,   0, j*j);
92
 
91
 
93
        }
92
        }
94
    }
93
    }
95
 
94
 
96
    cv::completeSymm(M, false);
95
    cv::completeSymm(M, false);
Line 117... Line 116...
117
    float nz = u.at<float>(idx, 2);
116
    float nz = u.at<float>(idx, 2);
118
    //float d  = u.at<float>(idx, 3);
117
    //float d  = u.at<float>(idx, 3);
119
    float dh = u.at<float>(idx, 4);
118
    float dh = u.at<float>(idx, 4);
120
    float dv = u.at<float>(idx, 5);
119
    float dv = u.at<float>(idx, 5);
121
 
120
 
122
//    // Paper version: c is initially eliminated
121
    // Paper version: c is initially eliminated
123
//    cv::Mat A(l*mn, mn+2, CV_32F, cv::Scalar(0.0));
122
    /*cv::Mat A(l*mn, mn+2, CV_32F, cv::Scalar(0.0));
124
//    cv::Mat bb(l*mn, 1, CV_32F);
123
        cv::Mat bb(l*mn, 1, CV_32F);
125
 
124
 
126
//    for(int k=0; k<l; k++){
125
        for(int k=0; k<l; k++){
127
//        for(unsigned int idx=0; idx<mn; idx++){
126
            for(unsigned int idx=0; idx<mn; idx++){
128
 
127
 
129
//            float i = Qobj[idx].x;
128
                float i = Qobj[idx].x;
130
//            float j = Qobj[idx].y;
129
                float j = Qobj[idx].y;
131
 
130
 
132
//            float x = Qcam[k][idx].x;
131
                float x = Qcam[k][idx].x;
133
//            float y = Qcam[k][idx].y;
132
                float y = Qcam[k][idx].y;
134
//            float z = Qcam[k][idx].z;
133
                float z = Qcam[k][idx].z;
135
 
134
 
136
//            float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
135
                float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
137
 
136
 
138
//            int row = k*mn+idx;
137
                int row = k*mn+idx;
139
//            A.at<float>(row, 0) = 2*x - (2*z*nx)/nz;
138
                A.at<float>(row, 0) = 2*x - (2*z*nx)/nz;
140
//            A.at<float>(row, 1) = 2*y - (2*z*ny)/nz;
139
                A.at<float>(row, 1) = 2*y - (2*z*ny)/nz;
141
//            A.at<float>(row, idx+2) = 1.0;
140
                A.at<float>(row, idx+2) = 1.0;
142
 
141
 
143
//            bb.at<float>(row, 0) = f + (2*z*d)/nz;
142
                bb.at<float>(row, 0) = f + (2*z*d)/nz;
144
//        }
143
            }
145
//    }
144
        }
146
 
145
 
147
//    // solve for point
146
        // solve for point
148
//    cv::Mat abe;
147
        cv::Mat abe;
149
//    cv::solve(A, bb, abe, cv::DECOMP_SVD);
148
        cv::solve(A, bb, abe, cv::DECOMP_SVD);
150
 
149
 
151
//    float a = abe.at<float>(0, 0);
150
        float a = abe.at<float>(0, 0);
152
//    float b = abe.at<float>(1, 0);
151
        float b = abe.at<float>(1, 0);
153
//    float c = -(nx*a+ny*b+d)/nz;
152
        float c = -(nx*a+ny*b+d)/nz;
-
 
153
        */
154
 
154
 
155
    // Our version: solve simultanously for a,b,c
155
    // Our version: solve simultanously for a,b,c
156
    cv::Mat A(l*mn, mn+3, CV_32F, cv::Scalar(0.0));
156
    cv::Mat A(l*mn, mn+3, CV_32F, cv::Scalar(0.0));
157
    cv::Mat bb(l*mn, 1, CV_32F);
157
    cv::Mat bb(l*mn, 1, CV_32F);
158
 
158
 
Line 200... Line 200...
200
        std::vector<cv::Point3f> pointsOnArch(l);
200
        std::vector<cv::Point3f> pointsOnArch(l);
201
        for(int k=0; k<l; k++){
201
        for(int k=0; k<l; k++){
202
            pointsOnArch[k] = Qcam[k][idx];
202
            pointsOnArch[k] = Qcam[k][idx];
203
        }
203
        }
204
        ceres::CostFunction* cost_function =
204
        ceres::CostFunction* cost_function =
205
           new ceres::AutoDiffCostFunction<CircleResidual, ceres::DYNAMIC, 3, 3>(
205
                new ceres::AutoDiffCostFunction<CircleResidual, ceres::DYNAMIC, 3, 3>(
206
               new CircleResidual(pointsOnArch), l);
206
                    new CircleResidual(pointsOnArch), l);
207
        problem.AddResidualBlock(cost_function, NULL, pointArray, axisArray);
207
        problem.AddResidualBlock(cost_function, NULL, pointArray, axisArray);
208
    }
208
    }
209
 
209
 
210
    // Run the solver!
210
    // Run the solver!
211
    ceres::Solver::Options options;
211
    ceres::Solver::Options options;
Line 260... Line 260...
260
    // Extract checker corners
260
    // Extract checker corners
261
    bool success = cv::findChessboardCorners(gray, checkerCount, qciX, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
261
    bool success = cv::findChessboardCorners(gray, checkerCount, qciX, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
262
    if(success){
262
    if(success){
263
        cv::cornerSubPix(gray, qciX, cv::Size(6, 6), cv::Size(1, 1), cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
263
        cv::cornerSubPix(gray, qciX, cv::Size(6, 6), cv::Size(1, 1), cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
264
        // Draw colored chessboard
264
        // Draw colored chessboard
265
        cv::Mat color;
-
 
266
        if(SMCalibrationSetI_frameX.channels() == 1)
265
        if(SMCalibrationSetI_frameX.channels() == 1)
267
            cv::cvtColor(SMCalibrationSetI_frameX, color, CV_BayerBG2RGB);
266
            cv::cvtColor(SMCalibrationSetI_frameX, SMCalibrationSetI_frameXResult, CV_BayerBG2RGB);
268
        else
267
        else
269
            color = SMCalibrationSetI_frameX.clone();
268
            SMCalibrationSetI_frameXResult = SMCalibrationSetI_frameX.clone();
270
 
269
 
271
        cvtools::drawChessboardCorners(color, checkerCount, qciX, success, 10);
270
        cvtools::drawChessboardCorners(SMCalibrationSetI_frameXResult, checkerCount, qciX, success, 10);
272
        SMCalibrationSetI_frameXResult = color;
-
 
273
    }
271
    }
274
    return success;
272
    return success;
275
}
273
}
276
 
274
 
277
void SMCalibrationWorker::performCalibration(std::vector<SMCalibrationSet> calibrationData){
275
std::vector<cv::Point3f> SMCalibrationWorker::generateObjCoordsInWorldCS(const cv::Size checkerCount, const float checkerSize)
-
 
276
{
-
 
277
    std::vector<cv::Point3f> Qi;
-
 
278
    for (int h=0; h<checkerCount.height; h++)
-
 
279
        for (int w=0; w<checkerCount.width; w++)
-
 
280
            Qi.push_back(cv::Point3f(checkerSize * w, checkerSize* h, 0.0));
-
 
281
 
-
 
282
    return Qi;
-
 
283
}
278
 
284
 
-
 
285
void SMCalibrationWorker::perViewReprojError(const std::vector<bool> &success0,
-
 
286
                                             const std::vector< std::vector<cv::Point3f> > &Q0,
-
 
287
                                             const std::vector<cv::Mat> &cam_tvecs0,
-
 
288
                                             const std::vector<cv::Mat> &cam_rvecs0,
-
 
289
                                             const std::vector< std::vector<cv::Point2f> > &qc0,
-
 
290
                                             const cv::Matx33f &K0,
-
 
291
                                             const cv::Vec<float, 5> &k0,
-
 
292
                                             std::vector<float> &camX_errors_per_view)
-
 
293
{
-
 
294
    unsigned int nSets = success0.size();
-
 
295
    camX_errors_per_view.resize(nSets);
279
    QSettings settings;
296
    int idx = 0;
-
 
297
    //#pragma omp parallel for
-
 
298
    for(unsigned int i = 0; i < nSets; ++i){
-
 
299
        if(success0[i]){
-
 
300
            int n = (int)Q0[idx].size();
-
 
301
            std::vector<cv::Point2f> qc_proj;
-
 
302
            cv::projectPoints(cv::Mat(Q0[idx]), cam_rvecs0[idx], cam_tvecs0[idx], K0,  k0, qc_proj);
-
 
303
            float err = 0;
-
 
304
            for(unsigned int j=0; j<qc_proj.size(); j++){
-
 
305
                cv::Point2f d = qc0[idx][j] - qc_proj[j];
-
 
306
                err += cv::sqrt(d.x*d.x + d.y*d.y);
-
 
307
            }
-
 
308
            camX_errors_per_view[i] = (float)err/n;
-
 
309
            idx++;
-
 
310
        } else
-
 
311
            camX_errors_per_view[i] = NAN;
-
 
312
    }
-
 
313
}
280
 
314
 
281
    // Number of saddle points on calibration pattern
-
 
282
    int checkerCountX = settings.value("calibration/patternSizeX", 22).toInt();
-
 
283
    int checkerCountY = settings.value("calibration/patternSizeY", 13).toInt();
315
void SMCalibrationWorker::performCameraCalibration(std::vector<SMCalibrationSet> calibrationData){
284
    cv::Size checkerCount(checkerCountX, checkerCountY);
-
 
285
 
316
 
-
 
317
    QSettings settings;
-
 
318
    cv::Size checkerCount(
-
 
319
                cv::Size(settings.value("calibration/patternSizeX", 22).toInt(),settings.value("calibration/patternSizeY", 13).toInt()));
286
    unsigned int nSets = calibrationData.size();
320
    unsigned int nSets = calibrationData.size();
287
 
321
 
288
    // 2D Points collected for OpenCV's calibration procedures
322
    // 2D Points collected for OpenCV's calibration procedures
289
    std::vector< std::vector<cv::Point2f> > qc0, qc1;
323
#define OLD_WAYZ 0
-
 
324
#if OLD_WAYZ
290
    std::vector< std::vector<cv::Point2f> > qc0Stereo, qc1Stereo;
325
    std::vector< std::vector<cv::Point2f> > qc0, qc1, qc0Stereo, qc1Stereo;
291
 
-
 
292
    std::vector<bool> success0(nSets), success1(nSets);
326
    std::vector<bool> fwdToStageEstimation;
293
 
-
 
294
    std::vector<float> angles;
327
    std::vector<float> angles;
-
 
328
    std::vector<unsigned int> TESTER;
295
 
329
#else
-
 
330
    std::vector< std::vector<cv::Point2f> > qc0(nSets), qc1(nSets), qc0Stereo(nSets), qc1Stereo(nSets);
-
 
331
    std::vector<bool> fwdToStageEstimation(nSets);
-
 
332
    std::vector<float> angles(nSets);
-
 
333
    std::vector<unsigned int> TESTER(nSets);
-
 
334
#endif
-
 
335
    std::vector<bool> success0(nSets, false), success1(nSets, false);
296
    // Loop through calibration sets
336
    // Loop through calibration sets
-
 
337
#if !OLD_WAYZ
-
 
338
#pragma omp parallel for
-
 
339
#endif
297
    for(unsigned int i=0; i<nSets; i++){
340
    for(unsigned int i=0; i<nSets; i++){
298
 
341
 
299
        SMCalibrationSet SMCalibrationSetI = calibrationData[i];
342
        SMCalibrationSet SMCalibrationSetI = calibrationData[i];
300
 
343
 
-
 
344
        // TODO this changes semantics of the checkboxes in the GUI
301
        if(!SMCalibrationSetI.checked)
345
        /*if(!SMCalibrationSetI.checked)
302
            continue;
346
            continue;*/
303
 
347
 
304
        // Camera 0
348
        // Camera 0
305
        std::vector<cv::Point2f> qci0;
349
        std::vector<cv::Point2f> qci0;
306
 
-
 
307
        // Convert to grayscale
-
 
308
        cv::Mat gray;
-
 
309
        if(SMCalibrationSetI.frame0.channels() == 1)
-
 
310
            cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_BayerBG2GRAY);
-
 
311
        else
-
 
312
            cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_RGB2GRAY);
-
 
313
 
-
 
314
        // Extract checker corners
-
 
315
        success0[i] = cv::findChessboardCorners(gray, checkerCount, qci0, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
350
        success0[i] = processCBCorners(checkerCount, SMCalibrationSetI.frame0, SMCalibrationSetI.frame0Result, qci0);
316
        if(success0[i]){
-
 
317
            cv::cornerSubPix(gray, qci0, cv::Size(6, 6), cv::Size(1, 1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
-
 
318
            // Draw colored chessboard
351
#pragma omp critical (CBCALCupdateUI1)
319
            cv::Mat color;
-
 
320
            if(SMCalibrationSetI.frame0.channels() == 1)
-
 
321
                cv::cvtColor(SMCalibrationSetI.frame0, color, CV_BayerBG2RGB);
-
 
322
            else
-
 
323
                color = SMCalibrationSetI.frame0.clone();
-
 
324
 
-
 
325
            cvtools::drawChessboardCorners(color, checkerCount, qci0, success0[i], 10);
-
 
326
            SMCalibrationSetI.frame0Result = color;
-
 
327
        }
-
 
328
 
352
{
329
        emit newFrameResult(i, 0, success0[i], SMCalibrationSetI.frame0Result);
353
        emit newFrameResult(i, 0, success0[i], SMCalibrationSetI.frame0Result);
330
 
354
}
331
        // Camera 1
355
        // Camera 1
332
        std::vector<cv::Point2f> qci1;
356
        std::vector<cv::Point2f> qci1;
333
 
-
 
334
        // Convert to grayscale
-
 
335
        if(SMCalibrationSetI.frame1.channels() == 1)
-
 
336
            cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_BayerBG2GRAY);
-
 
337
        else
-
 
338
            cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_RGB2GRAY);
-
 
339
 
-
 
340
        // Extract checker corners
-
 
341
        success1[i] = cv::findChessboardCorners(gray, checkerCount, qci1, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
357
        success1[i] = processCBCorners(checkerCount, SMCalibrationSetI.frame1, SMCalibrationSetI.frame1Result, qci1);
342
        if(success1[i]){
-
 
343
            cv::cornerSubPix(gray, qci1, cv::Size(6, 6), cv::Size(1, 1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
-
 
344
            // Draw colored chessboard
358
#pragma omp critical (CBCALCupdateUI2)
345
            cv::Mat color;
-
 
346
            if(SMCalibrationSetI.frame1.channels() == 1)
-
 
347
                cv::cvtColor(SMCalibrationSetI.frame1, color, CV_BayerBG2RGB);
-
 
348
            else
-
 
349
                color = SMCalibrationSetI.frame1.clone();
-
 
350
 
-
 
351
            cvtools::drawChessboardCorners(color, checkerCount, qci1, success1[i], 10);
-
 
352
            SMCalibrationSetI.frame1Result = color;
-
 
353
        }
-
 
354
 
359
{
355
        emit newFrameResult(i, 1, success1[i], SMCalibrationSetI.frame1Result);
360
        emit newFrameResult(i, 1, success1[i], SMCalibrationSetI.frame1Result);
356
 
361
}
-
 
362
        // store results
-
 
363
#if OLD_WAYZ
357
        if(success0[i])
364
        if(success0[i])
358
            qc0.push_back(qci0);
365
            qc0.push_back(qci0);
359
 
366
 
360
        if(success1[i])
367
        if(success1[i])
361
            qc1.push_back(qci1);
368
            qc1.push_back(qci1);
362
 
369
 
363
        if(success0[i] && success1[i]){
370
        if(success0[i] && success1[i]){
364
            qc0Stereo.push_back(qci0);
371
            qc0Stereo.push_back(qci0);
365
            qc1Stereo.push_back(qci1);
372
            qc1Stereo.push_back(qci1);
366
            angles.push_back(SMCalibrationSetI.rotationAngle);
373
            angles.push_back(SMCalibrationSetI.rotationAngle);
-
 
374
            fwdToStageEstimation.push_back(SMCalibrationSetI.checked);
-
 
375
            TESTER.push_back(i);
367
        }
376
        }
-
 
377
#else
-
 
378
        qc0[i] = qci0;
-
 
379
        qc1[i] = qci1;
368
 
380
 
-
 
381
        qc0Stereo[i] = qci0;
-
 
382
        qc1Stereo[i] = qci1;
-
 
383
        angles[i] = SMCalibrationSetI.rotationAngle;
-
 
384
        fwdToStageEstimation[i] = SMCalibrationSetI.checked;
-
 
385
        TESTER[i] = (i);
-
 
386
#endif
369
        // Show progress
387
        // Show progress
-
 
388
        #pragma omp critical (CBCALCupdateUI3)
-
 
389
        {
370
        emit newSetProcessed(i);
390
            emit newSetProcessed(i);
-
 
391
        }
371
    }
392
    }
-
 
393
#if !OLD_WAYZ
-
 
394
    auto s0it = success0.cbegin();
-
 
395
    qc0.erase(std::remove_if( qc0.begin(), qc0.end(), [&s0it](std::vector<cv::Point2f>){return !*s0it++;}),
-
 
396
            qc0.end());
372
 
397
 
-
 
398
    auto s1it = success1.cbegin();
-
 
399
    qc1.erase(std::remove_if( qc1.begin(), qc1.end(), [&s1it](std::vector<cv::Point2f>){return !*s1it++;}),
-
 
400
            qc1.end());
-
 
401
 
-
 
402
    s0it = success0.cbegin(); s1it = success1.cbegin();
-
 
403
    qc0Stereo.erase(std::remove_if( qc0Stereo.begin(), qc0Stereo.end(), [&s0it,&s1it](std::vector<cv::Point2f>){return !((*s0it++)&(*s1it++));}),
-
 
404
            qc0Stereo.end());
-
 
405
 
-
 
406
    s0it = success0.cbegin(); s1it = success1.cbegin();
-
 
407
    qc1Stereo.erase(std::remove_if( qc1Stereo.begin(), qc1Stereo.end(), [&s0it,&s1it](std::vector<cv::Point2f>){return !((*s0it++)&(*s1it++));}),
-
 
408
            qc1Stereo.end());
-
 
409
 
-
 
410
    s0it = success0.cbegin(); s1it = success1.cbegin();
-
 
411
    angles.erase(std::remove_if( angles.begin(), angles.end(), [&s0it,&s1it](float){return !((*s0it++)&(*s1it++));}),
-
 
412
            angles.end());
-
 
413
 
-
 
414
    s0it = success0.cbegin(); s1it = success1.cbegin();
-
 
415
    fwdToStageEstimation.erase(std::remove_if( fwdToStageEstimation.begin(), fwdToStageEstimation.end(), [&s0it,&s1it](bool){return !((*s0it++)&(*s1it++));}),
-
 
416
            fwdToStageEstimation.end());
-
 
417
 
-
 
418
    s0it = success0.cbegin(); s1it = success1.cbegin();
-
 
419
    TESTER.erase(std::remove_if( TESTER.begin(), TESTER.end(), [&s0it,&s1it](bool){return !((*s0it++)&(*s1it++));}),
-
 
420
            TESTER.end());
-
 
421
    /*for(unsigned int i=0; i<nSets; i++){
-
 
422
        if(~success0[i])
-
 
423
            std::remove(qc0.begin(),qc0.end(),i);
-
 
424
        if(~success1[i])
-
 
425
            std::remove(qc1.begin(),qc1.end(),i);
-
 
426
        if(~success0[i] || ~success1[i]){
-
 
427
            std::remove(qc0Stereo.begin(),qc0Stereo.end(),i);
-
 
428
            std::remove(qc1Stereo.begin(),qc1Stereo.end(),i);
-
 
429
            std::remove(angles.begin(),angles.end(),i);
-
 
430
            std::remove(fwdToStageEstimation.begin(),fwdToStageEstimation.end(),i);
-
 
431
        }
-
 
432
    }*/
-
 
433
#endif
-
 
434
    std::copy(TESTER.begin(), TESTER.end(), std::ostream_iterator<unsigned int>(std::cout, " "));
-
 
435
    std::cout << std::endl;
373
    size_t nValidSets = angles.size();
436
    const size_t nValidStereoSets = angles.size();
374
    if(nValidSets < 2){
437
    if(nValidStereoSets < 2){
375
        std::cerr << "Not enough valid calibration sequences!" << std::endl;
438
        std::cerr << "Not enough valid calibration sequences!" << std::endl;
376
        emit done();
439
        emit done();
377
        return;
440
        return;
378
    }
441
    }
379
 
442
 
380
    // Generate world object coordinates [mm]
443
    // Generate world object coordinates [mm]
381
    float checkerSize = settings.value("calibration/squareSize", 10.0).toFloat(); // width and height of one checker square in mm
-
 
382
    std::vector<cv::Point3f> Qi;
444
    std::vector<cv::Point3f> Qi = generateObjCoordsInWorldCS(checkerCount,
383
    for (int h=0; h<checkerCount.height; h++)
-
 
384
        for (int w=0; w<checkerCount.width; w++)
-
 
385
            Qi.push_back(cv::Point3f(checkerSize * w, checkerSize* h, 0.0));
445
                                                             settings.value("calibration/squareSize", 10.0).toFloat());
386
 
446
 
387
    std::vector< std::vector<cv::Point3f> > Q0, Q1, QStereo;
447
    std::vector< std::vector<cv::Point3f> >
388
    for(unsigned int i=0; i<qc0.size(); i++)
-
 
389
        Q0.push_back(Qi);
448
            Q0(qc0.size(), Qi),
390
    for(unsigned int i=0; i<qc1.size(); i++)
-
 
391
        Q1.push_back(Qi);
449
            Q1(qc1.size(), Qi),
392
    for(unsigned int i=0; i<nValidSets; i++)
-
 
393
        QStereo.push_back(Qi);
450
            QStereo(nValidStereoSets, Qi);
394
 
451
 
395
    // calibrate the cameras
452
    // calibrate the cameras
396
    SMCalibrationParameters cal;
453
    SMCalibrationParameters cal;
397
    cal.frameWidth = calibrationData[0].frame0.cols;
454
    cal.frameWidth = calibrationData[0].frame0.cols;
398
    cal.frameHeight = calibrationData[0].frame0.rows;
455
    cal.frameHeight = calibrationData[0].frame0.rows;
Line 402... Line 459...
402
    int flags = cv::CALIB_FIX_ASPECT_RATIO + cv::CALIB_FIX_K3 + cv::CALIB_ZERO_TANGENT_DIST + cv::CALIB_FIX_PRINCIPAL_POINT;
459
    int flags = cv::CALIB_FIX_ASPECT_RATIO + cv::CALIB_FIX_K3 + cv::CALIB_ZERO_TANGENT_DIST + cv::CALIB_FIX_PRINCIPAL_POINT;
403
    // Note: several of the output arguments below must be cv::Mat, otherwise segfault
460
    // Note: several of the output arguments below must be cv::Mat, otherwise segfault
404
    std::vector<cv::Mat> cam_rvecs0, cam_tvecs0;
461
    std::vector<cv::Mat> cam_rvecs0, cam_tvecs0;
405
    cal.cam0_error = cv::calibrateCamera(Q0, qc0, frameSize, cal.K0, cal.k0, cam_rvecs0, cam_tvecs0, flags,
462
    cal.cam0_error = cv::calibrateCamera(Q0, qc0, frameSize, cal.K0, cal.k0, cam_rvecs0, cam_tvecs0, flags,
406
                                         cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
463
                                         cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
407
//std::cout << cal.k0 << std::endl;
464
    //std::cout << cal.k0 << std::endl;
408
//    // refine extrinsics for camera 0
465
    //    // refine extrinsics for camera 0
409
//    for(int i=0; i<Q.size(); i++)
466
    //    for(int i=0; i<Q.size(); i++)
410
//        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);
467
    //        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);
411
 
468
 
412
    std::vector<cv::Mat> cam_rvecs1, cam_tvecs1;
469
    std::vector<cv::Mat> cam_rvecs1, cam_tvecs1;
413
    cal.cam1_error = cv::calibrateCamera(Q1, qc1, frameSize, cal.K1, cal.k1, cam_rvecs1, cam_tvecs1, flags,
470
    cal.cam1_error = cv::calibrateCamera(Q1, qc1, frameSize, cal.K1, cal.k1, cam_rvecs1, cam_tvecs1, flags,
414
                                         cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
471
                                         cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
415
//std::cout << cal.k1 << std::endl;
472
    //std::cout << cal.k1 << std::endl;
416
    // stereo calibration
473
    // stereo calibration
417
    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;
474
    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;
418
    cv::Mat E, F, R1, T1;
475
    cv::Mat E, F, R1, T1;
-
 
476
 
-
 
477
#if CV_MAJOR_VERSION < 3
419
    cal.stereo_error = cv::stereoCalibrate(QStereo, qc0Stereo, qc1Stereo, cal.K0, cal.k0, cal.K1, cal.k1,
478
        cal.stereo_error = cv::stereoCalibrate(QStereo, qc0Stereo, qc1Stereo, cal.K0, cal.k0, cal.K1, cal.k1,
-
 
479
                                               frameSize, R1, T1, E, F,
-
 
480
                                               cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 200, DBL_EPSILON),
-
 
481
                                               flags_stereo);
-
 
482
#else
-
 
483
        cal.stereo_error = cv::stereoCalibrate(QStereo, qc0Stereo, qc1Stereo, cal.K0, cal.k0, cal.K1, cal.k1,
420
                                              frameSize, R1, T1, E, F, flags_stereo,
484
                                               frameSize, R1, T1, E, F,flags_stereo,
421
                                              cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 200, DBL_EPSILON));
485
                                               cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 200, DBL_EPSILON));
-
 
486
#endif
422
 
487
 
423
    cal.R1 = R1;
488
    cal.R1 = R1;
424
    cal.T1 = T1;
489
    cal.T1 = T1;
425
    cal.E = E;
490
    cal.E = E;
426
    cal.F = F;
491
    cal.F = F;
427
 
492
 
428
    // Determine per-view reprojection errors:
493
    // Determine per-view reprojection errors:
429
    cal.cam0_errors_per_view.resize(nSets);
-
 
430
    int idx = 0;
-
 
431
    for(unsigned int i = 0; i < nSets; ++i){
-
 
432
        if(success0[i]){
-
 
433
            int n = (int)Q0[idx].size();
-
 
434
            std::vector<cv::Point2f> qc_proj;
-
 
435
            cv::projectPoints(cv::Mat(Q0[idx]), cam_rvecs0[idx], cam_tvecs0[idx], cal.K0,  cal.k0, qc_proj);
494
    perViewReprojError(success0, Q0, cam_tvecs0, cam_rvecs0, qc0, cal.K0, cal.k0, cal.cam0_errors_per_view);
436
            float err = 0;
-
 
437
            for(unsigned int j=0; j<qc_proj.size(); j++){
495
    perViewReprojError(success1, Q1, cam_tvecs1, cam_rvecs1, qc1, cal.K1, cal.k1, cal.cam1_errors_per_view);
-
 
496
 
438
                cv::Point2f d = qc0[idx][j] - qc_proj[j];
497
    // This would be so much nicer with range adaptors
439
                err += cv::sqrt(d.x*d.x + d.y*d.y);
498
    std::vector< std::vector<cv::Point2f> > qc0StereoFwd2Stage;
440
            }
-
 
441
            cal.cam0_errors_per_view[i] = (float)err/n;
499
    std::vector< std::vector<cv::Point2f> > qc1StereoFwd2Stage;
442
            idx++;
-
 
443
        } else
-
 
444
            cal.cam0_errors_per_view[i] = NAN;
-
 
445
    }
500
 
446
    cal.cam1_errors_per_view.resize(nSets);
-
 
447
    idx = 0;
-
 
448
    for(unsigned int i = 0; i < nSets; ++i){
501
    for(size_t i=0; i<qc0Stereo.size(); i++ )
449
        if(success1[i]){
502
        if(fwdToStageEstimation[i])
450
            int n = (int)Q1[idx].size();
-
 
451
            std::vector<cv::Point2f> qc_proj;
503
            qc0StereoFwd2Stage.push_back(qc0Stereo[i]);
452
            cv::projectPoints(cv::Mat(Q1[idx]), cam_rvecs1[idx], cam_tvecs1[idx], cal.K1,  cal.k1, qc_proj);
-
 
453
            float err = 0;
-
 
-
 
504
 
454
            for(unsigned int j=0; j<qc_proj.size(); j++){
505
    for(size_t i=0; i<qc1Stereo.size(); i++ )
455
                cv::Point2f d = qc1[idx][j] - qc_proj[j];
-
 
456
                err += cv::sqrt(d.x*d.x + d.y*d.y);
506
        if(fwdToStageEstimation[i])
457
            }
-
 
458
            cal.cam1_errors_per_view[i] = (float)err/n;
507
            qc1StereoFwd2Stage.push_back(qc1Stereo[i]);
459
            idx++;
-
 
460
       } else
-
 
461
            cal.cam1_errors_per_view[i] = NAN;
-
 
462
    }
-
 
463
 
508
 
464
//    // hand-eye calibration
-
 
465
//    std::vector<cv::Matx33f> Rc(nValidSets - 1); // rotations/translations of the checkerboard in camera 0 reference frame
-
 
466
//    std::vector<cv::Vec3f> Tc(nValidSets - 1);
-
 
467
//    std::vector<cv::Matx33f> Rr(nValidSets - 1); // in rotation stage reference frame
-
 
468
//    std::vector<cv::Vec3f> Tr(nValidSets - 1);
-
 
469
//    for(int i=0; i<nValidSets-1; i++){
-
 
470
//        // relative transformations in camera
-
 
471
//        cv::Mat cRw1, cRw2;
-
 
472
//        cv::Rodrigues(cam_rvecs0[i], cRw1);
-
 
473
//        cv::Rodrigues(cam_rvecs0[i+1], cRw2);
-
 
474
//        cv::Mat cTw1 = cam_tvecs0[i];
-
 
475
//        cv::Mat cTw2 = cam_tvecs0[i+1];
-
 
476
//        cv::Mat w1Rc = cRw1.t();
-
 
477
//        cv::Mat w1Tc = -cRw1.t()*cTw1;
-
 
478
//        Rc[i] = cv::Mat(cRw2*w1Rc);
-
 
479
//        Tc[i] = cv::Mat(cRw2*w1Tc+cTw2);
-
 
480
 
-
 
481
//        // relative transformations in rotation stage
-
 
482
//        // we define the rotation axis to be in origo, pointing in positive y direction
-
 
483
//        float angleRadians = (angles[i+1]-angles[i])/180.0*M_PI;
-
 
484
//        cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
-
 
485
//        cv::Mat Rri;
-
 
486
//        cv::Rodrigues(rot_rvec, Rri);
-
 
487
//        Rr[i] = Rri;
-
 
488
//        Tr[i] = 0.0;
-
 
489
 
-
 
490
////        std::cout << i << std::endl;
-
 
491
////        std::cout << "cTw1" << cTw1 << std::endl;
-
 
492
////        std::cout << "cTw2" << cTw2 << std::endl;
-
 
493
////        std::cout << "w2Rc" << w2Rc << std::endl;
-
 
494
////        std::cout << "w2Tc" << w2Tc << std::endl;
-
 
495
 
-
 
496
////        std::cout << "w2Rc" << w2Rc << std::endl;
-
 
497
////        std::cout << "w2Tc" << w2Tc << std::endl;
-
 
498
 
-
 
499
////        cv::Mat Rci;
-
 
500
////        cv::Rodrigues(Rc[i], Rci);
-
 
501
////        std::cout << "Rci" << Rci << std::endl;
-
 
502
////        std::cout << "Tc[i]" << Tc[i] << std::endl;
-
 
503
 
-
 
504
////        std::cout << "rot_rvec" << rot_rvec << std::endl;
-
 
505
////        std::cout << "Tr[i]" << Tr[i] << std::endl;
-
 
506
////        std::cout << std::endl;
-
 
507
//    }
-
 
508
 
-
 
509
//    // determine the transformation from rotation stage to camera 0
-
 
510
//    cvtools::handEyeCalibrationTsai(Rc, Tc, Rr, Tr, cal.Rr, cal.Tr);
-
 
511
 
-
 
512
//    for(int i=0; i<nValidSets-1; i++){
-
 
513
//        std::cout << i << std::endl;
-
 
514
 
-
 
515
//        cv::Mat Rci;
-
 
516
//        cv::Rodrigues(Rc[i], Rci);
-
 
517
//        std::cout << "Rc[i]" << Rci << std::endl;
-
 
518
//        std::cout << "Tc[i]" << Tc[i] << std::endl;
-
 
519
 
-
 
520
//        cv::Mat Rri;
-
 
521
//        cv::Rodrigues(Rr[i], Rri);
-
 
522
//        std::cout << "Rr[i]" << Rri << std::endl;
-
 
523
//        std::cout << "Tr[i]" << Tr[i] << std::endl;
-
 
524
 
-
 
525
//        cv::Mat Rcr = cv::Mat(cal.Rr)*cv::Mat(Rc[i])*cv::Mat(cal.Rr.t());
-
 
526
//        cv::Rodrigues(Rcr, Rcr);
-
 
527
//        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);
-
 
528
//        std::cout << "Rcr[i]" << Rcr << std::endl;
-
 
529
//        std::cout << "Tcr[i]" << Tcr << std::endl;
-
 
530
//        std::cout << std::endl;
-
 
531
//    }
-
 
532
 
-
 
533
 
-
 
534
    // Direct rotation axis calibration //
-
 
535
    // full camera matrices
-
 
536
    cv::Matx34f P0 = cv::Matx34f::eye();
-
 
537
    cv::Mat RT1(3, 4, CV_32F);
-
 
538
    cv::Mat(cal.R1).copyTo(RT1(cv::Range(0, 3), cv::Range(0, 3)));
-
 
539
    cv::Mat(cal.T1).copyTo(RT1(cv::Range(0, 3), cv::Range(3, 4)));
-
 
540
    cv::Matx34f P1 = cv::Matx34f(RT1);
-
 
541
 
-
 
542
    // calibration points in camera 0 frame
-
 
543
    std::vector< std::vector<cv::Point3f> > Qcam;
-
 
544
 
-
 
545
    for(unsigned int i=0; i<nValidSets; i++){
-
 
546
        std::vector<cv::Point2f> qc0i, qc1i;
-
 
547
 
-
 
548
        cv::undistortPoints(qc0Stereo[i], qc0i, cal.K0, cal.k0);
-
 
549
        cv::undistortPoints(qc1Stereo[i], qc1i, cal.K1, cal.k1);
-
 
550
//        qc0i = qc0[i];
-
 
551
//        qc1i = qc1[i];
-
 
552
 
-
 
553
        cv::Mat Qhom, Qcami;
-
 
554
        cv::triangulatePoints(P0, P1, qc0i, qc1i, Qhom);
-
 
555
        cvtools::convertMatFromHomogeneous(Qhom, Qcami);
-
 
556
        std::vector<cv::Point3f> QcamiPoints;
-
 
557
        cvtools::matToPoints3f(Qcami, QcamiPoints);
-
 
558
 
-
 
559
        Qcam.push_back(QcamiPoints);
-
 
560
    }
-
 
561
 
-
 
562
//    cv::Mat QcamMat(Qcam.size(), Qcam[0].size(), CV_32FC3);
-
 
563
//    for(int i=0; i<Qcam.size(); i++)
-
 
564
//        for(int j=0; j<Qcam[0].size(); j++)
-
 
565
//            QcamMat.at<cv::Point3f>(i,j) = Qcam[i][j];
-
 
566
//    cvtools::writeMat(QcamMat, "Qcam.mat", "Qcam");
-
 
567
 
-
 
568
    cv::Vec3f axis, point;
-
 
569
    float rot_axis_error;
-
 
570
    rotationAxisCalibration(Qcam, Qi, axis, point, rot_axis_error);
509
    performStageCalibration(qc0StereoFwd2Stage,qc1StereoFwd2Stage,cal);
571
 
-
 
572
    // construct transformation matrix
-
 
573
    cv::Vec3f ex = axis.cross(cv::Vec3f(0,0,1.0));
-
 
574
    ex = cv::normalize(ex);
-
 
575
    cv::Vec3f ez = ex.cross(axis);
-
 
576
    ez = cv::normalize(ez);
-
 
577
 
-
 
578
    cv::Mat RrMat(3, 3, CV_32F);
-
 
579
    cv::Mat(ex).copyTo(RrMat.col(0));
-
 
580
    cv::Mat(axis).copyTo(RrMat.col(1));
-
 
581
    cv::Mat(ez).copyTo(RrMat.col(2));
-
 
582
 
-
 
583
    cal.Rr = cv::Matx33f(RrMat).t();
-
 
584
    cal.Tr = -cv::Matx33f(RrMat).t()*point;
-
 
585
    cal.rot_axis_error = rot_axis_error;
-
 
586
 
510
 
587
    // Print to std::cout
511
    // Print to std::cout
-
 
512
    std::cout << std::endl << "========== BEGIN Calibration info ================================================" << std::endl;
-
 
513
    std::cout << "Num. Images used for intrinsics of cam0: " << qc0.size() << std::endl;
-
 
514
    std::cout << "Num. Images used for intrinsics of cam1: " << qc1.size() << std::endl;
-
 
515
    std::cout << "Num. Images used for extrinsocs of cam1: " << nValidStereoSets << std::endl;
-
 
516
    std::cout << "Num. Images used for rotation stage axis estim.: " << qc0StereoFwd2Stage.size() << std::endl << std::endl;
588
    cal.print();
517
    cal.print();
-
 
518
    std::cout << "========== END   Calibration info ================================================" << std::endl << std::endl;
589
 
519
 
590
    // save to (reentrant qsettings object)
520
    // save to (reentrant qsettings object)
591
    settings.setValue("calibration/parameters", QVariant::fromValue(cal));
521
    settings.setValue("calibration/parameters", QVariant::fromValue(cal));
592
 
522
 
593
    emit done();
523
    emit done();
-
 
524
}
-
 
525
 
-
 
526
 
-
 
527
void SMCalibrationWorker::performStageCalibration(
-
 
528
        const std::vector< std::vector<cv::Point2f> > &qc0Stereo,
-
 
529
        const std::vector< std::vector<cv::Point2f> > &qc1Stereo,
-
 
530
        SMCalibrationParameters& cal){
-
 
531
    assert(qc0Stereo.size()==qc1Stereo.size());
-
 
532
    if(qc0Stereo.size()>1 && qc1Stereo.size()>1){
-
 
533
        // save to (reentrant qsettings object)
-
 
534
        QSettings settings;
-
 
535
 
-
 
536
        // Generate world object coordinates [mm]
-
 
537
        std::vector<cv::Point3f> Qi = generateObjCoordsInWorldCS(
-
 
538
                    cv::Size(settings.value("calibration/patternSizeX", 22).toInt(),settings.value("calibration/patternSizeY", 13).toInt()),
-
 
539
                    settings.value("calibration/squareSize", 10.0).toFloat());
-
 
540
 
-
 
541
        // Direct rotation axis calibration //
-
 
542
        // full camera matrices
-
 
543
        cv::Matx34f P0 = cv::Matx34f::eye();
-
 
544
        cv::Mat RT1(3, 4, CV_32F);
-
 
545
        cv::Mat(cal.R1).copyTo(RT1(cv::Range(0, 3), cv::Range(0, 3)));
-
 
546
        cv::Mat(cal.T1).copyTo(RT1(cv::Range(0, 3), cv::Range(3, 4)));
-
 
547
        cv::Matx34f P1 = cv::Matx34f(RT1);
-
 
548
 
-
 
549
        // calibration points in camera 0 frame
-
 
550
        std::vector< std::vector<cv::Point3f> > Qcam(qc0Stereo.size());
-
 
551
#pragma omp parallel for
-
 
552
        for(unsigned int i=0; i<qc0Stereo.size(); i++){
-
 
553
            std::vector<cv::Point2f> qc0i, qc1i;
-
 
554
 
-
 
555
            cv::undistortPoints(qc0Stereo[i], qc0i, cal.K0, cal.k0);
-
 
556
            cv::undistortPoints(qc1Stereo[i], qc1i, cal.K1, cal.k1);
-
 
557
 
-
 
558
            cv::Mat Qhom, Qcami;
-
 
559
            cv::triangulatePoints(P0, P1, qc0i, qc1i, Qhom);
-
 
560
            cvtools::convertMatFromHomogeneous(Qhom, Qcami);
-
 
561
            std::vector<cv::Point3f> QcamiPoints;
-
 
562
            cvtools::matToPoints3f(Qcami, QcamiPoints);
594
 
563
 
-
 
564
            Qcam[i] = QcamiPoints;
-
 
565
        }
-
 
566
 
-
 
567
        cv::Vec3f axis, point;
-
 
568
        float rot_axis_error;
-
 
569
        rotationAxisCalibration(Qcam, Qi, axis, point, rot_axis_error);
-
 
570
 
-
 
571
        // construct transformation matrix
-
 
572
        cv::Vec3f ex = axis.cross(cv::Vec3f(0,0,1.0));
-
 
573
        ex = cv::normalize(ex);
-
 
574
        cv::Vec3f ez = ex.cross(axis);
-
 
575
        ez = cv::normalize(ez);
-
 
576
 
-
 
577
        cv::Mat RrMat(3, 3, CV_32F);
-
 
578
        cv::Mat(ex).copyTo(RrMat.col(0));
-
 
579
        cv::Mat(axis).copyTo(RrMat.col(1));
-
 
580
        cv::Mat(ez).copyTo(RrMat.col(2));
-
 
581
 
-
 
582
        cal.Rr = cv::Matx33f(RrMat).t();
-
 
583
        cal.Tr = -cv::Matx33f(RrMat).t()*point;
-
 
584
        cal.rot_axis_error = rot_axis_error;
-
 
585
    }else{
-
 
586
        cal.Rr = cv::Matx33f::eye();
-
 
587
        cal.Tr = cv::Vec3f(0,0,0);
-
 
588
        cal.rot_axis_error = -1;
-
 
589
    }
595
}
590
}