Subversion Repositories seema-scanner

Rev

Rev 31 | Rev 34 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 31 Rev 33
1
#include "SMCalibrationWorker.h"
1
#include "SMCalibrationWorker.h"
2
#include "SMCalibrationParameters.h"
2
#include "SMCalibrationParameters.h"
3
 
3
 
4
#include "cvtools.h"
4
#include "cvtools.h"
5
 
5
 
6
#include <QSettings>
6
#include <QSettings>
7
 
7
 
8
void SMCalibrationWorker::performCalibration(std::vector<SMCalibrationSet> calibrationData){
8
void SMCalibrationWorker::performCalibration(std::vector<SMCalibrationSet> calibrationData){
9
 
9
 
-
 
10
    QSettings settings;
-
 
11
 
10
    // Number of saddle points on calibration pattern
12
    // Number of saddle points on calibration pattern
-
 
13
    int checkerCountX = settings.value("calibration/checkerCountX", 10).toInt();
-
 
14
    int checkerCountY = settings.value("calibration/checkerCountY", 9).toInt();
11
    cv::Size patternSize(10, 9);
15
    cv::Size checkerCount(checkerCountX, checkerCountY);
12
 
16
 
13
    int nSets = calibrationData.size();
17
    int nSets = calibrationData.size();
14
 
18
 
15
    std::vector< std::vector<cv::Point2f> > qc0, qc1;
19
    std::vector< std::vector<cv::Point2f> > qc0, qc1;
16
    std::vector<float> angles;
20
    std::vector<float> angles;
17
 
21
 
18
    // Loop through calibration sets
22
    // Loop through calibration sets
19
    for(int i=0; i<nSets; i++){
23
    for(int i=0; i<nSets; i++){
20
 
24
 
21
        SMCalibrationSet SMCalibrationSetI = calibrationData[i];
25
        SMCalibrationSet SMCalibrationSetI = calibrationData[i];
22
 
26
 
23
        if(!SMCalibrationSetI.checked)
27
        if(!SMCalibrationSetI.checked)
24
            continue;
28
            continue;
25
 
29
 
26
        // Camera 0
30
        // Camera 0
27
        std::vector<cv::Point2f> qci0;
31
        std::vector<cv::Point2f> qci0;
28
        // Extract checker corners
32
        // Extract checker corners
29
        bool success0 = cv::findChessboardCorners(SMCalibrationSetI.frame0, patternSize, qci0, cv::CALIB_CB_ADAPTIVE_THRESH);
33
        bool success0 = cv::findChessboardCorners(SMCalibrationSetI.frame0, checkerCount, qci0, cv::CALIB_CB_ADAPTIVE_THRESH);
30
        if(success0){
34
        if(success0){
31
            cv::Mat gray;
35
            cv::Mat gray;
32
            cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_RGB2GRAY);
36
            cv::cvtColor(SMCalibrationSetI.frame0, gray, CV_RGB2GRAY);
33
            cv::cornerSubPix(gray, qci0, cv::Size(5, 5), cv::Size(-1, -1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.001));
37
            cv::cornerSubPix(gray, qci0, cv::Size(5, 5), cv::Size(-1, -1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.001));
34
            // Draw colored chessboard
38
            // Draw colored chessboard
35
            SMCalibrationSetI.frame0Result = SMCalibrationSetI.frame0.clone();
39
            SMCalibrationSetI.frame0Result = SMCalibrationSetI.frame0.clone();
36
            cv::drawChessboardCorners(SMCalibrationSetI.frame0Result, patternSize, qci0, success0);
40
            cv::drawChessboardCorners(SMCalibrationSetI.frame0Result, checkerCount, qci0, success0);
37
        }
41
        }
38
 
42
 
39
        emit newFrameResult(i, 0, success0, SMCalibrationSetI.frame0Result);
43
        emit newFrameResult(i, 0, success0, SMCalibrationSetI.frame0Result);
40
 
44
 
41
        // Camera 1
45
        // Camera 1
42
        std::vector<cv::Point2f> qci1;
46
        std::vector<cv::Point2f> qci1;
43
        // Extract checker corners
47
        // Extract checker corners
44
        bool success1 = cv::findChessboardCorners(SMCalibrationSetI.frame1, patternSize, qci1, cv::CALIB_CB_ADAPTIVE_THRESH);
48
        bool success1 = cv::findChessboardCorners(SMCalibrationSetI.frame1, checkerCount, qci1, cv::CALIB_CB_ADAPTIVE_THRESH);
45
        if(success1){
49
        if(success1){
46
            cv::Mat gray;
50
            cv::Mat gray;
47
            cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_RGB2GRAY);
51
            cv::cvtColor(SMCalibrationSetI.frame1, gray, CV_RGB2GRAY);
48
            cv::cornerSubPix(gray, qci1, cv::Size(5, 5), cv::Size(-1, -1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.001));
52
            cv::cornerSubPix(gray, qci1, cv::Size(5, 5), cv::Size(-1, -1),cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 20, 0.001));
49
            // Draw colored chessboard
53
            // Draw colored chessboard
50
            SMCalibrationSetI.frame1Result = SMCalibrationSetI.frame1.clone();
54
            SMCalibrationSetI.frame1Result = SMCalibrationSetI.frame1.clone();
51
            cv::drawChessboardCorners(SMCalibrationSetI.frame1Result, patternSize, qci1, success1);
55
            cv::drawChessboardCorners(SMCalibrationSetI.frame1Result, checkerCount, qci1, success1);
52
        }
56
        }
53
 
57
 
54
        emit newFrameResult(i, 1, success1, SMCalibrationSetI.frame1Result);
58
        emit newFrameResult(i, 1, success1, SMCalibrationSetI.frame1Result);
55
 
59
 
56
        SMCalibrationSetI.success = success0 && success1;
60
        SMCalibrationSetI.success = success0 && success1;
57
 
61
 
58
        // Add to whole set
62
        // Add to whole set
59
        if(SMCalibrationSetI.success){
63
        if(SMCalibrationSetI.success){
60
            qc0.push_back(qci0);
64
            qc0.push_back(qci0);
61
            qc1.push_back(qci1);
65
            qc1.push_back(qci1);
62
            angles.push_back(SMCalibrationSetI.rotationAngle);
66
            angles.push_back(SMCalibrationSetI.rotationAngle);
63
        }
67
        }
64
 
68
 
65
        // Show progress
69
        // Show progress
66
        emit newSetProcessed(i);
70
        emit newSetProcessed(i);
67
    }
71
    }
68
 
72
 
69
    int nValidSets = qc0.size();
73
    int nValidSets = qc0.size();
70
    if(nValidSets < 2){
74
    if(nValidSets < 2){
71
        std::cerr << "Not enough valid calibration sequences!" << std::endl;
75
        std::cerr << "Not enough valid calibration sequences!" << std::endl;
72
        emit done();
76
        emit done();
73
        return;
77
        return;
74
    }
78
    }
75
 
79
 
76
    // Generate world object coordinates [mm]
80
    // Generate world object coordinates [mm]
-
 
81
    float checkerSize = settings.value("calibration/checkerSize", 15.0).toFloat(); // width and height of one field in mm
77
    std::vector<cv::Point3f> Qi;
82
    std::vector<cv::Point3f> Qi;
78
    for (int h=0; h<patternSize.height; h++)
83
    for (int h=0; h<checkerCount.height; h++)
79
        for (int w=0; w<patternSize.width; w++)
84
        for (int w=0; w<checkerCount.width; w++)
80
            Qi.push_back(cv::Point3f(5 * w, 5* h, 0.0)); // 5mm chess field size
85
            Qi.push_back(cv::Point3f(checkerSize * w, checkerSize* h, 0.0));
81
    std::vector< std::vector<cv::Point3f> > Q;
86
    std::vector< std::vector<cv::Point3f> > Q;
82
    for(int i=0; i<qc0.size(); i++)
87
    for(int i=0; i<qc0.size(); i++)
83
        Q.push_back(Qi);
88
        Q.push_back(Qi);
84
 
89
 
85
    // calibrate the cameras
90
    // calibrate the cameras
86
    SMCalibrationParameters cal;
91
    SMCalibrationParameters cal;
87
    cal.frameWidth = calibrationData[0].frame0.cols;
92
    cal.frameWidth = calibrationData[0].frame0.cols;
88
    cal.frameHeight = calibrationData[0].frame0.rows;
93
    cal.frameHeight = calibrationData[0].frame0.rows;
89
    cv::Size frameSize(cal.frameWidth, cal.frameHeight);
94
    cv::Size frameSize(cal.frameWidth, cal.frameHeight);
90
 
95
 
91
    int flags = 0; //cv::CALIB_FIX_K3;
96
    int flags = 0; //cv::CALIB_FIX_K3;
92
 
97
 
-
 
98
    // Note: several of the output arguments below must be cv::Mat, otherwise segfault
93
    std::vector<cv::Vec3f> cam_rvecs0, cam_tvecs0;
99
    std::vector<cv::Mat> cam_rvecs0, cam_tvecs0;
94
    cal.cam0_error = cv::calibrateCamera(Q, qc0, frameSize, cal.K0, cal.k0, cam_rvecs0, cam_tvecs0, flags);
100
    cal.cam0_error = cv::calibrateCamera(Q, qc0, frameSize, cal.K0, cal.k0, cam_rvecs0, cam_tvecs0, flags);
95
 
101
 
96
    std::vector<cv::Vec3f> cam_rvecs1, cam_tvecs1;
102
    std::vector<cv::Mat> cam_rvecs1, cam_tvecs1;
97
    cal.cam1_error = cv::calibrateCamera(Q, qc1, frameSize, cal.K1, cal.k1, cam_rvecs1, cam_tvecs1, flags);
103
    cal.cam1_error = cv::calibrateCamera(Q, qc1, frameSize, cal.K1, cal.k1, cam_rvecs1, cam_tvecs1, flags);
98
 
104
 
99
    // stereo calibration (fix K0, K1, k0, k1)
105
    // stereo calibration (fix K0, K1, k0, k1)
100
    int flags_stereo = cv::CALIB_FIX_INTRINSIC;
106
    int flags_stereo = cv::CALIB_FIX_INTRINSIC;
-
 
107
    cv::Mat E, F, R1, T1;
101
    cal.stereo_error = cv::stereoCalibrate(Q, qc0, qc1, cal.K0, cal.k0, cal.K1, cal.k1,
108
    cal.stereo_error = cv::stereoCalibrate(Q, qc0, qc1, cal.K0, cal.k0, cal.K1, cal.k1,
102
                                              frameSize, cal.R1, cal.T1, cal.E, cal.F,
109
                                              frameSize, R1, T1, E, F,
103
                                              cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 50, DBL_EPSILON),
110
                                              cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 50, DBL_EPSILON),
104
                                              flags_stereo);
111
                                              flags_stereo);
105
 
112
 
-
 
113
    cal.R1 = R1;
-
 
114
    cal.T1 = T1;
-
 
115
    cal.E = E;
-
 
116
    cal.F = F;
-
 
117
 
106
    // calibrate rotation axis
118
    // calibrate rotation axis
107
    std::vector<cv::Matx33f> Rc(nValidSets - 1); // rotations/translations of the checkerboard in camera 0 reference frame
119
    std::vector<cv::Matx33f> Rc(nValidSets - 1); // rotations/translations of the checkerboard in camera 0 reference frame
108
    std::vector<cv::Vec3f> Tc(nValidSets - 1);
120
    std::vector<cv::Vec3f> Tc(nValidSets - 1);
109
    std::vector<cv::Matx33f> Rr(nValidSets - 1); // in rotation stage reference frame
121
    std::vector<cv::Matx33f> Rr(nValidSets - 1); // in rotation stage reference frame
110
    std::vector<cv::Vec3f> Tr(nValidSets - 1);
122
    std::vector<cv::Vec3f> Tr(nValidSets - 1);
111
    for(int i=0; i<nValidSets-1; i++){
123
    for(int i=0; i<nValidSets-1; i++){
112
        // relative transformations in camera
124
        // relative transformations in camera
113
        cv::Matx33f cRw1, cRw2;
125
        cv::Mat cRw1, cRw2;
114
        cv::Rodrigues(cam_rvecs0[i], cRw1);
126
        cv::Rodrigues(cam_rvecs0[i], cRw1);
115
        cv::Rodrigues(cam_rvecs0[i+1], cRw2);
127
        cv::Rodrigues(cam_rvecs0[i+1], cRw2);
116
        cv::Vec3f cTw1 = cam_tvecs0[i];
128
        cv::Mat cTw1 = cam_tvecs0[i];
117
        cv::Vec3f cTw2 = cam_tvecs0[i+1];
129
        cv::Mat cTw2 = cam_tvecs0[i+1];
118
 
-
 
-
 
130
        cv::Mat w1Rc = cRw1.t();
119
        cv::composeRT(cRw1, cTw1, cRw2.t(), -cRw2.t()*cTw2, Rc[i], Tc[i]);
131
        cv::Mat w1Tc = -cRw1.t()*cTw1;
-
 
132
        Rc[i] = cv::Mat(cRw2*w1Rc);
-
 
133
        Tc[i] = cv::Mat(cRw2*w1Tc+cTw2);
120
 
134
 
121
        // relative transformations in rotation stage
135
        // relative transformations in rotation stage
122
        // we define the rotation axis to be in origo, pointing in positive y direction
136
        // we define the rotation axis to be in origo, pointing in positive y direction
123
        float angleRadians = angles[i]/180.0*M_PI;
137
        float angleRadians = (angles[i+1]-angles[i])/180.0*M_PI;
124
        cv::Vec3f rot_rvec(0.0, angleRadians, 0.0);
138
        cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
-
 
139
        cv::Mat Rri;
125
        cv::Rodrigues(rot_rvec, Rr[i]);
140
        cv::Rodrigues(rot_rvec, Rri);
-
 
141
        Rr[i] = Rri;
126
        Tr[i] = 0.0;
142
        Tr[i] = 0.0;
-
 
143
 
-
 
144
        std::cout << i << std::endl;
-
 
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
 
-
 
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;
127
    }
161
    }
128
 
162
 
-
 
163
    // determine the transformation from rotation stage to camera 0
129
    cvtools::fitSixDofData(Rc, Tc, Rr, Tr, cal.Rr, cal.Tr);
164
    cvtools::fitSixDofData(Rr, Tr, Rc, Tc, cal.Rr, cal.Tr);
-
 
165
 
-
 
166
    for(int i=0; i<Rc.size(); i++){
-
 
167
 
-
 
168
//        cv::Matx33f a = cal.Rr*Rc[i];
-
 
169
//        cv::Matx33f b = Rr[i];
-
 
170
 
-
 
171
//        std::cout << "a:" << a << std::endl;
-
 
172
//        std::cout << "b:" << b << std::endl;
-
 
173
 
-
 
174
        cv::Vec3f a = cal.Rr*Tr[i]+cal.Tr;
-
 
175
        cv::Vec3f b = Tc[i];
-
 
176
 
-
 
177
        std::cout << "a:" << a << std::endl;
-
 
178
        std::cout << "b:" << b << std::endl;
-
 
179
    }
-
 
180
    cv::Mat rrvec;
-
 
181
    cv::Rodrigues(cal.Rr, rrvec);
-
 
182
    std::cout << "rrvec:" << rrvec << std::endl;
130
 
183
 
131
    // Print to std::cout
184
    // Print to std::cout
132
    cal.print();
185
    cal.print();
133
 
186
 
134
    // save to (reentrant qsettings object)
187
    // save to (reentrant qsettings object)
135
    QSettings settings;
-
 
136
    settings.setValue("calibration", QVariant::fromValue(cal));
188
    settings.setValue("calibration/parameters", QVariant::fromValue(cal));
137
 
189
 
138
    emit done();
190
    emit done();
139
 
191
 
140
}
192
}
141
 
193