Subversion Repositories seema-scanner

Rev

Rev 81 | Rev 86 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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