Subversion Repositories seema-scanner

Rev

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

Rev 43 Rev 44
1
#include "AlgorithmGrayCode.h"
1
#include "AlgorithmGrayCode.h"
2
#include <cmath>
2
#include <cmath>
3
#include "cvtools.h"
3
#include "cvtools.h"
4
 
4
 
5
#ifndef log2f
5
#ifndef log2f
6
#define log2f(x) (log(x)/log(2.0))
6
#define log2f(x) (log(x)/log(2.0))
7
#endif
7
#endif
8
 
8
 
9
//using namespace std;
9
//using namespace std;
10
 
10
 
11
/*
11
/*
12
 * The purpose of this function is to convert an unsigned
12
 * The purpose of this function is to convert an unsigned
13
 * binary number to reflected binary Gray code.
13
 * binary number to reflected binary Gray code.
14
 *
14
 *
15
 * The operator >> is shift right. The operator ^ is exclusive or.
15
 * The operator >> is shift right. The operator ^ is exclusive or.
16
 * Source: http://en.wikipedia.org/wiki/Gray_code
16
 * Source: http://en.wikipedia.org/wiki/Gray_code
17
 */
17
 */
18
static unsigned int binaryToGray(unsigned int num) {
18
static unsigned int binaryToGray(unsigned int num) {
19
    return (num >> 1) ^ num;
19
    return (num >> 1) ^ num;
20
}
20
}
21
 
21
 
22
/*
22
/*
23
 * From Wikipedia: http://en.wikipedia.org/wiki/Gray_code
23
 * From Wikipedia: http://en.wikipedia.org/wiki/Gray_code
24
 * The purpose of this function is to convert a reflected binary
24
 * The purpose of this function is to convert a reflected binary
25
 * Gray code number to a binary number.
25
 * Gray code number to a binary number.
26
 */
26
 */
27
static unsigned grayToBinary(unsigned num, unsigned numBits)
27
static unsigned grayToBinary(unsigned num, unsigned numBits)
28
{
28
{
29
    for (unsigned shift = 1; shift < numBits; shift <<= 1){
29
    for (unsigned shift = 1; shift < numBits; shift <<= 1){
30
        num ^= num >> shift;
30
        num ^= num >> shift;
31
    }
31
    }
32
    return num;
32
    return num;
33
}
33
}
34
 
34
 
35
/*
35
/*
36
 * Function takes the decimal number
36
 * Function takes the decimal number
37
 * Function takes the Nth bit (1 to 31)
37
 * Function takes the Nth bit (1 to 31)
38
 * Return the value of Nth bit from decimal
38
 * Return the value of Nth bit from decimal
39
 * Source: http://icfun.blogspot.com/2009/04/get-n-th-bit-value-of-any-integer.html
39
 * Source: http://icfun.blogspot.com/2009/04/get-n-th-bit-value-of-any-integer.html
40
 */
40
 */
41
static int get_bit(int decimal, int N){
41
static int get_bit(int decimal, int N){
42
 
42
 
43
    // Shifting the 1 for N-1 bits
43
    // Shifting the 1 for N-1 bits
44
    int constant = 1 << (N-1);
44
    int constant = 1 << (N-1);
45
 
45
 
46
    // If the bit is set, return 1
46
    // If the bit is set, return 1
47
    if( decimal & constant ){
47
    if( decimal & constant ){
48
        return 1;
48
        return 1;
49
    }
49
    }
50
 
50
 
51
    // If the bit is not set, return 0
51
    // If the bit is not set, return 0
52
    return 0;
52
    return 0;
53
}
53
}
54
 
54
 
55
static inline int powi(int num, unsigned int exponent){
55
static inline int powi(int num, unsigned int exponent){
56
    // NOT EQUIVALENT TO pow()
56
    // NOT EQUIVALENT TO pow()
57
    if(exponent == 0)
57
    if(exponent == 0)
58
        return 1;
58
        return 1;
59
 
59
 
60
    float res = num;
60
    float res = num;
61
    for(unsigned int i=0; i<exponent-1; i++)
61
    for(unsigned int i=0; i<exponent-1; i++)
62
        res *= num;
62
        res *= num;
63
 
63
 
64
    return res;
64
    return res;
65
}
65
}
66
 
66
 
67
// Algorithm
67
// Algorithm
68
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodingDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
68
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodingDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
69
 
69
 
70
    // on/off patterns
70
    // on/off patterns
71
    Nbits = ceilf(log2f((float)screenCols));
71
    Nbits = ceilf(log2f((float)screenCols));
72
    N = 2 + Nbits*2;
72
    N = 2 + Nbits*2;
73
 
73
 
74
    // all on pattern
74
    // all on pattern
75
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
75
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
76
    patterns.push_back(allOn);
76
    patterns.push_back(allOn);
77
 
77
 
78
    // all off pattern
78
    // all off pattern
79
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
79
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
80
    patterns.push_back(allOff);
80
    patterns.push_back(allOff);
81
 
81
 
82
 
82
 
83
    // horizontally encoding patterns
83
    // horizontally encoding patterns
84
    for(unsigned int p=0; p<Nbits; p++){
84
    for(unsigned int p=0; p<Nbits; p++){
85
        cv::Mat pattern(1, screenCols, CV_8UC3);
85
        cv::Mat pattern(1, screenCols, CV_8UC3);
86
        cv::Mat patternInv(1, screenCols, CV_8UC3);
86
        cv::Mat patternInv(1, screenCols, CV_8UC3);
87
 
87
 
88
        for(unsigned int j=0; j<screenCols; j++){
88
        for(unsigned int j=0; j<screenCols; j++){
89
 
89
 
90
            unsigned int jGray = binaryToGray(j);
90
            unsigned int jGray = binaryToGray(j);
91
            // Amplitude of channels
91
            // Amplitude of channels
92
            int bit = get_bit(jGray, Nbits-p);
92
            int bit = get_bit(jGray, Nbits-p);
93
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
93
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
94
            int invBit = bit^1;
94
            int invBit = bit^1;
95
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
95
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
96
        }
96
        }
97
        patterns.push_back(pattern);
97
        patterns.push_back(pattern);
98
        patterns.push_back(patternInv);
98
        patterns.push_back(patternInv);
99
    }
99
    }
100
 
100
 
101
 
101
 
102
}
102
}
103
 
103
 
104
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
104
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
105
    return patterns[depth];
105
    return patterns[depth];
106
}
106
}
107
 
107
 
108
 
108
 
109
bool sortingLarger(cv::Vec4f i,cv::Vec4f j){ return (i[3]<j[3]);}
109
bool sortingLarger(cv::Vec4f i,cv::Vec4f j){ return (i[3]<j[3]);}
110
bool sortingEqual(cv::Vec4f i,cv::Vec4f j){ return (i[3]==j[3]);}
110
bool sortingEqual(cv::Vec4f i,cv::Vec4f j){ return (i[3]==j[3]);}
111
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4f>& edges){
111
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4f>& edges){
112
 
112
 
113
    int nCols = scanLine.cols;
113
    int nCols = scanLine.cols;
114
    const short *data = scanLine.ptr<const short>(0);
114
    const short *data = scanLine.ptr<const short>(0);
115
 
115
 
116
    short labelLeft;
116
    short labelLeft;
117
    short labelRight = data[0];
117
    short labelRight = data[0];
118
 
118
 
119
    // collect edges
119
    // collect edges
120
    for(int col=1; col<nCols; col++){
120
    for(int col=1; col<nCols; col++){
121
 
121
 
122
        labelLeft = labelRight;
122
        labelLeft = labelRight;
123
        labelRight = data[col];
123
        labelRight = data[col];
124
 
124
 
125
        if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
125
        if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
126
            int orderingRelation = (labelLeft << Nbits) + labelRight;
126
            int orderingRelation = (labelLeft << Nbits) + labelRight;
127
            edges.push_back(cv::Vec4f(col-0.5, labelLeft, labelRight, orderingRelation));
127
            edges.push_back(cv::Vec4f(col-0.5, labelLeft, labelRight, orderingRelation));
128
        }
128
        }
129
    }
129
    }
130
 
130
 
131
    // sort
131
    // sort
132
    std::sort(edges.begin(), edges.end(), sortingLarger);
132
    std::sort(edges.begin(), edges.end(), sortingLarger);
133
 
133
 
134
    // remove duplicates
134
    // remove duplicates
135
    std::vector<cv::Vec4f>::iterator it;
135
    std::vector<cv::Vec4f>::iterator it;
136
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
136
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
137
    edges.resize(std::distance(edges.begin(),it));
137
    edges.resize(std::distance(edges.begin(),it));
138
}
138
}
139
 
139
 
140
void AlgorithmGrayCode::get3DPoints(SMCalibrationParameters calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point3f>& Q, std::vector<cv::Vec3b>& color){
140
void AlgorithmGrayCode::get3DPoints(SMCalibrationParameters calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point3f>& Q, std::vector<cv::Vec3b>& color){
141
 
141
 
142
    assert(frames0.size() == N);
142
    assert(frames0.size() == N);
143
    assert(frames1.size() == N);
143
    assert(frames1.size() == N);
144
 
144
 
145
    int frameRows = frames0[0].rows;
145
    int frameRows = frames0[0].rows;
146
    int frameCols = frames0[0].cols;
146
    int frameCols = frames0[0].cols;
147
 
147
 
148
    // rectifying homographies (rotation+projections)
148
    // rectifying homographies (rotation+projections)
149
    cv::Size frameSize(frameCols, frameRows);
149
    cv::Size frameSize(frameCols, frameRows);
150
    cv::Mat R, T;
150
    cv::Mat R, T;
151
    // stereoRectify segfaults unless R is double precision
151
    // stereoRectify segfaults unless R is double precision
152
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
152
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
153
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
153
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
154
    cv::Mat R0, R1, P0, P1, QRect;
154
    cv::Mat R0, R1, P0, P1, QRect;
155
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
155
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
156
 
156
 
157
    std::cout << "R0" << std::endl << R0 << std::endl;
157
    std::cout << "R0" << std::endl << R0 << std::endl;
158
    std::cout << "P0" << std::endl << P0 << std::endl;
158
    std::cout << "P0" << std::endl << P0 << std::endl;
159
    std::cout << "R1" << std::endl << R1 << std::endl;
159
    std::cout << "R1" << std::endl << R1 << std::endl;
160
    std::cout << "P1" << std::endl << P1 << std::endl;
160
    std::cout << "P1" << std::endl << P1 << std::endl;
161
 
161
 
162
    // interpolation maps
162
    // interpolation maps
163
    cv::Mat map0X, map0Y, map1X, map1Y;
163
    cv::Mat map0X, map0Y, map1X, map1Y;
164
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
164
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
165
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
165
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
166
 
166
 
167
    // gray-scale and remap
167
    // gray-scale and remap
168
    std::vector<cv::Mat> frames0Rect(N);
168
    std::vector<cv::Mat> frames0Rect(N);
169
    std::vector<cv::Mat> frames1Rect(N);
169
    std::vector<cv::Mat> frames1Rect(N);
170
    for(int i=0; i<N; i++){
170
    for(int i=0; i<N; i++){
171
        cv::Mat temp;
171
        cv::Mat temp;
172
        cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
172
        cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
173
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
173
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
174
        cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
174
        cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
175
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
175
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
176
    }
176
    }
177
 
177
 
178
    // color remaps
178
    // color remaps
179
    cv::Mat color0Rect, color1Rect;
179
    cv::Mat color0Rect, color1Rect;
180
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
180
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
181
    cv::remap(frames1[0], color1Rect, map0X, map0Y, CV_INTER_CUBIC);
181
    cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
182
 
182
 
183
    int frameRectRows = frames0Rect[0].rows;
183
    int frameRectRows = frames0Rect[0].rows;
184
    int frameRectCols = frames0Rect[0].cols;
184
    int frameRectCols = frames0Rect[0].cols;
185
 
185
 
186
    // occlusion maps
186
    // occlusion maps
187
    cv::Mat occlusion0Rect, occlusion1Rect;
187
    cv::Mat occlusion0Rect, occlusion1Rect;
188
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
188
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
189
    occlusion0Rect = occlusion0Rect > 50;
189
    occlusion0Rect = occlusion0Rect > 50;
190
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
190
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
191
    occlusion1Rect = occlusion1Rect > 50;
191
    occlusion1Rect = occlusion1Rect > 50;
192
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
192
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
193
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
193
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
194
 
194
 
195
    // decode patterns
195
    // decode patterns
196
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_16S, cv::Scalar(-1));
196
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_16S, cv::Scalar(-1));
197
    cv::Mat code1Rect(frameRectRows, frameRectCols, CV_16S, cv::Scalar(-1));
197
    cv::Mat code1Rect(frameRectRows, frameRectCols, CV_16S, cv::Scalar(-1));
198
    cv::add(code0Rect, 1, code0Rect, occlusion0Rect, CV_16S);
198
    cv::add(code0Rect, 1, code0Rect, occlusion0Rect, CV_16S);
199
    cv::add(code1Rect, 1, code1Rect, occlusion1Rect, CV_16S);
199
    cv::add(code1Rect, 1, code1Rect, occlusion1Rect, CV_16S);
200
 
200
 
201
    for(int i=0; i<Nbits; i++){
201
    for(int i=0; i<Nbits; i++){
202
        cv::Mat bit0;
202
        cv::Mat bit0;
203
        cv::subtract(frames0Rect[i*2+2], frames0Rect[i*2+3], bit0, cv::noArray(), CV_16S);
203
        cv::subtract(frames0Rect[i*2+2], frames0Rect[i*2+3], bit0, cv::noArray(), CV_16S);
204
        bit0 = bit0 > 0;
204
        bit0 = bit0 > 0;
205
//    cvtools::writeMat(bit0, "bit0.mat", "bit0");
205
//    cvtools::writeMat(bit0, "bit0.mat", "bit0");
206
        cv::add(code0Rect, bit0/255*powi(2,i), code0Rect, occlusion0Rect, CV_16S);
206
        cv::add(code0Rect, bit0/255*powi(2,i), code0Rect, occlusion0Rect, CV_16S);
207
//    cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
207
//    cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
208
        cv::Mat bit1;
208
        cv::Mat bit1;
209
        cv::subtract(frames1Rect[i*2+2], frames1Rect[i*2+3], bit1, cv::noArray(), CV_16S);
209
        cv::subtract(frames1Rect[i*2+2], frames1Rect[i*2+3], bit1, cv::noArray(), CV_16S);
210
        bit1 = bit1 > 0;
210
        bit1 = bit1 > 0;
211
        cv::add(code1Rect, bit1/255*powi(2,i), code1Rect, occlusion1Rect, CV_16S);
211
        cv::add(code1Rect, bit1/255*powi(2,i), code1Rect, occlusion1Rect, CV_16S);
212
    }
212
    }
213
 
213
 
214
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
214
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
215
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
215
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
216
 
216
 
217
    // matching
217
    // matching
218
    std::vector<cv::Vec2f> q0Rect, q1Rect;
218
    std::vector<cv::Vec2f> q0Rect, q1Rect;
219
    for(int row=0; row<frameRectRows; row++){
219
    for(int row=0; row<frameRectRows; row++){
220
 
220
 
221
        std::vector<cv::Vec4f> edges0, edges1;
221
        std::vector<cv::Vec4f> edges0, edges1;
222
 
222
 
223
        // sorted, unique edges
223
        // sorted, unique edges
224
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
224
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
225
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
225
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
226
 
226
 
227
        // subpixel refinement
227
        // subpixel refinement
228
        // ...
228
        // ...
229
 
229
 
230
        int i=0, j=0;
230
        int i=0, j=0;
231
        while(i<edges0.size() && j<edges1.size()){
231
        while(i<edges0.size() && j<edges1.size()){
232
 
232
 
233
            if(edges0[i][3] == edges1[j][3]){
233
            if(edges0[i][3] == edges1[j][3]){
234
                q0Rect.push_back(cv::Vec2f(edges0[i][0], row));
234
                q0Rect.push_back(cv::Vec2f(edges0[i][0], row));
235
                q1Rect.push_back(cv::Vec2f(edges1[j][0], row));
235
                q1Rect.push_back(cv::Vec2f(edges1[j][0], row));
236
                i += 1;
236
                i += 1;
237
                j += 1;
237
                j += 1;
238
            } else if(edges0[i][3] < edges1[j][3]){
238
            } else if(edges0[i][3] < edges1[j][3]){
239
                i += 1;
239
                i += 1;
240
            } else if(edges0[i][3] > edges1[j][3]){
240
            } else if(edges0[i][3] > edges1[j][3]){
241
                j += 1;
241
                j += 1;
242
            }
242
            }
243
        }
243
        }
244
 
244
 
245
 
245
 
246
    }
246
    }
247
 
247
 
248
    // retrieve color information
248
    // retrieve color information
249
    int nMatches = q0Rect.size();
249
    int nMatches = q0Rect.size();
250
    color.resize(nMatches);
250
    color.resize(nMatches);
251
    for(int i=0; i<nMatches; i++){
251
    for(int i=0; i<nMatches; i++){
252
 
252
 
253
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
253
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
254
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
254
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
255
 
255
 
256
        color[i] = 0.5*(c0 + c1);
256
        color[i] = 0.5*c0 + 0.5*c1;
257
    }
257
    }
258
 
258
 
259
    // triangulate points
259
    // triangulate points
260
    cv::Mat QMatHomogenous, QMat;
260
    cv::Mat QMatHomogenous, QMat;
-
 
261
//    cv::Mat C0 = P0.clone();
-
 
262
//    cv::Mat C1 = P1.clone();
-
 
263
//    C0.colRange(0, 3) = C0.colRange(0, 3)*R0;
-
 
264
//    C1.colRange(0, 3) = C1.colRange(0, 3)*R1.t();
261
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
265
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
262
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
266
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
-
 
267
 
-
 
268
    // undo rectification
-
 
269
    cv::Mat R0Inv;
-
 
270
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
-
 
271
    QMat = R0Inv*QMat;
-
 
272
 
263
    cvtools::matToPoints3f(QMat, Q);
273
    cvtools::matToPoints3f(QMat, Q);
-
 
274
 
264
}
275
}
265
 
276