Subversion Repositories seema-scanner

Rev

Rev 198 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 198 Rev 205
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
#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, const std::vector<cv::Point3f> Qobj, cv::Vec3f &axis, cv::Vec3f &point, float &error){
59
static void rotationAxisCalibration(const std::vector< std::vector<cv::Point3f> > Qcam, const std::vector<cv::Point3f> Qobj, cv::Vec3f &axis, cv::Vec3f &point, float &error){
60
 
60
 
61
    // number of frames (points on each arch)
61
    // number of frames (points on each arch)
62
    int l = Qcam.size();
62
    int l = Qcam.size();
63
 
63
 
64
    // number of points in each frame
64
    // number of points in each frame
65
    size_t mn = Qobj.size();
65
    size_t mn = Qobj.size();
66
 
66
 
67
    assert(mn == Qcam[0].size());
67
    assert(mn == Qcam[0].size());
68
 
68
 
69
    // construct matrix for axis determination
69
    // construct matrix for axis determination
70
    cv::Mat M(6, 6, CV_32F, cv::Scalar(0));
70
    cv::Mat M(6, 6, CV_32F, cv::Scalar(0));
71
 
71
 
72
    for(int k=0; k<l; k++){
72
    for(int k=0; k<l; k++){
73
        for(unsigned int idx=0; idx<mn; idx++){
73
        for(unsigned int idx=0; idx<mn; idx++){
74
 
74
 
75
//            float i = Qobj[idx].x+4;
75
//            float i = Qobj[idx].x+4;
76
//            float j = Qobj[idx].y+4;
76
//            float j = Qobj[idx].y+4;
77
            float i = Qobj[idx].x;
77
            float i = Qobj[idx].x;
78
            float j = Qobj[idx].y;
78
            float j = Qobj[idx].y;
79
 
79
 
80
            float x = Qcam[k][idx].x;
80
            float x = Qcam[k][idx].x;
81
            float y = Qcam[k][idx].y;
81
            float y = Qcam[k][idx].y;
82
            float z = Qcam[k][idx].z;
82
            float z = Qcam[k][idx].z;
83
 
83
 
84
            M += (cv::Mat_<float>(6,6) << x*x, x*y, x*z, x, i*x, j*x,
84
            M += (cv::Mat_<float>(6,6) << x*x, x*y, x*z, x, i*x, j*x,
85
                                            0, y*y, y*z, y, i*y, j*y,
85
                                            0, y*y, y*z, y, i*y, j*y,
86
                                            0,   0, z*z, z, i*z, j*z,
86
                                            0,   0, z*z, z, i*z, j*z,
87
                                            0,   0,   0, 1,   i,   j,
87
                                            0,   0,   0, 1,   i,   j,
88
                                            0,   0,   0, 0, i*i, i*j,
88
                                            0,   0,   0, 0, i*i, i*j,
89
                                            0,   0,   0, 0,   0, j*j);
89
                                            0,   0,   0, 0,   0, j*j);
90
 
90
 
91
        }
91
        }
92
    }
92
    }
93
 
93
 
94
    cv::completeSymm(M, false);
94
    cv::completeSymm(M, false);
95
 
95
 
96
    // solve for axis
96
    // solve for axis
97
    std::vector<float> lambda;
97
    std::vector<float> lambda;
98
    cv::Mat u;
98
    cv::Mat u;
99
    cv::eigen(M, lambda, u);
99
    cv::eigen(M, lambda, u);
100
 
100
 
101
    float minLambda = std::abs(lambda[0]);
101
    float minLambda = std::abs(lambda[0]);
102
    int idx = 0;
102
    int idx = 0;
103
    for(unsigned int i=1; i<lambda.size(); i++){
103
    for(unsigned int i=1; i<lambda.size(); i++){
104
        if(abs(lambda[i]) < minLambda){
104
        if(abs(lambda[i]) < minLambda){
105
            minLambda = lambda[i];
105
            minLambda = lambda[i];
106
            idx = i;
106
            idx = i;
107
        }
107
        }
108
    }
108
    }
109
 
109
 
110
    axis = u.row(idx).colRange(0, 3);
110
    axis = u.row(idx).colRange(0, 3);
111
    axis = cv::normalize(axis);
111
    axis = cv::normalize(axis);
112
 
112
 
113
    float nx = u.at<float>(idx, 0);
113
    float nx = u.at<float>(idx, 0);
114
    float ny = u.at<float>(idx, 1);
114
    float ny = u.at<float>(idx, 1);
115
    float nz = u.at<float>(idx, 2);
115
    float nz = u.at<float>(idx, 2);
116
    //float d  = u.at<float>(idx, 3);
116
    //float d  = u.at<float>(idx, 3);
117
    float dh = u.at<float>(idx, 4);
117
    float dh = u.at<float>(idx, 4);
118
    float dv = u.at<float>(idx, 5);
118
    float dv = u.at<float>(idx, 5);
119
 
119
 
120
//    // Paper version: c is initially eliminated
120
//    // Paper version: c is initially eliminated
121
//    cv::Mat A(l*mn, mn+2, CV_32F, cv::Scalar(0.0));
121
//    cv::Mat A(l*mn, mn+2, CV_32F, cv::Scalar(0.0));
122
//    cv::Mat bb(l*mn, 1, CV_32F);
122
//    cv::Mat bb(l*mn, 1, CV_32F);
123
 
123
 
124
//    for(int k=0; k<l; k++){
124
//    for(int k=0; k<l; k++){
125
//        for(unsigned int idx=0; idx<mn; idx++){
125
//        for(unsigned int idx=0; idx<mn; idx++){
126
 
126
 
127
//            float i = Qobj[idx].x;
127
//            float i = Qobj[idx].x;
128
//            float j = Qobj[idx].y;
128
//            float j = Qobj[idx].y;
129
 
129
 
130
//            float x = Qcam[k][idx].x;
130
//            float x = Qcam[k][idx].x;
131
//            float y = Qcam[k][idx].y;
131
//            float y = Qcam[k][idx].y;
132
//            float z = Qcam[k][idx].z;
132
//            float z = Qcam[k][idx].z;
133
 
133
 
134
//            float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
134
//            float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
135
 
135
 
136
//            int row = k*mn+idx;
136
//            int row = k*mn+idx;
137
//            A.at<float>(row, 0) = 2*x - (2*z*nx)/nz;
137
//            A.at<float>(row, 0) = 2*x - (2*z*nx)/nz;
138
//            A.at<float>(row, 1) = 2*y - (2*z*ny)/nz;
138
//            A.at<float>(row, 1) = 2*y - (2*z*ny)/nz;
139
//            A.at<float>(row, idx+2) = 1.0;
139
//            A.at<float>(row, idx+2) = 1.0;
140
 
140
 
141
//            bb.at<float>(row, 0) = f + (2*z*d)/nz;
141
//            bb.at<float>(row, 0) = f + (2*z*d)/nz;
142
//        }
142
//        }
143
//    }
143
//    }
144
 
144
 
145
//    // solve for point
145
//    // solve for point
146
//    cv::Mat abe;
146
//    cv::Mat abe;
147
//    cv::solve(A, bb, abe, cv::DECOMP_SVD);
147
//    cv::solve(A, bb, abe, cv::DECOMP_SVD);
148
 
148
 
149
//    float a = abe.at<float>(0, 0);
149
//    float a = abe.at<float>(0, 0);
150
//    float b = abe.at<float>(1, 0);
150
//    float b = abe.at<float>(1, 0);
151
//    float c = -(nx*a+ny*b+d)/nz;
151
//    float c = -(nx*a+ny*b+d)/nz;
152
 
152
 
153
    // Our version: solve simultanously for a,b,c
153
    // Our version: solve simultanously for a,b,c
154
    cv::Mat A(l*mn, mn+3, CV_32F, cv::Scalar(0.0));
154
    cv::Mat A(l*mn, mn+3, CV_32F, cv::Scalar(0.0));
155
    cv::Mat bb(l*mn, 1, CV_32F);
155
    cv::Mat bb(l*mn, 1, CV_32F);
156
 
156
 
157
    for(int k=0; k<l; k++){
157
    for(int k=0; k<l; k++){
158
        for(unsigned int idx=0; idx<mn; idx++){
158
        for(unsigned int idx=0; idx<mn; idx++){
159
 
159
 
160
            float i = Qobj[idx].x;
160
            float i = Qobj[idx].x;
161
            float j = Qobj[idx].y;
161
            float j = Qobj[idx].y;
162
 
162
 
163
            float x = Qcam[k][idx].x;
163
            float x = Qcam[k][idx].x;
164
            float y = Qcam[k][idx].y;
164
            float y = Qcam[k][idx].y;
165
            float z = Qcam[k][idx].z;
165
            float z = Qcam[k][idx].z;
166
 
166
 
167
            float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
167
            float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
168
 
168
 
169
            int row = k*mn+idx;
169
            int row = k*mn+idx;
170
            A.at<float>(row, 0) = 2*x;
170
            A.at<float>(row, 0) = 2*x;
171
            A.at<float>(row, 1) = 2*y;
171
            A.at<float>(row, 1) = 2*y;
172
            A.at<float>(row, 2) = 2*z;
172
            A.at<float>(row, 2) = 2*z;
173
            A.at<float>(row, idx+3) = 1.0;
173
            A.at<float>(row, idx+3) = 1.0;
174
 
174
 
175
            bb.at<float>(row, 0) = f;
175
            bb.at<float>(row, 0) = f;
176
        }
176
        }
177
    }
177
    }
178
 
178
 
179
    // solve for point
179
    // solve for point
180
    cv::Mat abe;
180
    cv::Mat abe;
181
    cv::solve(A, bb, abe, cv::DECOMP_SVD);
181
    cv::solve(A, bb, abe, cv::DECOMP_SVD);
182
 
182
 
183
    float a = abe.at<float>(0, 0);
183
    float a = abe.at<float>(0, 0);
184
    float b = abe.at<float>(1, 0);
184
    float b = abe.at<float>(1, 0);
185
    float c = abe.at<float>(2, 0);
185
    float c = abe.at<float>(2, 0);
186
 
186
 
187
    point[0] = a;
187
    point[0] = a;
188
    point[1] = b;
188
    point[1] = b;
189
    point[2] = c;
189
    point[2] = c;
190
 
190
 
191
    // Non-linear optimization using Ceres
191
    // Non-linear optimization using Ceres
192
    double pointArray[] = {point[0], point[1], point[2]};
192
    double pointArray[] = {point[0], point[1], point[2]};
193
    double axisArray[] = {axis[0], axis[1], axis[2]};
193
    double axisArray[] = {axis[0], axis[1], axis[2]};
194
 
194
 
195
    ceres::Problem problem;
195
    ceres::Problem problem;
196
    // loop through saddle points
196
    // loop through saddle points
197
    for(unsigned int idx=0; idx<mn; idx++){
197
    for(unsigned int idx=0; idx<mn; idx++){
198
        std::vector<cv::Point3f> pointsOnArch(l);
198
        std::vector<cv::Point3f> pointsOnArch(l);
199
        for(int k=0; k<l; k++){
199
        for(int k=0; k<l; k++){
200
            pointsOnArch[k] = Qcam[k][idx];
200
            pointsOnArch[k] = Qcam[k][idx];
201
        }
201
        }
202
        ceres::CostFunction* cost_function =
202
        ceres::CostFunction* cost_function =
203
           new ceres::AutoDiffCostFunction<CircleResidual, ceres::DYNAMIC, 3, 3>(
203
           new ceres::AutoDiffCostFunction<CircleResidual, ceres::DYNAMIC, 3, 3>(
204
               new CircleResidual(pointsOnArch), l);
204
               new CircleResidual(pointsOnArch), l);
205
        problem.AddResidualBlock(cost_function, NULL, pointArray, axisArray);
205
        problem.AddResidualBlock(cost_function, NULL, pointArray, axisArray);
206
    }
206
    }
207
 
207
 
208
    // Run the solver!
208
    // Run the solver!
209
    ceres::Solver::Options options;
209
    ceres::Solver::Options options;
210
    options.linear_solver_type = ceres::DENSE_QR;
210
    options.linear_solver_type = ceres::DENSE_QR;
211
    options.minimizer_progress_to_stdout = true;
211
    options.minimizer_progress_to_stdout = true;
212
    ceres::Solver::Summary summary;
212
    ceres::Solver::Summary summary;
213
    ceres::Solve(options, &problem, &summary);
213
    ceres::Solve(options, &problem, &summary);
214
 
214
 
215
    std::cout << summary.BriefReport() << "\n";
215
    std::cout << summary.BriefReport() << "\n";
216
 
216
 
217
    point = cv::Vec3f(pointArray[0], pointArray[1], pointArray[2]);
217
    point = cv::Vec3f(pointArray[0], pointArray[1], pointArray[2]);
218
    axis = cv::Vec3f(axisArray[0], axisArray[1], axisArray[2]);
218
    axis = cv::Vec3f(axisArray[0], axisArray[1], axisArray[2]);
219
    axis /= cv::norm(axis);
219
    axis /= cv::norm(axis);
220
 
220
 
221
 
221
 
222
    // Error estimate (sum of squared differences)
222
    // Error estimate (sum of squared differences)
223
    error = 0;
223
    error = 0;
224
    // loop through saddle points
224
    // loop through saddle points
225
    for(unsigned int idx=0; idx<mn; idx++){
225
    for(unsigned int idx=0; idx<mn; idx++){
226
 
226
 
227
        // vector of distances from rotation axis
227
        // vector of distances from rotation axis
228
        std::vector<float> dI(l);
228
        std::vector<float> dI(l);
229
        // loop through angular positions
229
        // loop through angular positions
230
        for(int k=0; k<l; k++){
230
        for(int k=0; k<l; k++){
231
            cv::Vec3f p = cv::Vec3f(Qcam[k][idx]);
231
            cv::Vec3f p = cv::Vec3f(Qcam[k][idx]);
232
            // point to line distance
232
            // point to line distance
233
            dI[k] = cv::norm((point-p)-(point-p).dot(axis)*axis);
233
            dI[k] = cv::norm((point-p)-(point-p).dot(axis)*axis);
234
        }
234
        }
235
        float sum = std::accumulate(dI.begin(), dI.end(), 0.0);
235
        float sum = std::accumulate(dI.begin(), dI.end(), 0.0);
236
        float mean = sum / dI.size();
236
        float mean = sum / dI.size();
237
        float meanDev = 0;
237
        float meanDev = 0;
238
        for(int k=0; k<l; k++){
238
        for(int k=0; k<l; k++){
239
            meanDev += std::abs(dI[k] - mean);
239
            meanDev += std::abs(dI[k] - mean);
240
        }
240
        }
241
        meanDev /= l;
241
        meanDev /= l;
242
        error += meanDev;
242
        error += meanDev;
243
    }
243
    }
244
    error /= mn;
244
    error /= mn;
245
}
245
}
246
 
246
 
247
void SMCalibrationWorker::performCalibration(std::vector<SMCalibrationSet> calibrationData){
247
void SMCalibrationWorker::performCalibration(std::vector<SMCalibrationSet> calibrationData){
248
 
248
 
249
    QSettings settings;
249
    QSettings settings;
250
 
250
 
251
    // Number of saddle points on calibration pattern
251
    // Number of saddle points on calibration pattern
252
    int checkerCountX = settings.value("calibration/patternSizeX", 22).toInt();
252
    int checkerCountX = settings.value("calibration/patternSizeX", 22).toInt();
253
    int checkerCountY = settings.value("calibration/patternSizeY", 13).toInt();
253
    int checkerCountY = settings.value("calibration/patternSizeY", 13).toInt();
254
    cv::Size checkerCount(checkerCountX, checkerCountY);
254
    cv::Size checkerCount(checkerCountX, checkerCountY);
255
 
255
 
256
    unsigned int nSets = calibrationData.size();
256
    unsigned int nSets = calibrationData.size();
257
 
257
 
258
    // 2D Points collected for OpenCV's calibration procedures
258
    // 2D Points collected for OpenCV's calibration procedures
259
    std::vector< std::vector<cv::Point2f> > qc0, qc1;
259
    std::vector< std::vector<cv::Point2f> > qc0, qc1;
260
    std::vector< std::vector<cv::Point2f> > qc0Stereo, qc1Stereo;
260
    std::vector< std::vector<cv::Point2f> > qc0Stereo, qc1Stereo;
261
 
261
 
262
    std::vector<bool> success0(nSets), success1(nSets);
262
    std::vector<bool> success0(nSets), success1(nSets);
263
 
263
 
264
    std::vector<float> angles;
264
    std::vector<float> angles;
265
 
265
 
266
    // Loop through calibration sets
266
    // Loop through calibration sets
267
    for(unsigned int i=0; i<nSets; i++){
267
    for(unsigned int i=0; i<nSets; i++){
268
 
268
 
269
        SMCalibrationSet SMCalibrationSetI = calibrationData[i];
269
        SMCalibrationSet SMCalibrationSetI = calibrationData[i];
270
 
270
 
271
        if(!SMCalibrationSetI.checked)
271
        if(!SMCalibrationSetI.checked)
272
            continue;
272
            continue;
273
 
273
 
274
        // Camera 0
274
        // Camera 0
275
        std::vector<cv::Point2f> qci0;
275
        std::vector<cv::Point2f> qci0;
276
 
276
 
277
        // Convert to grayscale
277
        // Convert to grayscale
278
        cv::Mat gray;
278
        cv::Mat gray;
279
        if(SMCalibrationSetI.frame0.channels() == 1)
279
        if(SMCalibrationSetI.frame0.channels() == 1)
280
            cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_BayerBG2GRAY);
280
            cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_BayerBG2GRAY);
281
        else
281
        else
282
            cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_RGB2GRAY);
282
            cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_RGB2GRAY);
283
 
283
 
284
        // Extract checker corners
284
        // Extract checker corners
285
        success0[i] = cv::findChessboardCorners(gray, checkerCount, qci0, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
285
        success0[i] = cv::findChessboardCorners(gray, checkerCount, qci0, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
286
        if(success0[i]){
286
        if(success0[i]){
287
            cv::cornerSubPix(gray, qci0, cv::Size(6, 6), cv::Size(1, 1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
287
            cv::cornerSubPix(gray, qci0, cv::Size(6, 6), cv::Size(1, 1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
288
            // Draw colored chessboard
288
            // Draw colored chessboard
289
            cv::Mat color;
289
            cv::Mat color;
290
            if(SMCalibrationSetI.frame0.channels() == 1)
290
            if(SMCalibrationSetI.frame0.channels() == 1)
291
                cv::cvtColor(SMCalibrationSetI.frame0, color, CV_BayerBG2RGB);
291
                cv::cvtColor(SMCalibrationSetI.frame0, color, CV_BayerBG2RGB);
292
            else
292
            else
293
                color = SMCalibrationSetI.frame0.clone();
293
                color = SMCalibrationSetI.frame0.clone();
294
 
294
 
295
            cvtools::drawChessboardCorners(color, checkerCount, qci0, success0[i], 10);
295
            cvtools::drawChessboardCorners(color, checkerCount, qci0, success0[i], 10);
296
            SMCalibrationSetI.frame0Result = color;
296
            SMCalibrationSetI.frame0Result = color;
297
        }
297
        }
298
 
298
 
299
        emit newFrameResult(i, 0, success0[i], SMCalibrationSetI.frame0Result);
299
        emit newFrameResult(i, 0, success0[i], SMCalibrationSetI.frame0Result);
300
 
300
 
301
        // Camera 1
301
        // Camera 1
302
        std::vector<cv::Point2f> qci1;
302
        std::vector<cv::Point2f> qci1;
303
 
303
 
304
        // Convert to grayscale
304
        // Convert to grayscale
305
        if(SMCalibrationSetI.frame1.channels() == 1)
305
        if(SMCalibrationSetI.frame1.channels() == 1)
306
            cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_BayerBG2GRAY);
306
            cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_BayerBG2GRAY);
307
        else
307
        else
308
            cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_RGB2GRAY);
308
            cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_RGB2GRAY);
309
 
309
 
310
        // Extract checker corners
310
        // Extract checker corners
311
        success1[i] = cv::findChessboardCorners(gray, checkerCount, qci1, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
311
        success1[i] = cv::findChessboardCorners(gray, checkerCount, qci1, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_FAST_CHECK);
312
        if(success1[i]){
312
        if(success1[i]){
313
            cv::cornerSubPix(gray, qci1, cv::Size(6, 6), cv::Size(1, 1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
313
            cv::cornerSubPix(gray, qci1, cv::Size(6, 6), cv::Size(1, 1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.0001));
314
            // Draw colored chessboard
314
            // Draw colored chessboard
315
            cv::Mat color;
315
            cv::Mat color;
316
            if(SMCalibrationSetI.frame1.channels() == 1)
316
            if(SMCalibrationSetI.frame1.channels() == 1)
317
                cv::cvtColor(SMCalibrationSetI.frame1, color, CV_BayerBG2RGB);
317
                cv::cvtColor(SMCalibrationSetI.frame1, color, CV_BayerBG2RGB);
318
            else
318
            else
319
                color = SMCalibrationSetI.frame1.clone();
319
                color = SMCalibrationSetI.frame1.clone();
320
 
320
 
321
            cvtools::drawChessboardCorners(color, checkerCount, qci1, success1[i], 10);
321
            cvtools::drawChessboardCorners(color, checkerCount, qci1, success1[i], 10);
322
            SMCalibrationSetI.frame1Result = color;
322
            SMCalibrationSetI.frame1Result = color;
323
        }
323
        }
324
 
324
 
325
        emit newFrameResult(i, 1, success1[i], SMCalibrationSetI.frame1Result);
325
        emit newFrameResult(i, 1, success1[i], SMCalibrationSetI.frame1Result);
326
 
326
 
327
        if(success0[i])
327
        if(success0[i])
328
            qc0.push_back(qci0);
328
            qc0.push_back(qci0);
329
 
329
 
330
        if(success1[i])
330
        if(success1[i])
331
            qc1.push_back(qci1);
331
            qc1.push_back(qci1);
332
 
332
 
333
        if(success0[i] && success1[i]){
333
        if(success0[i] && success1[i]){
334
            qc0Stereo.push_back(qci0);
334
            qc0Stereo.push_back(qci0);
335
            qc1Stereo.push_back(qci1);
335
            qc1Stereo.push_back(qci1);
336
            angles.push_back(SMCalibrationSetI.rotationAngle);
336
            angles.push_back(SMCalibrationSetI.rotationAngle);
337
        }
337
        }
338
 
338
 
339
        // Show progress
339
        // Show progress
340
        emit newSetProcessed(i);
340
        emit newSetProcessed(i);
341
    }
341
    }
342
 
342
 
343
    size_t nValidSets = angles.size();
343
    size_t nValidSets = angles.size();
344
    if(nValidSets < 2){
344
    if(nValidSets < 2){
345
        std::cerr << "Not enough valid calibration sequences!" << std::endl;
345
        std::cerr << "Not enough valid calibration sequences!" << std::endl;
346
        emit done();
346
        emit done();
347
        return;
347
        return;
348
    }
348
    }
349
 
349
 
350
    // Generate world object coordinates [mm]
350
    // Generate world object coordinates [mm]
351
    float checkerSize = settings.value("calibration/squareSize", 10.0).toFloat(); // width and height of one checker square in mm
351
    float checkerSize = settings.value("calibration/squareSize", 10.0).toFloat(); // width and height of one checker square in mm
352
    std::vector<cv::Point3f> Qi;
352
    std::vector<cv::Point3f> Qi;
353
    for (int h=0; h<checkerCount.height; h++)
353
    for (int h=0; h<checkerCount.height; h++)
354
        for (int w=0; w<checkerCount.width; w++)
354
        for (int w=0; w<checkerCount.width; w++)
355
            Qi.push_back(cv::Point3f(checkerSize * w, checkerSize* h, 0.0));
355
            Qi.push_back(cv::Point3f(checkerSize * w, checkerSize* h, 0.0));
356
 
356
 
357
    std::vector< std::vector<cv::Point3f> > Q0, Q1, QStereo;
357
    std::vector< std::vector<cv::Point3f> > Q0, Q1, QStereo;
358
    for(unsigned int i=0; i<qc0.size(); i++)
358
    for(unsigned int i=0; i<qc0.size(); i++)
359
        Q0.push_back(Qi);
359
        Q0.push_back(Qi);
360
    for(unsigned int i=0; i<qc1.size(); i++)
360
    for(unsigned int i=0; i<qc1.size(); i++)
361
        Q1.push_back(Qi);
361
        Q1.push_back(Qi);
362
    for(unsigned int i=0; i<nValidSets; i++)
362
    for(unsigned int i=0; i<nValidSets; i++)
363
        QStereo.push_back(Qi);
363
        QStereo.push_back(Qi);
364
 
364
 
365
    // calibrate the cameras
365
    // calibrate the cameras
366
    SMCalibrationParameters cal;
366
    SMCalibrationParameters cal;
367
    cal.frameWidth = calibrationData[0].frame0.cols;
367
    cal.frameWidth = calibrationData[0].frame0.cols;
368
    cal.frameHeight = calibrationData[0].frame0.rows;
368
    cal.frameHeight = calibrationData[0].frame0.rows;
369
    cv::Size frameSize(cal.frameWidth, cal.frameHeight);
369
    cv::Size frameSize(cal.frameWidth, cal.frameHeight);
370
 
370
 
371
    // determine only k1, k2 for lens distortion
371
    // determine only k1, k2 for lens distortion
372
    int flags = cv::CALIB_FIX_ASPECT_RATIO + cv::CALIB_FIX_K3 + cv::CALIB_ZERO_TANGENT_DIST + cv::CALIB_FIX_PRINCIPAL_POINT;
372
    int flags = cv::CALIB_FIX_ASPECT_RATIO + cv::CALIB_FIX_K3 + cv::CALIB_ZERO_TANGENT_DIST + cv::CALIB_FIX_PRINCIPAL_POINT;
373
    // Note: several of the output arguments below must be cv::Mat, otherwise segfault
373
    // Note: several of the output arguments below must be cv::Mat, otherwise segfault
374
    std::vector<cv::Mat> cam_rvecs0, cam_tvecs0;
374
    std::vector<cv::Mat> cam_rvecs0, cam_tvecs0;
375
    cal.cam0_error = cv::calibrateCamera(Q0, qc0, frameSize, cal.K0, cal.k0, cam_rvecs0, cam_tvecs0, flags,
375
    cal.cam0_error = cv::calibrateCamera(Q0, qc0, frameSize, cal.K0, cal.k0, cam_rvecs0, cam_tvecs0, flags,
376
                                         cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
376
                                         cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
377
//std::cout << cal.k0 << std::endl;
377
//std::cout << cal.k0 << std::endl;
378
//    // refine extrinsics for camera 0
378
//    // refine extrinsics for camera 0
379
//    for(int i=0; i<Q.size(); i++)
379
//    for(int i=0; i<Q.size(); i++)
380
//        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);
380
//        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);
381
 
381
 
382
    std::vector<cv::Mat> cam_rvecs1, cam_tvecs1;
382
    std::vector<cv::Mat> cam_rvecs1, cam_tvecs1;
383
    cal.cam1_error = cv::calibrateCamera(Q1, qc1, frameSize, cal.K1, cal.k1, cam_rvecs1, cam_tvecs1, flags,
383
    cal.cam1_error = cv::calibrateCamera(Q1, qc1, frameSize, cal.K1, cal.k1, cam_rvecs1, cam_tvecs1, flags,
384
                                         cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
384
                                         cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 100, DBL_EPSILON));
385
//std::cout << cal.k1 << std::endl;
385
//std::cout << cal.k1 << std::endl;
386
    // stereo calibration
386
    // stereo calibration
387
    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;
387
    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;
388
    cv::Mat E, F, R1, T1;
388
    cv::Mat E, F, R1, T1;
389
    cal.stereo_error = cv::stereoCalibrate(QStereo, qc0Stereo, qc1Stereo, cal.K0, cal.k0, cal.K1, cal.k1,
389
    cal.stereo_error = cv::stereoCalibrate(QStereo, qc0Stereo, qc1Stereo, cal.K0, cal.k0, cal.K1, cal.k1,
390
                                              frameSize, R1, T1, E, F,
390
                                              frameSize, R1, T1, E, F,
391
                                              cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 200, DBL_EPSILON),
391
                                              cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 200, DBL_EPSILON),
392
                                              flags_stereo);
392
                                              flags_stereo);
393
 
393
 
394
    cal.R1 = R1;
394
    cal.R1 = R1;
395
    cal.T1 = T1;
395
    cal.T1 = T1;
396
    cal.E = E;
396
    cal.E = E;
397
    cal.F = F;
397
    cal.F = F;
398
 
398
 
399
    // Determine per-view reprojection errors:
399
    // Determine per-view reprojection errors:
400
    cal.cam0_errors_per_view.resize(nSets);
400
    cal.cam0_errors_per_view.resize(nSets);
401
    int idx = 0;
401
    int idx = 0;
402
    for(unsigned int i = 0; i < nSets; ++i){
402
    for(unsigned int i = 0; i < nSets; ++i){
403
        if(success0[i]){
403
        if(success0[i]){
404
            int n = (int)Q0[idx].size();
404
            int n = (int)Q0[idx].size();
405
            std::vector<cv::Point2f> qc_proj;
405
            std::vector<cv::Point2f> qc_proj;
406
            cv::projectPoints(cv::Mat(Q0[idx]), cam_rvecs0[idx], cam_tvecs0[idx], cal.K0,  cal.k0, qc_proj);
406
            cv::projectPoints(cv::Mat(Q0[idx]), cam_rvecs0[idx], cam_tvecs0[idx], cal.K0,  cal.k0, qc_proj);
407
            float err = 0;
407
            float err = 0;
408
            for(unsigned int j=0; j<qc_proj.size(); j++){
408
            for(unsigned int j=0; j<qc_proj.size(); j++){
409
                cv::Point2f d = qc0[idx][j] - qc_proj[j];
409
                cv::Point2f d = qc0[idx][j] - qc_proj[j];
410
                err += cv::sqrt(d.x*d.x + d.y*d.y);
410
                err += cv::sqrt(d.x*d.x + d.y*d.y);
411
            }
411
            }
412
            cal.cam0_errors_per_view[i] = (float)err/n;
412
            cal.cam0_errors_per_view[i] = (float)err/n;
413
            idx++;
413
            idx++;
414
        } else
414
        } else
415
            cal.cam0_errors_per_view[i] = NAN;
415
            cal.cam0_errors_per_view[i] = NAN;
416
    }
416
    }
417
    cal.cam1_errors_per_view.resize(nSets);
417
    cal.cam1_errors_per_view.resize(nSets);
418
    idx = 0;
418
    idx = 0;
419
    for(unsigned int i = 0; i < nSets; ++i){
419
    for(unsigned int i = 0; i < nSets; ++i){
420
        if(success1[i]){
420
        if(success1[i]){
421
            int n = (int)Q1[idx].size();
421
            int n = (int)Q1[idx].size();
422
            std::vector<cv::Point2f> qc_proj;
422
            std::vector<cv::Point2f> qc_proj;
423
            cv::projectPoints(cv::Mat(Q1[idx]), cam_rvecs1[idx], cam_tvecs1[idx], cal.K1,  cal.k1, qc_proj);
423
            cv::projectPoints(cv::Mat(Q1[idx]), cam_rvecs1[idx], cam_tvecs1[idx], cal.K1,  cal.k1, qc_proj);
424
            float err = 0;
424
            float err = 0;
425
            for(unsigned int j=0; j<qc_proj.size(); j++){
425
            for(unsigned int j=0; j<qc_proj.size(); j++){
426
                cv::Point2f d = qc1[idx][j] - qc_proj[j];
426
                cv::Point2f d = qc1[idx][j] - qc_proj[j];
427
                err += cv::sqrt(d.x*d.x + d.y*d.y);
427
                err += cv::sqrt(d.x*d.x + d.y*d.y);
428
            }
428
            }
429
            cal.cam1_errors_per_view[i] = (float)err/n;
429
            cal.cam1_errors_per_view[i] = (float)err/n;
430
            idx++;
430
            idx++;
431
       } else
431
       } else
432
            cal.cam1_errors_per_view[i] = NAN;
432
            cal.cam1_errors_per_view[i] = NAN;
433
    }
433
    }
434
 
434
 
435
//    // hand-eye calibration
435
//    // hand-eye calibration
436
//    std::vector<cv::Matx33f> Rc(nValidSets - 1); // rotations/translations of the checkerboard in camera 0 reference frame
436
//    std::vector<cv::Matx33f> Rc(nValidSets - 1); // rotations/translations of the checkerboard in camera 0 reference frame
437
//    std::vector<cv::Vec3f> Tc(nValidSets - 1);
437
//    std::vector<cv::Vec3f> Tc(nValidSets - 1);
438
//    std::vector<cv::Matx33f> Rr(nValidSets - 1); // in rotation stage reference frame
438
//    std::vector<cv::Matx33f> Rr(nValidSets - 1); // in rotation stage reference frame
439
//    std::vector<cv::Vec3f> Tr(nValidSets - 1);
439
//    std::vector<cv::Vec3f> Tr(nValidSets - 1);
440
//    for(int i=0; i<nValidSets-1; i++){
440
//    for(int i=0; i<nValidSets-1; i++){
441
//        // relative transformations in camera
441
//        // relative transformations in camera
442
//        cv::Mat cRw1, cRw2;
442
//        cv::Mat cRw1, cRw2;
443
//        cv::Rodrigues(cam_rvecs0[i], cRw1);
443
//        cv::Rodrigues(cam_rvecs0[i], cRw1);
444
//        cv::Rodrigues(cam_rvecs0[i+1], cRw2);
444
//        cv::Rodrigues(cam_rvecs0[i+1], cRw2);
445
//        cv::Mat cTw1 = cam_tvecs0[i];
445
//        cv::Mat cTw1 = cam_tvecs0[i];
446
//        cv::Mat cTw2 = cam_tvecs0[i+1];
446
//        cv::Mat cTw2 = cam_tvecs0[i+1];
447
//        cv::Mat w1Rc = cRw1.t();
447
//        cv::Mat w1Rc = cRw1.t();
448
//        cv::Mat w1Tc = -cRw1.t()*cTw1;
448
//        cv::Mat w1Tc = -cRw1.t()*cTw1;
449
//        Rc[i] = cv::Mat(cRw2*w1Rc);
449
//        Rc[i] = cv::Mat(cRw2*w1Rc);
450
//        Tc[i] = cv::Mat(cRw2*w1Tc+cTw2);
450
//        Tc[i] = cv::Mat(cRw2*w1Tc+cTw2);
451
 
451
 
452
//        // relative transformations in rotation stage
452
//        // relative transformations in rotation stage
453
//        // we define the rotation axis to be in origo, pointing in positive y direction
453
//        // we define the rotation axis to be in origo, pointing in positive y direction
454
//        float angleRadians = (angles[i+1]-angles[i])/180.0*M_PI;
454
//        float angleRadians = (angles[i+1]-angles[i])/180.0*M_PI;
455
//        cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
455
//        cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
456
//        cv::Mat Rri;
456
//        cv::Mat Rri;
457
//        cv::Rodrigues(rot_rvec, Rri);
457
//        cv::Rodrigues(rot_rvec, Rri);
458
//        Rr[i] = Rri;
458
//        Rr[i] = Rri;
459
//        Tr[i] = 0.0;
459
//        Tr[i] = 0.0;
460
 
460
 
461
////        std::cout << i << std::endl;
461
////        std::cout << i << std::endl;
462
////        std::cout << "cTw1" << cTw1 << std::endl;
462
////        std::cout << "cTw1" << cTw1 << std::endl;
463
////        std::cout << "cTw2" << cTw2 << std::endl;
463
////        std::cout << "cTw2" << cTw2 << std::endl;
464
////        std::cout << "w2Rc" << w2Rc << std::endl;
464
////        std::cout << "w2Rc" << w2Rc << std::endl;
465
////        std::cout << "w2Tc" << w2Tc << std::endl;
465
////        std::cout << "w2Tc" << w2Tc << std::endl;
466
 
466
 
467
////        std::cout << "w2Rc" << w2Rc << std::endl;
467
////        std::cout << "w2Rc" << w2Rc << std::endl;
468
////        std::cout << "w2Tc" << w2Tc << std::endl;
468
////        std::cout << "w2Tc" << w2Tc << std::endl;
469
 
469
 
470
////        cv::Mat Rci;
470
////        cv::Mat Rci;
471
////        cv::Rodrigues(Rc[i], Rci);
471
////        cv::Rodrigues(Rc[i], Rci);
472
////        std::cout << "Rci" << Rci << std::endl;
472
////        std::cout << "Rci" << Rci << std::endl;
473
////        std::cout << "Tc[i]" << Tc[i] << std::endl;
473
////        std::cout << "Tc[i]" << Tc[i] << std::endl;
474
 
474
 
475
////        std::cout << "rot_rvec" << rot_rvec << std::endl;
475
////        std::cout << "rot_rvec" << rot_rvec << std::endl;
476
////        std::cout << "Tr[i]" << Tr[i] << std::endl;
476
////        std::cout << "Tr[i]" << Tr[i] << std::endl;
477
////        std::cout << std::endl;
477
////        std::cout << std::endl;
478
//    }
478
//    }
479
 
479
 
480
//    // determine the transformation from rotation stage to camera 0
480
//    // determine the transformation from rotation stage to camera 0
481
//    cvtools::handEyeCalibrationTsai(Rc, Tc, Rr, Tr, cal.Rr, cal.Tr);
481
//    cvtools::handEyeCalibrationTsai(Rc, Tc, Rr, Tr, cal.Rr, cal.Tr);
482
 
482
 
483
//    for(int i=0; i<nValidSets-1; i++){
483
//    for(int i=0; i<nValidSets-1; i++){
484
//        std::cout << i << std::endl;
484
//        std::cout << i << std::endl;
485
 
485
 
486
//        cv::Mat Rci;
486
//        cv::Mat Rci;
487
//        cv::Rodrigues(Rc[i], Rci);
487
//        cv::Rodrigues(Rc[i], Rci);
488
//        std::cout << "Rc[i]" << Rci << std::endl;
488
//        std::cout << "Rc[i]" << Rci << std::endl;
489
//        std::cout << "Tc[i]" << Tc[i] << std::endl;
489
//        std::cout << "Tc[i]" << Tc[i] << std::endl;
490
 
490
 
491
//        cv::Mat Rri;
491
//        cv::Mat Rri;
492
//        cv::Rodrigues(Rr[i], Rri);
492
//        cv::Rodrigues(Rr[i], Rri);
493
//        std::cout << "Rr[i]" << Rri << std::endl;
493
//        std::cout << "Rr[i]" << Rri << std::endl;
494
//        std::cout << "Tr[i]" << Tr[i] << std::endl;
494
//        std::cout << "Tr[i]" << Tr[i] << std::endl;
495
 
495
 
496
//        cv::Mat Rcr = cv::Mat(cal.Rr)*cv::Mat(Rc[i])*cv::Mat(cal.Rr.t());
496
//        cv::Mat Rcr = cv::Mat(cal.Rr)*cv::Mat(Rc[i])*cv::Mat(cal.Rr.t());
497
//        cv::Rodrigues(Rcr, Rcr);
497
//        cv::Rodrigues(Rcr, Rcr);
498
//        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);
498
//        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);
499
//        std::cout << "Rcr[i]" << Rcr << std::endl;
499
//        std::cout << "Rcr[i]" << Rcr << std::endl;
500
//        std::cout << "Tcr[i]" << Tcr << std::endl;
500
//        std::cout << "Tcr[i]" << Tcr << std::endl;
501
//        std::cout << std::endl;
501
//        std::cout << std::endl;
502
//    }
502
//    }
503
 
503
 
504
 
504
 
505
    // Direct rotation axis calibration //
505
    // Direct rotation axis calibration //
506
    // full camera matrices
506
    // full camera matrices
507
    cv::Matx34f P0 = cv::Matx34f::eye();
507
    cv::Matx34f P0 = cv::Matx34f::eye();
508
    cv::Mat RT1(3, 4, CV_32F);
508
    cv::Mat RT1(3, 4, CV_32F);
509
    cv::Mat(cal.R1).copyTo(RT1(cv::Range(0, 3), cv::Range(0, 3)));
509
    cv::Mat(cal.R1).copyTo(RT1(cv::Range(0, 3), cv::Range(0, 3)));
510
    cv::Mat(cal.T1).copyTo(RT1(cv::Range(0, 3), cv::Range(3, 4)));
510
    cv::Mat(cal.T1).copyTo(RT1(cv::Range(0, 3), cv::Range(3, 4)));
511
    cv::Matx34f P1 = cv::Matx34f(RT1);
511
    cv::Matx34f P1 = cv::Matx34f(RT1);
512
 
512
 
513
    // calibration points in camera 0 frame
513
    // calibration points in camera 0 frame
514
    std::vector< std::vector<cv::Point3f> > Qcam;
514
    std::vector< std::vector<cv::Point3f> > Qcam;
515
 
515
 
516
    for(unsigned int i=0; i<nValidSets; i++){
516
    for(unsigned int i=0; i<nValidSets; i++){
517
        std::vector<cv::Point2f> qc0i, qc1i;
517
        std::vector<cv::Point2f> qc0i, qc1i;
518
 
518
 
519
        cv::undistortPoints(qc0Stereo[i], qc0i, cal.K0, cal.k0);
519
        cv::undistortPoints(qc0Stereo[i], qc0i, cal.K0, cal.k0);
520
        cv::undistortPoints(qc1Stereo[i], qc1i, cal.K1, cal.k1);
520
        cv::undistortPoints(qc1Stereo[i], qc1i, cal.K1, cal.k1);
521
//        qc0i = qc0[i];
521
//        qc0i = qc0[i];
522
//        qc1i = qc1[i];
522
//        qc1i = qc1[i];
523
 
523
 
524
        cv::Mat Qhom, Qcami;
524
        cv::Mat Qhom, Qcami;
525
        cv::triangulatePoints(P0, P1, qc0i, qc1i, Qhom);
525
        cv::triangulatePoints(P0, P1, qc0i, qc1i, Qhom);
526
        cvtools::convertMatFromHomogeneous(Qhom, Qcami);
526
        cvtools::convertMatFromHomogeneous(Qhom, Qcami);
527
        std::vector<cv::Point3f> QcamiPoints;
527
        std::vector<cv::Point3f> QcamiPoints;
528
        cvtools::matToPoints3f(Qcami, QcamiPoints);
528
        cvtools::matToPoints3f(Qcami, QcamiPoints);
529
 
529
 
530
        Qcam.push_back(QcamiPoints);
530
        Qcam.push_back(QcamiPoints);
531
    }
531
    }
532
 
532
 
533
//    cv::Mat QcamMat(Qcam.size(), Qcam[0].size(), CV_32FC3);
533
//    cv::Mat QcamMat(Qcam.size(), Qcam[0].size(), CV_32FC3);
534
//    for(int i=0; i<Qcam.size(); i++)
534
//    for(int i=0; i<Qcam.size(); i++)
535
//        for(int j=0; j<Qcam[0].size(); j++)
535
//        for(int j=0; j<Qcam[0].size(); j++)
536
//            QcamMat.at<cv::Point3f>(i,j) = Qcam[i][j];
536
//            QcamMat.at<cv::Point3f>(i,j) = Qcam[i][j];
537
//    cvtools::writeMat(QcamMat, "Qcam.mat", "Qcam");
537
//    cvtools::writeMat(QcamMat, "Qcam.mat", "Qcam");
538
 
538
 
539
    cv::Vec3f axis, point;
539
    cv::Vec3f axis, point;
540
    float rot_axis_error;
540
    float rot_axis_error;
541
    rotationAxisCalibration(Qcam, Qi, axis, point, rot_axis_error);
541
    rotationAxisCalibration(Qcam, Qi, axis, point, rot_axis_error);
542
 
542
 
543
    // construct transformation matrix
543
    // construct transformation matrix
544
    cv::Vec3f ex = axis.cross(cv::Vec3f(0,0,1.0));
544
    cv::Vec3f ex = axis.cross(cv::Vec3f(0,0,1.0));
545
    ex = cv::normalize(ex);
545
    ex = cv::normalize(ex);
546
    cv::Vec3f ez = ex.cross(axis);
546
    cv::Vec3f ez = ex.cross(axis);
547
    ez = cv::normalize(ez);
547
    ez = cv::normalize(ez);
548
 
548
 
549
    cv::Mat RrMat(3, 3, CV_32F);
549
    cv::Mat RrMat(3, 3, CV_32F);
550
    cv::Mat(ex).copyTo(RrMat.col(0));
550
    cv::Mat(ex).copyTo(RrMat.col(0));
551
    cv::Mat(axis).copyTo(RrMat.col(1));
551
    cv::Mat(axis).copyTo(RrMat.col(1));
552
    cv::Mat(ez).copyTo(RrMat.col(2));
552
    cv::Mat(ez).copyTo(RrMat.col(2));
553
 
553
 
554
    cal.Rr = cv::Matx33f(RrMat).t();
554
    cal.Rr = cv::Matx33f(RrMat).t();
555
    cal.Tr = -cv::Matx33f(RrMat).t()*point;
555
    cal.Tr = -cv::Matx33f(RrMat).t()*point;
556
    cal.rot_axis_error = rot_axis_error;
556
    cal.rot_axis_error = rot_axis_error;
557
 
557
 
558
    // Print to std::cout
558
    // Print to std::cout
559
    cal.print();
559
    cal.print();
560
 
560
 
561
    // save to (reentrant qsettings object)
561
    // save to (reentrant qsettings object)
562
    settings.setValue("calibration/parameters", QVariant::fromValue(cal));
562
    settings.setValue("calibration/parameters", QVariant::fromValue(cal));
563
 
563
 
564
    emit done();
564
    emit done();
565
 
565
 
566
}
566
}
567
 
567