Subversion Repositories seema-scanner

Rev

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

Rev 119 Rev 120
1
#include "cvtools.h"
1
#include "cvtools.h"
2
 
2
 
3
#ifdef _WIN32
3
#ifdef _WIN32
4
#include <cstdint>
4
#include <cstdint>
5
#endif
5
#endif
6
 
6
 
7
#include <stdio.h>
7
#include <stdio.h>
8
 
8
 
9
namespace cvtools{
9
namespace cvtools{
10
 
10
 
11
// Create a mask for feature matching which disallows matches not satisfying the epipolar constraint.
11
// Create a mask for feature matching which disallows matches not satisfying the epipolar constraint.
12
// Works like cv::windowedMatchingMask in conjunction with cv::BFMatcher::match().
12
// Works like cv::windowedMatchingMask in conjunction with cv::BFMatcher::match().
13
// F is the fundamental matrix.
13
// F is the fundamental matrix.
14
// maxD is the maximum point to line distance permissible.
14
// maxD is the maximum point to line distance permissible.
15
cv::Mat epipolarMatchingMask(const cv::vector<cv::KeyPoint> &keypoints1, const cv::vector<cv::KeyPoint> &keypoints2, cv::Matx33f F, float maxD){
15
cv::Mat epipolarMatchingMask(const cv::vector<cv::KeyPoint> &keypoints1, const cv::vector<cv::KeyPoint> &keypoints2, cv::Matx33f F, float maxD){
16
 
16
 
17
    if(keypoints1.empty() || keypoints2.empty())
17
    if(keypoints1.empty() || keypoints2.empty())
18
        return cv::Mat();
18
        return cv::Mat();
19
 
19
 
20
    int n1 = (int)keypoints1.size(), n2 = (int)keypoints2.size();
20
    int n1 = (int)keypoints1.size(), n2 = (int)keypoints2.size();
21
    cv::Mat mask(n1, n2, CV_8UC1);
21
    cv::Mat mask(n1, n2, CV_8UC1);
22
 
22
 
23
    // Point to line distance
23
    // Point to line distance
24
//    for( int i = 0; i < n1; i++ ){
24
//    for( int i = 0; i < n1; i++ ){
25
//        cv::Vec3f p1 = cv::Vec3f(keypoints1[i].pt.x, keypoints1[i].pt.y, 1.0);
25
//        cv::Vec3f p1 = cv::Vec3f(keypoints1[i].pt.x, keypoints1[i].pt.y, 1.0);
26
//        // Epipolar line defined by p1
26
//        // Epipolar line defined by p1
27
//        cv::Vec3f l = F*p1;
27
//        cv::Vec3f l = F*p1;
28
//        l /= sqrt(l(0)*l(0) + l(1)*l(1));
28
//        l /= sqrt(l(0)*l(0) + l(1)*l(1));
29
//        for( int j = 0; j < n2; j++ ){
29
//        for( int j = 0; j < n2; j++ ){
30
//            cv::Vec3f p2 = cv::Vec3f(keypoints2[j].pt.x, keypoints2[j].pt.y, 1.0);
30
//            cv::Vec3f p2 = cv::Vec3f(keypoints2[j].pt.x, keypoints2[j].pt.y, 1.0);
31
//            // Signed distance to line
31
//            // Signed distance to line
32
//            float d = l.dot(p2);
32
//            float d = l.dot(p2);
33
//            mask.at<uchar>(i, j) = fabs(d) < maxD;
33
//            mask.at<uchar>(i, j) = fabs(d) < maxD;
34
//        }
34
//        }
35
//    }
35
//    }
36
 
36
 
37
    // Symmetric epipolar distance
37
    // Symmetric epipolar distance
38
    std::vector<cv::Point2f> q1, q2;
38
    std::vector<cv::Point2f> q1, q2;
39
    cvtools::keypointsToPoints(keypoints1, q1);
39
    cvtools::keypointsToPoints(keypoints1, q1);
40
    cvtools::keypointsToPoints(keypoints2, q2);
40
    cvtools::keypointsToPoints(keypoints2, q2);
41
    std::vector<cv::Point3f> l1, l2;
41
    std::vector<cv::Point3f> l1, l2;
42
    cv::computeCorrespondEpilines(q1, 1, F, l1);
42
    cv::computeCorrespondEpilines(q1, 1, F, l1);
43
    cv::computeCorrespondEpilines(q2, 2, F, l2);
43
    cv::computeCorrespondEpilines(q2, 2, F, l2);
44
 
44
 
45
    for( int i = 0; i < n1; i++ ){
45
    for( int i = 0; i < n1; i++ ){
46
        cv::Vec3f p1 = cv::Vec3f(q1[i].x, q1[i].y, 1.0);
46
        cv::Vec3f p1 = cv::Vec3f(q1[i].x, q1[i].y, 1.0);
47
        for( int j = 0; j < n2; j++ ){
47
        for( int j = 0; j < n2; j++ ){
48
            cv::Vec3f p2 = cv::Vec3f(q2[j].x, q2[j].y, 1.0);
48
            cv::Vec3f p2 = cv::Vec3f(q2[j].x, q2[j].y, 1.0);
49
            float d12 = l1[i].dot(p2);
49
            float d12 = l1[i].dot(p2);
50
            float d21 = l2[j].dot(p1);
50
            float d21 = l2[j].dot(p1);
51
            float d = d12*d12 + d21*d21;
51
            float d = d12*d12 + d21*d21;
52
            mask.at<uchar>(i, j) = d < maxD;
52
            mask.at<uchar>(i, j) = d < maxD;
53
        }
53
        }
54
    }
54
    }
55
 
55
 
56
//    // Sampson Error (H&Z, p. 287) (expensive...)
56
//    // Sampson Error (H&Z, p. 287) (expensive...)
57
//    std::vector<cv::Point2f> q1, q2;
57
//    std::vector<cv::Point2f> q1, q2;
58
//    cvtools::keypointsToPoints(keypoints1, q1);
58
//    cvtools::keypointsToPoints(keypoints1, q1);
59
//    cvtools::keypointsToPoints(keypoints2, q2);
59
//    cvtools::keypointsToPoints(keypoints2, q2);
60
//    std::vector<cv::Point3f> p1, p2;
60
//    std::vector<cv::Point3f> p1, p2;
61
//    cv::convertPointsToHomogeneous(q1, p1);
61
//    cv::convertPointsToHomogeneous(q1, p1);
62
//    cv::convertPointsToHomogeneous(q2, p2);
62
//    cv::convertPointsToHomogeneous(q2, p2);
63
//    cv::Mat Fp1Mat = cv::Mat(F)*cv::Mat(p1).reshape(1).t();
63
//    cv::Mat Fp1Mat = cv::Mat(F)*cv::Mat(p1).reshape(1).t();
64
//    cv::Mat FTp2Mat = cv::Mat(F.t())*cv::Mat(p2).reshape(1).t();
64
//    cv::Mat FTp2Mat = cv::Mat(F.t())*cv::Mat(p2).reshape(1).t();
65
//    for( int i = 0; i < n1; i++ ){
65
//    for( int i = 0; i < n1; i++ ){
66
//        cv::Vec3f Fp1 = Fp1Mat.col(i);
66
//        cv::Vec3f Fp1 = Fp1Mat.col(i);
67
//        for( int j = 0; j < n2; j++ ){
67
//        for( int j = 0; j < n2; j++ ){
68
//            cv::Vec3f FTp2 = FTp2Mat.col(j);
68
//            cv::Vec3f FTp2 = FTp2Mat.col(j);
69
//            cv::Matx<float,1,1> p2TFp1 = cv::Matx31f(p2[j]).t()*F*cv::Matx31f(p1[i]);
69
//            cv::Matx<float,1,1> p2TFp1 = cv::Matx31f(p2[j]).t()*F*cv::Matx31f(p1[i]);
70
//            float d = p2TFp1(0)*p2TFp1(0) / (Fp1(0)*Fp1(0) + Fp1(1)*Fp1(1) + FTp2(0)*FTp2(0) + FTp2(1)*FTp2(1));
70
//            float d = p2TFp1(0)*p2TFp1(0) / (Fp1(0)*Fp1(0) + Fp1(1)*Fp1(1) + FTp2(0)*FTp2(0) + FTp2(1)*FTp2(1));
71
//            mask.at<uchar>(i, j) = d < maxD;
71
//            mask.at<uchar>(i, j) = d < maxD;
72
//        }
72
//        }
73
//    }
73
//    }
74
 
74
 
75
    return mask;
75
    return mask;
76
}
76
}
77
 
77
 
78
 
78
 
79
// Remove correspondences which have a distance metric above thresh.
79
// Remove correspondences which have a distance metric above thresh.
80
void matchingThreshold(const std::vector<cv::DMatch> &matchesIn, std::vector<cv::DMatch> &matchesOut, float thresh){
80
void matchingThreshold(const std::vector<cv::DMatch> &matchesIn, std::vector<cv::DMatch> &matchesOut, float thresh){
81
 
81
 
82
    int nMatches = matchesIn.size();
82
    int nMatches = matchesIn.size();
83
    matchesOut.clear();
83
    matchesOut.clear();
84
    matchesOut.reserve(nMatches);
84
    matchesOut.reserve(nMatches);
85
 
85
 
86
    for(int i=0; i<nMatches; i++){
86
    for(int i=0; i<nMatches; i++){
87
        if(matchesIn[i].distance < thresh)
87
        if(matchesIn[i].distance < thresh)
88
            matchesOut.push_back(matchesIn[i]);
88
            matchesOut.push_back(matchesIn[i]);
89
    }
89
    }
90
 
90
 
91
}
91
}
92
 
92
 
93
// Lightly modified OpenCV function which accepts a line width argument
93
// Lightly modified OpenCV function which accepts a line width argument
94
void drawChessboardCorners(cv::InputOutputArray _image, cv::Size patternSize, cv::InputArray _corners, bool patternWasFound, int line_width){
94
void drawChessboardCorners(cv::InputOutputArray _image, cv::Size patternSize, cv::InputArray _corners, bool patternWasFound, int line_width){
95
    cv::Mat corners = _corners.getMat();
95
    cv::Mat corners = _corners.getMat();
96
    if( corners.empty() )
96
    if( corners.empty() )
97
        return;
97
        return;
98
    cv::Mat image = _image.getMat(); CvMat c_image = _image.getMat();
98
    cv::Mat image = _image.getMat(); CvMat c_image = _image.getMat();
99
    int nelems = corners.checkVector(2, CV_32F, true);
99
    int nelems = corners.checkVector(2, CV_32F, true);
100
    CV_Assert(nelems >= 0);
100
    CV_Assert(nelems >= 0);
101
    cvDrawChessboardCorners( &c_image, patternSize, (CvPoint2D32f*)corners.data,
101
    cvDrawChessboardCorners( &c_image, patternSize, (CvPoint2D32f*)corners.data,
102
                             nelems, patternWasFound, line_width);
102
                             nelems, patternWasFound, line_width);
103
}
103
}
104
 
104
 
-
 
105
void rshift(cv::Mat& I, unsigned int shift){
-
 
106
 
-
 
107
    int nRows = I.rows;
-
 
108
    int nCols = I.cols;
-
 
109
 
-
 
110
    if (I.isContinuous()){
-
 
111
        nCols *= nRows;
-
 
112
        nRows = 1;
-
 
113
    }
-
 
114
 
-
 
115
    int i,j;
-
 
116
    unsigned short* p;
-
 
117
    for( i = 0; i < nRows; ++i){
-
 
118
        p = I.ptr<unsigned short>(i);
-
 
119
        for ( j = 0; j < nCols; ++j){
-
 
120
            p[j] = p[j]>>shift;
-
 
121
        }
-
 
122
    }
-
 
123
}
-
 
124
 
105
void cvDrawChessboardCorners(CvArr* _image, CvSize pattern_size, CvPoint2D32f* corners, int count, int found, int line_width){
125
void cvDrawChessboardCorners(CvArr* _image, CvSize pattern_size, CvPoint2D32f* corners, int count, int found, int line_width){
106
    const int shift = 0;
126
    const int shift = 0;
107
    const int radius = 12;
127
    const int radius = 12;
108
    const int r = radius*(1 << shift);
128
    const int r = radius*(1 << shift);
109
    int i;
129
    int i;
110
    CvMat stub, *image;
130
    CvMat stub, *image;
111
    double scale = 1;
131
    double scale = 1;
112
    int type, cn, line_type;
132
    int type, cn, line_type;
113
 
133
 
114
    image = cvGetMat( _image, &stub );
134
    image = cvGetMat( _image, &stub );
115
 
135
 
116
    type = CV_MAT_TYPE(image->type);
136
    type = CV_MAT_TYPE(image->type);
117
    cn = CV_MAT_CN(type);
137
    cn = CV_MAT_CN(type);
118
    if( cn != 1 && cn != 3 && cn != 4 )
138
    if( cn != 1 && cn != 3 && cn != 4 )
119
        CV_Error( CV_StsUnsupportedFormat, "Number of channels must be 1, 3 or 4" );
139
        CV_Error( CV_StsUnsupportedFormat, "Number of channels must be 1, 3 or 4" );
120
 
140
 
121
    switch( CV_MAT_DEPTH(image->type) )
141
    switch( CV_MAT_DEPTH(image->type) )
122
    {
142
    {
123
    case CV_8U:
143
    case CV_8U:
124
        scale = 1;
144
        scale = 1;
125
        break;
145
        break;
126
    case CV_16U:
146
    case CV_16U:
127
        scale = 256;
147
        scale = 256;
128
        break;
148
        break;
129
    case CV_32F:
149
    case CV_32F:
130
        scale = 1./255;
150
        scale = 1./255;
131
        break;
151
        break;
132
    default:
152
    default:
133
        CV_Error( CV_StsUnsupportedFormat,
153
        CV_Error( CV_StsUnsupportedFormat,
134
            "Only 8-bit, 16-bit or floating-point 32-bit images are supported" );
154
            "Only 8-bit, 16-bit or floating-point 32-bit images are supported" );
135
    }
155
    }
136
 
156
 
137
    line_type = type == CV_8UC1 || type == CV_8UC3 ? CV_AA : 8;
157
    line_type = type == CV_8UC1 || type == CV_8UC3 ? CV_AA : 8;
138
 
158
 
139
    if( !found )
159
    if( !found )
140
    {
160
    {
141
        CvScalar color = {{0,0,255}};
161
        CvScalar color = {{0,0,255}};
142
        if( cn == 1 )
162
        if( cn == 1 )
143
            color = cvScalarAll(200);
163
            color = cvScalarAll(200);
144
        color.val[0] *= scale;
164
        color.val[0] *= scale;
145
        color.val[1] *= scale;
165
        color.val[1] *= scale;
146
        color.val[2] *= scale;
166
        color.val[2] *= scale;
147
        color.val[3] *= scale;
167
        color.val[3] *= scale;
148
 
168
 
149
        for( i = 0; i < count; i++ )
169
        for( i = 0; i < count; i++ )
150
        {
170
        {
151
            CvPoint pt;
171
            CvPoint pt;
152
            pt.x = cvRound(corners[i].x*(1 << shift));
172
            pt.x = cvRound(corners[i].x*(1 << shift));
153
            pt.y = cvRound(corners[i].y*(1 << shift));
173
            pt.y = cvRound(corners[i].y*(1 << shift));
154
            cvLine( image, cvPoint( pt.x - r, pt.y - r ),
174
            cvLine( image, cvPoint( pt.x - r, pt.y - r ),
155
                    cvPoint( pt.x + r, pt.y + r ), color, line_width, line_type, shift );
175
                    cvPoint( pt.x + r, pt.y + r ), color, line_width, line_type, shift );
156
            cvLine( image, cvPoint( pt.x - r, pt.y + r),
176
            cvLine( image, cvPoint( pt.x - r, pt.y + r),
157
                    cvPoint( pt.x + r, pt.y - r), color, line_width, line_type, shift );
177
                    cvPoint( pt.x + r, pt.y - r), color, line_width, line_type, shift );
158
            cvCircle( image, pt, r+(1<<shift), color, line_width, line_type, shift );
178
            cvCircle( image, pt, r+(1<<shift), color, line_width, line_type, shift );
159
        }
179
        }
160
    }
180
    }
161
    else
181
    else
162
    {
182
    {
163
        int x, y;
183
        int x, y;
164
        CvPoint prev_pt = {0, 0};
184
        CvPoint prev_pt = {0, 0};
165
        const int line_max = 7;
185
        const int line_max = 7;
166
        static const CvScalar line_colors[line_max] =
186
        static const CvScalar line_colors[line_max] =
167
        {
187
        {
168
            {{0,0,255}},
188
            {{0,0,255}},
169
            {{0,128,255}},
189
            {{0,128,255}},
170
            {{0,200,200}},
190
            {{0,200,200}},
171
            {{0,255,0}},
191
            {{0,255,0}},
172
            {{200,200,0}},
192
            {{200,200,0}},
173
            {{255,0,0}},
193
            {{255,0,0}},
174
            {{255,0,255}}
194
            {{255,0,255}}
175
        };
195
        };
176
 
196
 
177
        for( y = 0, i = 0; y < pattern_size.height; y++ )
197
        for( y = 0, i = 0; y < pattern_size.height; y++ )
178
        {
198
        {
179
            CvScalar color = line_colors[y % line_max];
199
            CvScalar color = line_colors[y % line_max];
180
            if( cn == 1 )
200
            if( cn == 1 )
181
                color = cvScalarAll(200);
201
                color = cvScalarAll(200);
182
            color.val[0] *= scale;
202
            color.val[0] *= scale;
183
            color.val[1] *= scale;
203
            color.val[1] *= scale;
184
            color.val[2] *= scale;
204
            color.val[2] *= scale;
185
            color.val[3] *= scale;
205
            color.val[3] *= scale;
186
 
206
 
187
            for( x = 0; x < pattern_size.width; x++, i++ )
207
            for( x = 0; x < pattern_size.width; x++, i++ )
188
            {
208
            {
189
                CvPoint pt;
209
                CvPoint pt;
190
                pt.x = cvRound(corners[i].x*(1 << shift));
210
                pt.x = cvRound(corners[i].x*(1 << shift));
191
                pt.y = cvRound(corners[i].y*(1 << shift));
211
                pt.y = cvRound(corners[i].y*(1 << shift));
192
 
212
 
193
                if( i != 0 )
213
                if( i != 0 )
194
                    cvLine( image, prev_pt, pt, color, 1, line_type, shift );
214
                    cvLine( image, prev_pt, pt, color, 1, line_type, shift );
195
 
215
 
196
                cvLine( image, cvPoint(pt.x - r, pt.y - r),
216
                cvLine( image, cvPoint(pt.x - r, pt.y - r),
197
                        cvPoint(pt.x + r, pt.y + r), color, line_width, line_type, shift );
217
                        cvPoint(pt.x + r, pt.y + r), color, line_width, line_type, shift );
198
                cvLine( image, cvPoint(pt.x - r, pt.y + r),
218
                cvLine( image, cvPoint(pt.x - r, pt.y + r),
199
                        cvPoint(pt.x + r, pt.y - r), color, line_width, line_type, shift );
219
                        cvPoint(pt.x + r, pt.y - r), color, line_width, line_type, shift );
200
                cvCircle( image, pt, r+(1<<shift), color, line_width, line_type, shift );
220
                cvCircle( image, pt, r+(1<<shift), color, line_width, line_type, shift );
201
                prev_pt = pt;
221
                prev_pt = pt;
202
            }
222
            }
203
        }
223
        }
204
    }
224
    }
205
}
225
}
206
 
226
 
207
// Returns the result of mod(mat(x,y), moduli) for each matrix element
227
// Returns the result of mod(mat(x,y), moduli) for each matrix element
208
cv::Mat modulo(const cv::Mat &mat, float n){
228
cv::Mat modulo(const cv::Mat &mat, float n){
209
 
229
 
210
    cv::Mat ret(mat.rows, mat.cols, mat.type());
230
    cv::Mat ret(mat.rows, mat.cols, mat.type());
211
 
231
 
212
    for(int row=0; row<ret.rows; row++){
232
    for(int row=0; row<ret.rows; row++){
213
        for(int col=0; col<ret.cols; col++){
233
        for(int col=0; col<ret.cols; col++){
214
            float val = mat.at<float>(row, col);
234
            float val = mat.at<float>(row, col);
215
            // note: std::fmod calculates the remainder, not arithmetic modulo
235
            // note: std::fmod calculates the remainder, not arithmetic modulo
216
            ret.at<float>(row, col) = val - n * std::floor(val / n);
236
            ret.at<float>(row, col) = val - n * std::floor(val / n);
217
        }
237
        }
218
    }
238
    }
219
 
239
 
220
    return ret;
240
    return ret;
221
}
241
}
222
 
242
 
223
// Convert a 3xN matrix to a vector of Point3fs.
243
// Convert a 3xN matrix to a vector of Point3fs.
224
void matToPoints3f(const cv::Mat &mat, std::vector<cv::Point3f> &points){
244
void matToPoints3f(const cv::Mat &mat, std::vector<cv::Point3f> &points){
225
 
245
 
226
    unsigned int nPoints = mat.cols;
246
    unsigned int nPoints = mat.cols;
227
    points.resize(nPoints);
247
    points.resize(nPoints);
228
 
248
 
229
    for(unsigned int i=0; i<nPoints; i++)
249
    for(unsigned int i=0; i<nPoints; i++)
230
        points[i] = cv::Point3f(mat.at<float>(0, i), mat.at<float>(1, i), mat.at<float>(2, i));
250
        points[i] = cv::Point3f(mat.at<float>(0, i), mat.at<float>(1, i), mat.at<float>(2, i));
231
}
251
}
232
 
252
 
233
// Convert a (Dim+1)xN matrix of homogenous points to a DimxN matrix of points in non-homogenous coordinates.
253
// Convert a (Dim+1)xN matrix of homogenous points to a DimxN matrix of points in non-homogenous coordinates.
234
void convertMatFromHomogeneous(cv::Mat &src, cv::Mat &dst){
254
void convertMatFromHomogeneous(cv::Mat &src, cv::Mat &dst){
235
    unsigned int N = src.cols;
255
    unsigned int N = src.cols;
236
    unsigned int Dim = src.rows-1;
256
    unsigned int Dim = src.rows-1;
237
    dst.create(Dim, N, src.type());
257
    dst.create(Dim, N, src.type());
238
    for(unsigned int i=0; i<N; i++){
258
    for(unsigned int i=0; i<N; i++){
239
        for(unsigned int j=0; j<Dim; j++)
259
        for(unsigned int j=0; j<Dim; j++)
240
            dst.at<float>(j,i) = src.at<float>(j,i)/src.at<float>(Dim,i);
260
            dst.at<float>(j,i) = src.at<float>(j,i)/src.at<float>(Dim,i);
241
    }
261
    }
242
 
262
 
243
}
263
}
244
 
264
 
245
// Function to solve the hand-eye (or eye-in-hand) calibration problem.
265
// Function to solve the hand-eye (or eye-in-hand) calibration problem.
246
// Finds [Omega | tau], to minimize ||[R_mark | t_mark][Omega | tau] - [Omega | tau][R | t]||^2
266
// Finds [Omega | tau], to minimize ||[R_mark | t_mark][Omega | tau] - [Omega | tau][R | t]||^2
247
// Algorithm according to Tsai, Lenz, A new technique for fully autonomous and efficient 3d robotics hand-eye calibration
267
// Algorithm according to Tsai, Lenz, A new technique for fully autonomous and efficient 3d robotics hand-eye calibration
248
// DTU, 2014, Jakob Wilm
268
// DTU, 2014, Jakob Wilm
249
void handEyeCalibrationTsai(const std::vector<cv::Matx33f> R, const std::vector<cv::Vec3f> t, const std::vector<cv::Matx33f> R_mark, const std::vector<cv::Vec3f> t_mark, cv::Matx33f &Omega, cv::Vec3f &tau){
269
void handEyeCalibrationTsai(const std::vector<cv::Matx33f> R, const std::vector<cv::Vec3f> t, const std::vector<cv::Matx33f> R_mark, const std::vector<cv::Vec3f> t_mark, cv::Matx33f &Omega, cv::Vec3f &tau){
250
 
270
 
251
    int N = R.size();
271
    int N = R.size();
252
    assert(N == R_mark.size());
272
    assert(N == R_mark.size());
253
    assert(N == t.size());
273
    assert(N == t.size());
254
    assert(N == t_mark.size());
274
    assert(N == t_mark.size());
255
 
275
 
256
    // construct equations for rotation
276
    // construct equations for rotation
257
    cv::Mat A(3*N, 3, CV_32F);
277
    cv::Mat A(3*N, 3, CV_32F);
258
    cv::Mat b(3*N, 1, CV_32F);
278
    cv::Mat b(3*N, 1, CV_32F);
259
    for(int i=0; i<N; i++){
279
    for(int i=0; i<N; i++){
260
        // angle axis representations
280
        // angle axis representations
261
        cv::Vec3f rot;
281
        cv::Vec3f rot;
262
        cv::Vec3f rot_mark;
282
        cv::Vec3f rot_mark;
263
        cv::Rodrigues(R[i], rot);
283
        cv::Rodrigues(R[i], rot);
264
        cv::Rodrigues(R_mark[i], rot_mark);
284
        cv::Rodrigues(R_mark[i], rot_mark);
265
 
285
 
266
        cv::Vec3f P = 2.0*sin(cv::norm(rot)/2.0)*cv::normalize(rot);
286
        cv::Vec3f P = 2.0*sin(cv::norm(rot)/2.0)*cv::normalize(rot);
267
//std::cout << "P: " << std::endl << P << std::endl;
287
//std::cout << "P: " << std::endl << P << std::endl;
268
        cv::Vec3f P_mark = 2.0*sin(cv::norm(rot_mark)/2.0)*cv::normalize(rot_mark);
288
        cv::Vec3f P_mark = 2.0*sin(cv::norm(rot_mark)/2.0)*cv::normalize(rot_mark);
269
//std::cout << "P_mark: " << std::endl << P_mark << std::endl;
289
//std::cout << "P_mark: " << std::endl << P_mark << std::endl;
270
        cv::Vec3f sum = P+P_mark;
290
        cv::Vec3f sum = P+P_mark;
271
        cv::Mat crossProduct = (cv::Mat_<float>(3,3) << 0.0, -sum(2), sum(1), sum(2), 0.0, -sum(0), -sum(1), sum(0), 0.0);
291
        cv::Mat crossProduct = (cv::Mat_<float>(3,3) << 0.0, -sum(2), sum(1), sum(2), 0.0, -sum(0), -sum(1), sum(0), 0.0);
272
//std::cout << "crossProduct: " << std::endl << crossProduct << std::endl;
292
//std::cout << "crossProduct: " << std::endl << crossProduct << std::endl;
273
        crossProduct.copyTo(A.rowRange(i*3, i*3+3));
293
        crossProduct.copyTo(A.rowRange(i*3, i*3+3));
274
 
294
 
275
        cv::Mat(P-P_mark).copyTo(b.rowRange(i*3, i*3+3));
295
        cv::Mat(P-P_mark).copyTo(b.rowRange(i*3, i*3+3));
276
    }
296
    }
277
 
297
 
278
    // solve for rotation
298
    // solve for rotation
279
    cv::Vec3f P_prime;
299
    cv::Vec3f P_prime;
280
    cv::solve(A, b, P_prime, cv::DECOMP_SVD);
300
    cv::solve(A, b, P_prime, cv::DECOMP_SVD);
281
    cv::Vec3f P = 2.0*P_prime/(cv::sqrt(1.0 + cv::norm(P_prime)*cv::norm(P_prime)));
301
    cv::Vec3f P = 2.0*P_prime/(cv::sqrt(1.0 + cv::norm(P_prime)*cv::norm(P_prime)));
282
    float nP = cv::norm(P);
302
    float nP = cv::norm(P);
283
    cv::Mat crossProduct = (cv::Mat_<float>(3,3) << 0.0, -P(2), P(1), P(2), 0.0, -P(0), -P(1), P(0), 0.0);
303
    cv::Mat crossProduct = (cv::Mat_<float>(3,3) << 0.0, -P(2), P(1), P(2), 0.0, -P(0), -P(1), P(0), 0.0);
284
    cv::Mat OmegaMat = (1.0-nP*nP/2.0)*cv::Mat::eye(3,3,CV_32F) + 0.5*(cv::Mat(P)*cv::Mat(P).t() + cv::sqrt(4.0 - nP*nP)*crossProduct);
304
    cv::Mat OmegaMat = (1.0-nP*nP/2.0)*cv::Mat::eye(3,3,CV_32F) + 0.5*(cv::Mat(P)*cv::Mat(P).t() + cv::sqrt(4.0 - nP*nP)*crossProduct);
285
    Omega = cv::Matx33f(OmegaMat);
305
    Omega = cv::Matx33f(OmegaMat);
286
 
306
 
287
    // construct equations for translation
307
    // construct equations for translation
288
    A.setTo(0.0);
308
    A.setTo(0.0);
289
    b.setTo(0.0);
309
    b.setTo(0.0);
290
    for(int i=0; i<N; i++){
310
    for(int i=0; i<N; i++){
291
 
311
 
292
        cv::Mat diff = cv::Mat(R_mark[i]) - cv::Mat::eye(3, 3, CV_32F);
312
        cv::Mat diff = cv::Mat(R_mark[i]) - cv::Mat::eye(3, 3, CV_32F);
293
        diff.copyTo(A.rowRange(i*3, i*3+3));
313
        diff.copyTo(A.rowRange(i*3, i*3+3));
294
 
314
 
295
        cv::Mat diff_mark = cv::Mat(Omega*t[i] - t_mark[i]);
315
        cv::Mat diff_mark = cv::Mat(Omega*t[i] - t_mark[i]);
296
        diff_mark.copyTo(b.rowRange(i*3, i*3+3));
316
        diff_mark.copyTo(b.rowRange(i*3, i*3+3));
297
    }
317
    }
298
 
318
 
299
    // solve for translation
319
    // solve for translation
300
    cv::solve(A, b, tau, cv::DECOMP_SVD);
320
    cv::solve(A, b, tau, cv::DECOMP_SVD);
301
 
321
 
302
    cv::Mat err_tau = b - (A*cv::Mat(tau));
322
    cv::Mat err_tau = b - (A*cv::Mat(tau));
303
    std::cout << err_tau << std::endl;
323
    std::cout << err_tau << std::endl;
304
}
324
}
305
 
325
 
306
// Function to solve for the rotation axis from sets of 3D point coordinates of flat pattern feature points
326
// Function to solve for the rotation axis from sets of 3D point coordinates of flat pattern feature points
307
// Algorithm according to Chen et al., Rotation axis calibration of a turntable using constrained global optimization, Optik 2014
327
// Algorithm according to Chen et al., Rotation axis calibration of a turntable using constrained global optimization, Optik 2014
308
// DTU, 2014, Jakob Wilm
328
// DTU, 2014, Jakob Wilm
309
void rotationAxisCalibration(const std::vector< std::vector<cv::Point3f> > Qcam, const std::vector<cv::Point3f> Qobj, cv::Vec3f &axis, cv::Vec3f &point){
329
void rotationAxisCalibration(const std::vector< std::vector<cv::Point3f> > Qcam, const std::vector<cv::Point3f> Qobj, cv::Vec3f &axis, cv::Vec3f &point){
310
 
330
 
311
    // number of frames (points on each arch)
331
    // number of frames (points on each arch)
312
    int l = Qcam.size();
332
    int l = Qcam.size();
313
 
333
 
314
    // number of points in each frame
334
    // number of points in each frame
315
    int mn = Qobj.size();
335
    int mn = Qobj.size();
316
 
336
 
317
    assert(mn == Qcam[0].size());
337
    assert(mn == Qcam[0].size());
318
 
338
 
319
    // construct matrix for axis determination
339
    // construct matrix for axis determination
320
    cv::Mat M(6, 6, CV_32F, cv::Scalar(0));
340
    cv::Mat M(6, 6, CV_32F, cv::Scalar(0));
321
 
341
 
322
    for(int k=0; k<l; k++){
342
    for(int k=0; k<l; k++){
323
        for(int idx=0; idx<mn; idx++){
343
        for(int idx=0; idx<mn; idx++){
324
 
344
 
325
//            float i = Qobj[idx].x+4;
345
//            float i = Qobj[idx].x+4;
326
//            float j = Qobj[idx].y+4;
346
//            float j = Qobj[idx].y+4;
327
            float i = Qobj[idx].x;
347
            float i = Qobj[idx].x;
328
            float j = Qobj[idx].y;
348
            float j = Qobj[idx].y;
329
 
349
 
330
            float x = Qcam[k][idx].x;
350
            float x = Qcam[k][idx].x;
331
            float y = Qcam[k][idx].y;
351
            float y = Qcam[k][idx].y;
332
            float z = Qcam[k][idx].z;
352
            float z = Qcam[k][idx].z;
333
 
353
 
334
            M += (cv::Mat_<float>(6,6) << x*x, x*y, x*z, x, i*x, j*x,
354
            M += (cv::Mat_<float>(6,6) << x*x, x*y, x*z, x, i*x, j*x,
335
                                            0, y*y, y*z, y, i*y, j*y,
355
                                            0, y*y, y*z, y, i*y, j*y,
336
                                            0,   0, z*z, z, i*z, j*z,
356
                                            0,   0, z*z, z, i*z, j*z,
337
                                            0,   0,   0, 1,   i,   j,
357
                                            0,   0,   0, 1,   i,   j,
338
                                            0,   0,   0, 0, i*i, i*j,
358
                                            0,   0,   0, 0, i*i, i*j,
339
                                            0,   0,   0, 0,   0, j*j);
359
                                            0,   0,   0, 0,   0, j*j);
340
 
360
 
341
        }
361
        }
342
    }
362
    }
343
 
363
 
344
    cv::completeSymm(M, false);
364
    cv::completeSymm(M, false);
345
 
365
 
346
    // solve for axis
366
    // solve for axis
347
    std::vector<float> lambda;
367
    std::vector<float> lambda;
348
    cv::Mat u;
368
    cv::Mat u;
349
    cv::eigen(M, lambda, u);
369
    cv::eigen(M, lambda, u);
350
 
370
 
351
    float minLambda = abs(lambda[0]);
371
    float minLambda = abs(lambda[0]);
352
    int idx = 0;
372
    int idx = 0;
353
    for(int i=1; i<lambda.size(); i++){
373
    for(int i=1; i<lambda.size(); i++){
354
        if(abs(lambda[i]) < minLambda){
374
        if(abs(lambda[i]) < minLambda){
355
            minLambda = lambda[i];
375
            minLambda = lambda[i];
356
            idx = i;
376
            idx = i;
357
        }
377
        }
358
    }
378
    }
359
 
379
 
360
    axis = u.row(idx).colRange(0, 3);
380
    axis = u.row(idx).colRange(0, 3);
361
    axis = cv::normalize(axis);
381
    axis = cv::normalize(axis);
362
 
382
 
363
    float nx = u.at<float>(idx, 0);
383
    float nx = u.at<float>(idx, 0);
364
    float ny = u.at<float>(idx, 1);
384
    float ny = u.at<float>(idx, 1);
365
    float nz = u.at<float>(idx, 2);
385
    float nz = u.at<float>(idx, 2);
366
    float d  = u.at<float>(idx, 3);
386
    float d  = u.at<float>(idx, 3);
367
    float dh = u.at<float>(idx, 4);
387
    float dh = u.at<float>(idx, 4);
368
    float dv = u.at<float>(idx, 5);
388
    float dv = u.at<float>(idx, 5);
369
 
389
 
370
//    // Paper version: c is initially eliminated
390
//    // Paper version: c is initially eliminated
371
//    cv::Mat A(l*mn, mn+2, CV_32F, cv::Scalar(0.0));
391
//    cv::Mat A(l*mn, mn+2, CV_32F, cv::Scalar(0.0));
372
//    cv::Mat bb(l*mn, 1, CV_32F);
392
//    cv::Mat bb(l*mn, 1, CV_32F);
373
 
393
 
374
//    for(int k=0; k<l; k++){
394
//    for(int k=0; k<l; k++){
375
//        for(int idx=0; idx<mn; idx++){
395
//        for(int idx=0; idx<mn; idx++){
376
 
396
 
377
//            float i = Qobj[idx].x;
397
//            float i = Qobj[idx].x;
378
//            float j = Qobj[idx].y;
398
//            float j = Qobj[idx].y;
379
 
399
 
380
//            float x = Qcam[k][idx].x;
400
//            float x = Qcam[k][idx].x;
381
//            float y = Qcam[k][idx].y;
401
//            float y = Qcam[k][idx].y;
382
//            float z = Qcam[k][idx].z;
402
//            float z = Qcam[k][idx].z;
383
 
403
 
384
//            float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
404
//            float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
385
 
405
 
386
//            int row = k*mn+idx;
406
//            int row = k*mn+idx;
387
//            A.at<float>(row, 0) = 2*x - (2*z*nx)/nz;
407
//            A.at<float>(row, 0) = 2*x - (2*z*nx)/nz;
388
//            A.at<float>(row, 1) = 2*y - (2*z*ny)/nz;
408
//            A.at<float>(row, 1) = 2*y - (2*z*ny)/nz;
389
//            A.at<float>(row, idx+2) = 1.0;
409
//            A.at<float>(row, idx+2) = 1.0;
390
 
410
 
391
//            bb.at<float>(row, 0) = f + (2*z*d)/nz;
411
//            bb.at<float>(row, 0) = f + (2*z*d)/nz;
392
//        }
412
//        }
393
//    }
413
//    }
394
 
414
 
395
//    // solve for point
415
//    // solve for point
396
//    cv::Mat abe;
416
//    cv::Mat abe;
397
//    cv::solve(A, bb, abe, cv::DECOMP_SVD);
417
//    cv::solve(A, bb, abe, cv::DECOMP_SVD);
398
 
418
 
399
//    float a = abe.at<float>(0, 0);
419
//    float a = abe.at<float>(0, 0);
400
//    float b = abe.at<float>(1, 0);
420
//    float b = abe.at<float>(1, 0);
401
//    float c = -(nx*a+ny*b+d)/nz;
421
//    float c = -(nx*a+ny*b+d)/nz;
402
 
422
 
403
    // Our version: solve simultanously for a,b,c
423
    // Our version: solve simultanously for a,b,c
404
    cv::Mat A(l*mn, mn+3, CV_32F, cv::Scalar(0.0));
424
    cv::Mat A(l*mn, mn+3, CV_32F, cv::Scalar(0.0));
405
    cv::Mat bb(l*mn, 1, CV_32F);
425
    cv::Mat bb(l*mn, 1, CV_32F);
406
 
426
 
407
    for(int k=0; k<l; k++){
427
    for(int k=0; k<l; k++){
408
        for(int idx=0; idx<mn; idx++){
428
        for(int idx=0; idx<mn; idx++){
409
 
429
 
410
            float i = Qobj[idx].x;
430
            float i = Qobj[idx].x;
411
            float j = Qobj[idx].y;
431
            float j = Qobj[idx].y;
412
 
432
 
413
            float x = Qcam[k][idx].x;
433
            float x = Qcam[k][idx].x;
414
            float y = Qcam[k][idx].y;
434
            float y = Qcam[k][idx].y;
415
            float z = Qcam[k][idx].z;
435
            float z = Qcam[k][idx].z;
416
 
436
 
417
            float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
437
            float f = x*x + y*y + z*z + (2*x*nx + 2*y*ny + 2*z*nz)*(i*dh + j*dv);
418
 
438
 
419
            int row = k*mn+idx;
439
            int row = k*mn+idx;
420
            A.at<float>(row, 0) = 2*x;
440
            A.at<float>(row, 0) = 2*x;
421
            A.at<float>(row, 1) = 2*y;
441
            A.at<float>(row, 1) = 2*y;
422
            A.at<float>(row, 2) = 2*z;
442
            A.at<float>(row, 2) = 2*z;
423
            A.at<float>(row, idx+3) = 1.0;
443
            A.at<float>(row, idx+3) = 1.0;
424
 
444
 
425
            bb.at<float>(row, 0) = f;
445
            bb.at<float>(row, 0) = f;
426
        }
446
        }
427
    }
447
    }
428
 
448
 
429
    // solve for point
449
    // solve for point
430
    cv::Mat abe;
450
    cv::Mat abe;
431
    cv::solve(A, bb, abe, cv::DECOMP_SVD);
451
    cv::solve(A, bb, abe, cv::DECOMP_SVD);
432
 
452
 
433
    float a = abe.at<float>(0, 0);
453
    float a = abe.at<float>(0, 0);
434
    float b = abe.at<float>(1, 0);
454
    float b = abe.at<float>(1, 0);
435
    float c = abe.at<float>(2, 0);
455
    float c = abe.at<float>(2, 0);
436
 
456
 
437
    point[0] = a;
457
    point[0] = a;
438
    point[1] = b;
458
    point[1] = b;
439
    point[2] = c;
459
    point[2] = c;
440
 
460
 
441
}
461
}
442
 
462
 
443
// Function to fit two sets of corresponding pose data.
463
// Function to fit two sets of corresponding pose data.
444
// Finds [Omega | tau], to minimize ||[R_mark | t_mark] - [Omega | tau][R | t]||^2
464
// Finds [Omega | tau], to minimize ||[R_mark | t_mark] - [Omega | tau][R | t]||^2
445
// Algorithm and notation according to Mili Shah, Comparing two sets of corresponding six degree of freedom data, CVIU 2011.
465
// Algorithm and notation according to Mili Shah, Comparing two sets of corresponding six degree of freedom data, CVIU 2011.
446
// DTU, 2013, Oline V. Olesen, Jakob Wilm
466
// DTU, 2013, Oline V. Olesen, Jakob Wilm
447
void fitSixDofData(const std::vector<cv::Matx33f> R, const std::vector<cv::Vec3f> t, const std::vector<cv::Matx33f> R_mark, const std::vector<cv::Vec3f> t_mark, cv::Matx33f &Omega, cv::Vec3f &tau){
467
void fitSixDofData(const std::vector<cv::Matx33f> R, const std::vector<cv::Vec3f> t, const std::vector<cv::Matx33f> R_mark, const std::vector<cv::Vec3f> t_mark, cv::Matx33f &Omega, cv::Vec3f &tau){
448
 
468
 
449
    int N = R.size();
469
    int N = R.size();
450
    assert(N == R_mark.size());
470
    assert(N == R_mark.size());
451
    assert(N == t.size());
471
    assert(N == t.size());
452
    assert(N == t_mark.size());
472
    assert(N == t_mark.size());
453
 
473
 
454
    // Mean translations
474
    // Mean translations
455
    cv::Vec3f t_mean;
475
    cv::Vec3f t_mean;
456
    cv::Vec3f t_mark_mean;
476
    cv::Vec3f t_mark_mean;
457
    for(int i=0; i<N; i++){
477
    for(int i=0; i<N; i++){
458
        t_mean += 1.0/N * t[i];
478
        t_mean += 1.0/N * t[i];
459
        t_mark_mean += 1.0/N * t_mark[i];
479
        t_mark_mean += 1.0/N * t_mark[i];
460
    }
480
    }
461
 
481
 
462
    // Data with mean adjusted translations
482
    // Data with mean adjusted translations
463
    cv::Mat X_bar(3, 4*N, CV_32F);
483
    cv::Mat X_bar(3, 4*N, CV_32F);
464
    cv::Mat X_mark_bar(3, 4*N, CV_32F);
484
    cv::Mat X_mark_bar(3, 4*N, CV_32F);
465
    for(int i=0; i<N; i++){
485
    for(int i=0; i<N; i++){
466
        cv::Mat(R[i]).copyTo(X_bar.colRange(i*4,i*4+3));
486
        cv::Mat(R[i]).copyTo(X_bar.colRange(i*4,i*4+3));
467
        cv::Mat(t[i] - t_mean).copyTo(X_bar.col(i*4+3));
487
        cv::Mat(t[i] - t_mean).copyTo(X_bar.col(i*4+3));
468
        cv::Mat(R_mark[i]).copyTo(X_mark_bar.colRange(i*4,i*4+3));
488
        cv::Mat(R_mark[i]).copyTo(X_mark_bar.colRange(i*4,i*4+3));
469
        cv::Mat(t_mark[i] - t_mark_mean).copyTo(X_mark_bar.col(i*4+3));
489
        cv::Mat(t_mark[i] - t_mark_mean).copyTo(X_mark_bar.col(i*4+3));
470
    }
490
    }
471
    //std::cout << X_bar << std::endl;
491
    //std::cout << X_bar << std::endl;
472
    // SVD
492
    // SVD
473
    cv::Mat W, U, VT;
493
    cv::Mat W, U, VT;
474
    cv::SVDecomp(X_bar*X_mark_bar.t(), W, U, VT);
494
    cv::SVDecomp(X_bar*X_mark_bar.t(), W, U, VT);
475
 
495
 
476
    cv::Matx33f D = cv::Matx33f::eye();
496
    cv::Matx33f D = cv::Matx33f::eye();
477
    if(cv::determinant(VT*U) < 0)
497
    if(cv::determinant(VT*U) < 0)
478
        D(3,3) = -1;
498
        D(3,3) = -1;
479
 
499
 
480
    // Best rotation
500
    // Best rotation
481
    Omega = cv::Matx33f(cv::Mat(VT.t()))*D*cv::Matx33f(cv::Mat(U.t()));
501
    Omega = cv::Matx33f(cv::Mat(VT.t()))*D*cv::Matx33f(cv::Mat(U.t()));
482
 
502
 
483
    // Best translation
503
    // Best translation
484
    tau = t_mark_mean - Omega*t_mean;
504
    tau = t_mark_mean - Omega*t_mean;
485
 
505
 
486
}
506
}
487
 
507
 
488
// Forward distortion of points. The inverse of the undistortion in cv::initUndistortRectifyMap().
508
// Forward distortion of points. The inverse of the undistortion in cv::initUndistortRectifyMap().
489
// Inspired by Pascal Thomet, http://code.opencv.org/issues/1387#note-11
509
// Inspired by Pascal Thomet, http://code.opencv.org/issues/1387#note-11
490
// Convention for distortion parameters: http://www.vision.caltech.edu/bouguetj/calib_doc/htmls/parameters.html
510
// Convention for distortion parameters: http://www.vision.caltech.edu/bouguetj/calib_doc/htmls/parameters.html
491
void initDistortMap(const cv::Matx33f cameraMatrix, const cv::Vec<float, 5> distCoeffs, const cv::Size size, cv::Mat &map1, cv::Mat &map2){
511
void initDistortMap(const cv::Matx33f cameraMatrix, const cv::Vec<float, 5> distCoeffs, const cv::Size size, cv::Mat &map1, cv::Mat &map2){
492
 
512
 
493
    float fx = cameraMatrix(0,0);
513
    float fx = cameraMatrix(0,0);
494
    float fy = cameraMatrix(1,1);
514
    float fy = cameraMatrix(1,1);
495
    float ux = cameraMatrix(0,2);
515
    float ux = cameraMatrix(0,2);
496
    float uy = cameraMatrix(1,2);
516
    float uy = cameraMatrix(1,2);
497
 
517
 
498
    float k1 = distCoeffs[0];
518
    float k1 = distCoeffs[0];
499
    float k2 = distCoeffs[1];
519
    float k2 = distCoeffs[1];
500
    float p1 = distCoeffs[2];
520
    float p1 = distCoeffs[2];
501
    float p2 = distCoeffs[3];
521
    float p2 = distCoeffs[3];
502
    float k3 = distCoeffs[4];
522
    float k3 = distCoeffs[4];
503
 
523
 
504
    map1.create(size, CV_32F);
524
    map1.create(size, CV_32F);
505
    map2.create(size, CV_32F);
525
    map2.create(size, CV_32F);
506
 
526
 
507
    for(int col = 0; col < size.width; col++){
527
    for(int col = 0; col < size.width; col++){
508
        for(int row = 0; row < size.height; row++){
528
        for(int row = 0; row < size.height; row++){
509
 
529
 
510
            // move origo to principal point and convert using focal length
530
            // move origo to principal point and convert using focal length
511
            float x = (col-ux)/fx;
531
            float x = (col-ux)/fx;
512
            float y = (row-uy)/fy;
532
            float y = (row-uy)/fy;
513
 
533
 
514
            float xCorrected, yCorrected;
534
            float xCorrected, yCorrected;
515
 
535
 
516
            //Step 1 : correct distortion
536
            //Step 1 : correct distortion
517
            float r2 = x*x + y*y;
537
            float r2 = x*x + y*y;
518
            //radial
538
            //radial
519
            xCorrected = x * (1. + k1*r2 + k2*r2*r2 + k3*r2*r2*r2);
539
            xCorrected = x * (1. + k1*r2 + k2*r2*r2 + k3*r2*r2*r2);
520
            yCorrected = y * (1. + k1*r2 + k2*r2*r2 + k3*r2*r2*r2);
540
            yCorrected = y * (1. + k1*r2 + k2*r2*r2 + k3*r2*r2*r2);
521
            //tangential
541
            //tangential
522
            xCorrected = xCorrected + (2.*p1*x*y + p2*(r2+2.*x*x));
542
            xCorrected = xCorrected + (2.*p1*x*y + p2*(r2+2.*x*x));
523
            yCorrected = yCorrected + (p1*(r2+2.*y*y) + 2.*p2*x*y);
543
            yCorrected = yCorrected + (p1*(r2+2.*y*y) + 2.*p2*x*y);
524
 
544
 
525
            //convert back to pixel coordinates
545
            //convert back to pixel coordinates
526
            float col_displaced = xCorrected * fx + ux;
546
            float col_displaced = xCorrected * fx + ux;
527
            float row_displaced = yCorrected * fy + uy;
547
            float row_displaced = yCorrected * fy + uy;
528
 
548
 
529
            // correct the vector in the opposite direction
549
            // correct the vector in the opposite direction
530
            map1.at<float>(row,col) = col+(col-col_displaced);
550
            map1.at<float>(row,col) = col+(col-col_displaced);
531
            map2.at<float>(row,col) = row +(row-row_displaced);
551
            map2.at<float>(row,col) = row +(row-row_displaced);
532
        }
552
        }
533
    }
553
    }
534
}
554
}
535
 
555
 
536
// Downsample a texture which was created in virtual column/row space for a diamond pixel array projector
556
// Downsample a texture which was created in virtual column/row space for a diamond pixel array projector
537
cv::Mat diamondDownsample(cv::Mat &pattern){
557
cv::Mat diamondDownsample(cv::Mat &pattern){
538
 
558
 
539
    cv::Mat pattern_diamond(pattern.rows,pattern.cols/2,CV_8UC3);
559
    cv::Mat pattern_diamond(pattern.rows,pattern.cols/2,CV_8UC3);
540
 
560
 
541
    for(unsigned int col = 0; col < pattern_diamond.cols; col++){
561
    for(unsigned int col = 0; col < pattern_diamond.cols; col++){
542
        for(unsigned int row = 0; row < pattern_diamond.rows; row++){
562
        for(unsigned int row = 0; row < pattern_diamond.rows; row++){
543
 
563
 
544
            pattern_diamond.at<cv::Vec3b>(row,col)=pattern.at<cv::Vec3b>(row,col*2+row%2);
564
            pattern_diamond.at<cv::Vec3b>(row,col)=pattern.at<cv::Vec3b>(row,col*2+row%2);
545
        }
565
        }
546
    }
566
    }
547
 
567
 
548
    return pattern_diamond;
568
    return pattern_diamond;
549
 
569
 
550
}
570
}
551
 
571
 
552
 
572
 
553
void mouseCallback(int evt, int x, int y, int flags, void* param){
573
void mouseCallback(int evt, int x, int y, int flags, void* param){
554
    cv::Mat *im = (cv::Mat*) param;
574
    cv::Mat *im = (cv::Mat*) param;
555
    if (evt == CV_EVENT_LBUTTONDOWN) {
575
    if (evt == CV_EVENT_LBUTTONDOWN) {
556
        if(im->type() == CV_8UC3){
576
        if(im->type() == CV_8UC3){
557
            printf("%d %d: %d, %d, %d\n",
577
            printf("%d %d: %d, %d, %d\n",
558
                   x, y,
578
                   x, y,
559
                   (int)(*im).at<cv::Vec3b>(y, x)[0],
579
                   (int)(*im).at<cv::Vec3b>(y, x)[0],
560
                    (int)(*im).at<cv::Vec3b>(y, x)[1],
580
                    (int)(*im).at<cv::Vec3b>(y, x)[1],
561
                    (int)(*im).at<cv::Vec3b>(y, x)[2]);
581
                    (int)(*im).at<cv::Vec3b>(y, x)[2]);
562
        } else if (im->type() == CV_32F) {
582
        } else if (im->type() == CV_32F) {
563
            printf("%d %d: %f\n",
583
            printf("%d %d: %f\n",
564
                   x, y,
584
                   x, y,
565
                   im->at<float>(y, x));
585
                   im->at<float>(y, x));
566
        }
586
        }
567
    }
587
    }
568
}
588
}
569
 
589
 
570
void imshow(const char *windowName, cv::Mat im, unsigned int x, unsigned int y){
590
void imshow(const char *windowName, cv::Mat im, unsigned int x, unsigned int y){
571
 
591
 
572
    // Imshow
592
    // Imshow
573
    if(!cvGetWindowHandle(windowName)){
593
    if(!cvGetWindowHandle(windowName)){
574
        int windowFlags = CV_GUI_EXPANDED | CV_WINDOW_KEEPRATIO;
594
        int windowFlags = CV_GUI_EXPANDED | CV_WINDOW_KEEPRATIO;
575
        cv::namedWindow(windowName, windowFlags);
595
        cv::namedWindow(windowName, windowFlags);
576
        cv::moveWindow(windowName, x, y);
596
        cv::moveWindow(windowName, x, y);
577
    }
597
    }
578
    cv::imshow(windowName, im);
598
    cv::imshow(windowName, im);
579
}
599
}
580
 
600
 
581
void imagesc(const char *windowName, cv::Mat im){
601
void imagesc(const char *windowName, cv::Mat im){
582
 
602
 
583
    // Imshow with scaled image
603
    // Imshow with scaled image
584
 
604
 
585
 
605
 
586
}
606
}
587
 
607
 
588
cv::Mat histimage(cv::Mat histogram){
608
cv::Mat histimage(cv::Mat histogram){
589
 
609
 
590
    cv::Mat histImage(512, 640, CV_8UC3, cv::Scalar(0));
610
    cv::Mat histImage(512, 640, CV_8UC3, cv::Scalar(0));
591
 
611
 
592
    // Normalize the result to [ 2, histImage.rows-2 ]
612
    // Normalize the result to [ 2, histImage.rows-2 ]
593
    cv::normalize(histogram, histogram, 2, histImage.rows-2, cv::NORM_MINMAX, -1, cv::Mat());
613
    cv::normalize(histogram, histogram, 2, histImage.rows-2, cv::NORM_MINMAX, -1, cv::Mat());
594
 
614
 
595
    float bin_w = (float)histImage.cols/(float)histogram.rows;
615
    float bin_w = (float)histImage.cols/(float)histogram.rows;
596
 
616
 
597
    // Draw main histogram
617
    // Draw main histogram
598
    for(int i = 1; i < histogram.rows-10; i++){
618
    for(int i = 1; i < histogram.rows-10; i++){
599
        cv::line(histImage, cv::Point( bin_w*(i-1), histImage.rows - cvRound(histogram.at<float>(i-1)) ),
619
        cv::line(histImage, cv::Point( bin_w*(i-1), histImage.rows - cvRound(histogram.at<float>(i-1)) ),
600
                 cv::Point( bin_w*(i), histImage.rows - cvRound(histogram.at<float>(i)) ),
620
                 cv::Point( bin_w*(i), histImage.rows - cvRound(histogram.at<float>(i)) ),
601
                 cv::Scalar(255, 255, 255), 2, 4);
621
                 cv::Scalar(255, 255, 255), 2, 4);
602
    }
622
    }
603
 
623
 
604
    // Draw red max
624
    // Draw red max
605
    for(int i = histogram.rows-10; i < histogram.rows; i++){
625
    for(int i = histogram.rows-10; i < histogram.rows; i++){
606
        cv::line(histImage, cv::Point( bin_w*(i-1), histImage.rows - cvRound(histogram.at<float>(i-1)) ),
626
        cv::line(histImage, cv::Point( bin_w*(i-1), histImage.rows - cvRound(histogram.at<float>(i-1)) ),
607
                 cv::Point( bin_w*(i), histImage.rows - cvRound(histogram.at<float>(i)) ),
627
                 cv::Point( bin_w*(i), histImage.rows - cvRound(histogram.at<float>(i)) ),
608
                 cv::Scalar(0, 0, 255), 2, 4);
628
                 cv::Scalar(0, 0, 255), 2, 4);
609
    }
629
    }
610
 
630
 
611
    return histImage;
631
    return histImage;
612
}
632
}
613
 
633
 
614
void hist(const char *windowName, cv::Mat histogram, unsigned int x, unsigned int y){
634
void hist(const char *windowName, cv::Mat histogram, unsigned int x, unsigned int y){
615
 
635
 
616
    // Display
636
    // Display
617
    imshow(windowName, histimage(histogram), x, y);
637
    imshow(windowName, histimage(histogram), x, y);
618
    cv::Point(1,2);
638
    cv::Point(1,2);
619
}
639
}
620
 
640
 
621
 
641
 
622
void writeMat(cv::Mat const& mat, const char* filename, const char* varName, bool bgr2rgb){
642
void writeMat(cv::Mat const& mat, const char* filename, const char* varName, bool bgr2rgb){
623
    /*!
643
    /*!
624
         *  \author Philip G. Lee <rocketman768@gmail.com>
644
         *  \author Philip G. Lee <rocketman768@gmail.com>
625
         *  Write \b mat into \b filename
645
         *  Write \b mat into \b filename
626
         *  in uncompressed .mat format (Level 5 MATLAB) for Matlab.
646
         *  in uncompressed .mat format (Level 5 MATLAB) for Matlab.
627
         *  The variable name in matlab will be \b varName. If
647
         *  The variable name in matlab will be \b varName. If
628
         *  \b bgr2rgb is true and there are 3 channels, swaps 1st and 3rd
648
         *  \b bgr2rgb is true and there are 3 channels, swaps 1st and 3rd
629
         *  channels in the output. This is needed because OpenCV matrices
649
         *  channels in the output. This is needed because OpenCV matrices
630
         *  are bgr, while Matlab is rgb. This has been tested to work with
650
         *  are bgr, while Matlab is rgb. This has been tested to work with
631
         *  3-channel single-precision floating point matrices, and I hope
651
         *  3-channel single-precision floating point matrices, and I hope
632
         *  it works on other types/channels, but not exactly sure.
652
         *  it works on other types/channels, but not exactly sure.
633
         *  Documentation at <http://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf>
653
         *  Documentation at <http://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf>
634
         */
654
         */
635
    int textLen = 116;
655
    int textLen = 116;
636
    char* text;
656
    char* text;
637
    int subsysOffsetLen = 8;
657
    int subsysOffsetLen = 8;
638
    char* subsysOffset;
658
    char* subsysOffset;
639
    int verLen = 2;
659
    int verLen = 2;
640
    char* ver;
660
    char* ver;
641
    char flags;
661
    char flags;
642
    int bytes;
662
    int bytes;
643
    int padBytes;
663
    int padBytes;
644
    int bytesPerElement;
664
    int bytesPerElement;
645
    int i,j,k,k2;
665
    int i,j,k,k2;
646
    bool doBgrSwap;
666
    bool doBgrSwap;
647
    char mxClass;
667
    char mxClass;
648
    int32_t miClass;
668
    int32_t miClass;
649
    uchar const* rowPtr;
669
    uchar const* rowPtr;
650
    uint32_t tmp32;
670
    uint32_t tmp32;
651
    float tmp;
671
    float tmp;
652
    FILE* fp;
672
    FILE* fp;
653
 
673
 
654
    // Matlab constants.
674
    // Matlab constants.
655
    const uint16_t MI = 0x4d49; // Contains "MI" in ascii.
675
    const uint16_t MI = 0x4d49; // Contains "MI" in ascii.
656
    const int32_t miINT8 = 1;
676
    const int32_t miINT8 = 1;
657
    const int32_t miUINT8 = 2;
677
    const int32_t miUINT8 = 2;
658
    const int32_t miINT16 = 3;
678
    const int32_t miINT16 = 3;
659
    const int32_t miUINT16 = 4;
679
    const int32_t miUINT16 = 4;
660
    const int32_t miINT32 = 5;
680
    const int32_t miINT32 = 5;
661
    const int32_t miUINT32 = 6;
681
    const int32_t miUINT32 = 6;
662
    const int32_t miSINGLE = 7;
682
    const int32_t miSINGLE = 7;
663
    const int32_t miDOUBLE = 9;
683
    const int32_t miDOUBLE = 9;
664
    const int32_t miMATRIX = 14;
684
    const int32_t miMATRIX = 14;
665
    const char mxDOUBLE_CLASS = 6;
685
    const char mxDOUBLE_CLASS = 6;
666
    const char mxSINGLE_CLASS = 7;
686
    const char mxSINGLE_CLASS = 7;
667
    const char mxINT8_CLASS = 8;
687
    const char mxINT8_CLASS = 8;
668
    const char mxUINT8_CLASS = 9;
688
    const char mxUINT8_CLASS = 9;
669
    const char mxINT16_CLASS = 10;
689
    const char mxINT16_CLASS = 10;
670
    const char mxUINT16_CLASS = 11;
690
    const char mxUINT16_CLASS = 11;
671
    const char mxINT32_CLASS = 12;
691
    const char mxINT32_CLASS = 12;
672
    const char mxUINT32_CLASS = 13;
692
    const char mxUINT32_CLASS = 13;
673
    const uint64_t zero = 0; // Used for padding.
693
    const uint64_t zero = 0; // Used for padding.
674
 
694
 
675
    fp = fopen( filename, "wb" );
695
    fp = fopen( filename, "wb" );
676
 
696
 
677
    if( fp == 0 )
697
    if( fp == 0 )
678
        return;
698
        return;
679
 
699
 
680
    const int rows = mat.rows;
700
    const int rows = mat.rows;
681
    const int cols = mat.cols;
701
    const int cols = mat.cols;
682
    const int chans = mat.channels();
702
    const int chans = mat.channels();
683
 
703
 
684
    doBgrSwap = (chans==3) && bgr2rgb;
704
    doBgrSwap = (chans==3) && bgr2rgb;
685
 
705
 
686
    // I hope this mapping is right :-/
706
    // I hope this mapping is right :-/
687
    switch( mat.depth() ){
707
    switch( mat.depth() ){
688
    case CV_8U:
708
    case CV_8U:
689
        mxClass = mxUINT8_CLASS;
709
        mxClass = mxUINT8_CLASS;
690
        miClass = miUINT8;
710
        miClass = miUINT8;
691
        bytesPerElement = 1;
711
        bytesPerElement = 1;
692
        break;
712
        break;
693
    case CV_8S:
713
    case CV_8S:
694
        mxClass = mxINT8_CLASS;
714
        mxClass = mxINT8_CLASS;
695
        miClass = miINT8;
715
        miClass = miINT8;
696
        bytesPerElement = 1;
716
        bytesPerElement = 1;
697
        break;
717
        break;
698
    case CV_16U:
718
    case CV_16U:
699
        mxClass = mxUINT16_CLASS;
719
        mxClass = mxUINT16_CLASS;
700
        miClass = miUINT16;
720
        miClass = miUINT16;
701
        bytesPerElement = 2;
721
        bytesPerElement = 2;
702
        break;
722
        break;
703
    case CV_16S:
723
    case CV_16S:
704
        mxClass = mxINT16_CLASS;
724
        mxClass = mxINT16_CLASS;
705
        miClass = miINT16;
725
        miClass = miINT16;
706
        bytesPerElement = 2;
726
        bytesPerElement = 2;
707
        break;
727
        break;
708
    case CV_32S:
728
    case CV_32S:
709
        mxClass = mxINT32_CLASS;
729
        mxClass = mxINT32_CLASS;
710
        miClass = miINT32;
730
        miClass = miINT32;
711
        bytesPerElement = 4;
731
        bytesPerElement = 4;
712
        break;
732
        break;
713
    case CV_32F:
733
    case CV_32F:
714
        mxClass = mxSINGLE_CLASS;
734
        mxClass = mxSINGLE_CLASS;
715
        miClass = miSINGLE;
735
        miClass = miSINGLE;
716
        bytesPerElement = 4;
736
        bytesPerElement = 4;
717
        break;
737
        break;
718
    case CV_64F:
738
    case CV_64F:
719
        mxClass = mxDOUBLE_CLASS;
739
        mxClass = mxDOUBLE_CLASS;
720
        miClass = miDOUBLE;
740
        miClass = miDOUBLE;
721
        bytesPerElement = 8;
741
        bytesPerElement = 8;
722
        break;
742
        break;
723
    default:
743
    default:
724
        return;
744
        return;
725
    }
745
    }
726
 
746
 
727
    //==================Mat-file header (128 bytes, page 1-5)==================
747
    //==================Mat-file header (128 bytes, page 1-5)==================
728
    text = new char[textLen]; // Human-readable text.
748
    text = new char[textLen]; // Human-readable text.
729
    memset( text, ' ', textLen );
749
    memset( text, ' ', textLen );
730
    text[textLen-1] = '\0';
750
    text[textLen-1] = '\0';
731
    const char* t = "MATLAB 5.0 MAT-file, Platform: PCWIN";
751
    const char* t = "MATLAB 5.0 MAT-file, Platform: PCWIN";
732
    memcpy( text, t, strlen(t) );
752
    memcpy( text, t, strlen(t) );
733
 
753
 
734
    subsysOffset = new char[subsysOffsetLen]; // Zeros for us.
754
    subsysOffset = new char[subsysOffsetLen]; // Zeros for us.
735
    memset( subsysOffset, 0x00, subsysOffsetLen );
755
    memset( subsysOffset, 0x00, subsysOffsetLen );
736
    ver = new char[verLen];
756
    ver = new char[verLen];
737
    ver[0] = 0x00;
757
    ver[0] = 0x00;
738
    ver[1] = 0x01;
758
    ver[1] = 0x01;
739
 
759
 
740
    fwrite( text, 1, textLen, fp );
760
    fwrite( text, 1, textLen, fp );
741
    fwrite( subsysOffset, 1, subsysOffsetLen, fp );
761
    fwrite( subsysOffset, 1, subsysOffsetLen, fp );
742
    fwrite( ver, 1, verLen, fp );
762
    fwrite( ver, 1, verLen, fp );
743
    // Endian indicator. MI will show up as "MI" on big-endian
763
    // Endian indicator. MI will show up as "MI" on big-endian
744
    // systems and "IM" on little-endian systems.
764
    // systems and "IM" on little-endian systems.
745
    fwrite( &MI, 2, 1, fp );
765
    fwrite( &MI, 2, 1, fp );
746
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
766
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
747
 
767
 
748
    //===================Data element tag (8 bytes, page 1-8)==================
768
    //===================Data element tag (8 bytes, page 1-8)==================
749
    bytes = 16 + 24 + (8 + strlen(varName) + (8-(strlen(varName)%8))%8)
769
    bytes = 16 + 24 + (8 + strlen(varName) + (8-(strlen(varName)%8))%8)
750
            + (8 + rows*cols*chans*bytesPerElement);
770
            + (8 + rows*cols*chans*bytesPerElement);
751
    fwrite( &miMATRIX, 4, 1, fp ); // Data type.
771
    fwrite( &miMATRIX, 4, 1, fp ); // Data type.
752
    fwrite( &bytes, 4, 1, fp); // Data size in bytes.
772
    fwrite( &bytes, 4, 1, fp); // Data size in bytes.
753
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
773
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
754
 
774
 
755
    //====================Array flags (16 bytes, page 1-15)====================
775
    //====================Array flags (16 bytes, page 1-15)====================
756
    bytes = 8;
776
    bytes = 8;
757
    fwrite( &miUINT32, 4, 1, fp );
777
    fwrite( &miUINT32, 4, 1, fp );
758
    fwrite( &bytes, 4, 1, fp );
778
    fwrite( &bytes, 4, 1, fp );
759
    flags = 0x00; // Complex, logical, and global flags all off.
779
    flags = 0x00; // Complex, logical, and global flags all off.
760
 
780
 
761
    tmp32 = 0;
781
    tmp32 = 0;
762
    tmp32 = (flags << 8 ) | (mxClass);
782
    tmp32 = (flags << 8 ) | (mxClass);
763
    fwrite( &tmp32, 4, 1, fp );
783
    fwrite( &tmp32, 4, 1, fp );
764
 
784
 
765
    fwrite( &zero, 4, 1, fp ); // Padding to 64-bit boundary.
785
    fwrite( &zero, 4, 1, fp ); // Padding to 64-bit boundary.
766
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
786
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
767
 
787
 
768
    //===============Dimensions subelement (24 bytes, page 1-17)===============
788
    //===============Dimensions subelement (24 bytes, page 1-17)===============
769
    bytes = 12;
789
    bytes = 12;
770
    fwrite( &miINT32, 4, 1, fp );
790
    fwrite( &miINT32, 4, 1, fp );
771
    fwrite( &bytes, 4, 1, fp );
791
    fwrite( &bytes, 4, 1, fp );
772
 
792
 
773
    fwrite( &rows, 4, 1, fp );
793
    fwrite( &rows, 4, 1, fp );
774
    fwrite( &cols, 4, 1, fp );
794
    fwrite( &cols, 4, 1, fp );
775
    fwrite( &chans, 4, 1, fp );
795
    fwrite( &chans, 4, 1, fp );
776
    fwrite( &zero, 4, 1, fp ); // Padding to 64-bit boundary.
796
    fwrite( &zero, 4, 1, fp ); // Padding to 64-bit boundary.
777
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
797
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
778
 
798
 
779
    //==Array name (8 + strlen(varName) + (8-(strlen(varName)%8))%8 bytes, page 1-17)==
799
    //==Array name (8 + strlen(varName) + (8-(strlen(varName)%8))%8 bytes, page 1-17)==
780
    bytes = strlen(varName);
800
    bytes = strlen(varName);
781
 
801
 
782
    fwrite( &miINT8, 4, 1, fp );
802
    fwrite( &miINT8, 4, 1, fp );
783
    fwrite( &bytes, 4, 1, fp );
803
    fwrite( &bytes, 4, 1, fp );
784
    fwrite( varName, 1, bytes, fp );
804
    fwrite( varName, 1, bytes, fp );
785
 
805
 
786
    // Pad to nearest 64-bit boundary.
806
    // Pad to nearest 64-bit boundary.
787
    padBytes = (8-(bytes%8))%8;
807
    padBytes = (8-(bytes%8))%8;
788
    fwrite( &zero, 1, padBytes, fp );
808
    fwrite( &zero, 1, padBytes, fp );
789
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
809
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
790
 
810
 
791
    //====Matrix data (rows*cols*chans*bytesPerElement+8 bytes, page 1-20)=====
811
    //====Matrix data (rows*cols*chans*bytesPerElement+8 bytes, page 1-20)=====
792
    bytes = rows*cols*chans*bytesPerElement;
812
    bytes = rows*cols*chans*bytesPerElement;
793
    fwrite( &miClass, 4, 1, fp );
813
    fwrite( &miClass, 4, 1, fp );
794
    fwrite( &bytes, 4, 1, fp );
814
    fwrite( &bytes, 4, 1, fp );
795
 
815
 
796
    for( k = 0; k < chans; ++k )
816
    for( k = 0; k < chans; ++k )
797
    {
817
    {
798
        if( doBgrSwap )
818
        if( doBgrSwap )
799
        {
819
        {
800
            k2 = (k==0)? 2 : ((k==2)? 0 : 1);
820
            k2 = (k==0)? 2 : ((k==2)? 0 : 1);
801
        }
821
        }
802
        else
822
        else
803
            k2 = k;
823
            k2 = k;
804
 
824
 
805
        for( j = 0; j < cols; ++j )
825
        for( j = 0; j < cols; ++j )
806
        {
826
        {
807
            for( i = 0; i < rows; ++i )
827
            for( i = 0; i < rows; ++i )
808
            {
828
            {
809
                rowPtr = mat.data + mat.step*i;
829
                rowPtr = mat.data + mat.step*i;
810
                fwrite( rowPtr + (chans*j + k2)*bytesPerElement, bytesPerElement, 1, fp );
830
                fwrite( rowPtr + (chans*j + k2)*bytesPerElement, bytesPerElement, 1, fp );
811
            }
831
            }
812
        }
832
        }
813
    }
833
    }
814
 
834
 
815
    // Pad to 64-bit boundary.
835
    // Pad to 64-bit boundary.
816
    padBytes = (8-(bytes%8))%8;
836
    padBytes = (8-(bytes%8))%8;
817
    fwrite( &zero, 1, padBytes, fp );
837
    fwrite( &zero, 1, padBytes, fp );
818
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
838
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
819
 
839
 
820
    fclose(fp);
840
    fclose(fp);
821
    delete[] text;
841
    delete[] text;
822
    delete[] subsysOffset;
842
    delete[] subsysOffset;
823
    delete[] ver;
843
    delete[] ver;
824
}
844
}
825
 
845
 
826
 
846
 
827
 
847
 
828
 
848
 
829
 
849
 
830
}
850
}
831
 
851