Subversion Repositories seema-scanner

Rev

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

Rev 42 Rev 43
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
    for(int col=1; col<nCols; col++){
120
    for(int col=1; col<nCols; col++){
120
 
121
 
121
        labelLeft = labelRight;
122
        labelLeft = labelRight;
122
        labelRight = data[col];
123
        labelRight = data[col];
123
 
124
 
124
        if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
125
        if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
125
 
-
 
126
            int orderingRelation = (2 << Nbits)*labelLeft + labelRight;
126
            int orderingRelation = (labelLeft << Nbits) + labelRight;
127
 
-
 
128
            edges.push_back(cv::Vec4f(col, labelLeft, labelRight, orderingRelation));
127
            edges.push_back(cv::Vec4f(col-0.5, labelLeft, labelRight, orderingRelation));
129
 
-
 
130
        }
128
        }
131
    }
129
    }
132
 
130
 
133
    // sort
131
    // sort
134
    std::sort(edges.begin(), edges.end(), sortingLarger);
132
    std::sort(edges.begin(), edges.end(), sortingLarger);
135
 
133
 
136
    // remove duplicates
134
    // remove duplicates
137
    std::vector<cv::Vec4f>::iterator it;
135
    std::vector<cv::Vec4f>::iterator it;
138
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
136
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
139
    edges.resize(std::distance(edges.begin(),it));
137
    edges.resize(std::distance(edges.begin(),it));
140
}
138
}
141
 
139
 
142
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){
143
 
141
 
144
    assert(frames0.size() == N);
142
    assert(frames0.size() == N);
145
    assert(frames1.size() == N);
143
    assert(frames1.size() == N);
146
 
144
 
147
    int frameRows = frames0[0].rows;
145
    int frameRows = frames0[0].rows;
148
    int frameCols = frames0[0].cols;
146
    int frameCols = frames0[0].cols;
149
 
147
 
150
    // convert to gray-scale
-
 
151
    std::vector<cv::Mat> frames0Gray(N);
-
 
152
    std::vector<cv::Mat> frames1Gray(N);
-
 
153
    for(int i=0; i<N; i++){
-
 
154
        cv::cvtColor(frames0[i], frames0Gray[i], CV_RGB2GRAY);
-
 
155
        cv::cvtColor(frames1[i], frames1Gray[i], CV_RGB2GRAY);
-
 
156
    }
-
 
157
 
-
 
158
    // occlusion maps
-
 
159
    cv::Mat occlusion0, occlusion1;
-
 
160
    cv::subtract(frames0Gray[0], frames0Gray[1], occlusion0);
-
 
161
    occlusion0 = occlusion0 > 50;
-
 
162
    cv::subtract(frames1Gray[0], frames1Gray[1], occlusion1);
-
 
163
    occlusion1 = occlusion1 > 50;
-
 
164
 
-
 
165
//    cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
-
 
166
//    cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
-
 
167
 
-
 
168
    // decoded patterns
-
 
169
    cv::Mat code0(frameRows, frameCols, CV_16S, cv::Scalar(-1)), code1(frameRows, frameCols, CV_16S, cv::Scalar(-1));
-
 
170
    cvtools::writeMat(code0, "code0.mat", "code0");
-
 
171
    cvtools::writeMat(code1, "code1.mat", "code1");
-
 
172
    for(int i=0; i<Nbits; i++){
-
 
173
        cv::Mat bit0;
-
 
174
        cv::subtract(frames0Gray[i*2+2], frames0Gray[i*2+3], bit0);
-
 
175
        bit0 = bit0 > 50;
-
 
176
//    cvtools::writeMat(bit0, "bit0.mat", "bit0");
-
 
177
        cv::add(code0, bit0/255*powi(2,i), code0, occlusion0, CV_16S);
-
 
178
//    cvtools::writeMat(code0, "code0.mat", "code0");
-
 
179
        cv::Mat bit1;
-
 
180
        cv::subtract(frames1Gray[i*2+2], frames1Gray[i*2+3], bit1);
-
 
181
        bit1 = bit1 > 50;
-
 
182
        cv::add(code1, bit1/255*powi(2,i), code1, occlusion1, CV_16S);
-
 
183
    }
-
 
184
 
-
 
185
    cvtools::writeMat(code0, "code0.mat", "code0");
-
 
186
    cvtools::writeMat(code1, "code1.mat", "code1");
-
 
187
 
-
 
188
    // rectifying homographies (rotation+projections)
148
    // rectifying homographies (rotation+projections)
189
    cv::Size frameSize(frameCols, frameRows);
149
    cv::Size frameSize(frameCols, frameRows);
190
    cv::Mat R, T;
150
    cv::Mat R, T;
191
    // stereoRectify segfaults unless R is double precision
151
    // stereoRectify segfaults unless R is double precision
192
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
152
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
193
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
153
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
194
    cv::Mat R0, R1, P0, P1, QRect;
154
    cv::Mat R0, R1, P0, P1, QRect;
195
    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);
196
 
156
 
-
 
157
    std::cout << "R0" << std::endl << R0 << std::endl;
-
 
158
    std::cout << "P0" << std::endl << P0 << std::endl;
-
 
159
    std::cout << "R1" << std::endl << R1 << std::endl;
-
 
160
    std::cout << "P1" << std::endl << P1 << std::endl;
-
 
161
 
197
    // interpolation maps
162
    // interpolation maps
198
    cv::Mat map0X, map0Y, map1X, map1Y;
163
    cv::Mat map0X, map0Y, map1X, map1Y;
199
    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);
200
    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);
201
 
166
 
202
    // remap
167
    // gray-scale and remap
-
 
168
    std::vector<cv::Mat> frames0Rect(N);
203
    cv::Mat code0Rect, code1Rect, color0Rect, color1Rect;
169
    std::vector<cv::Mat> frames1Rect(N);
-
 
170
    for(int i=0; i<N; i++){
-
 
171
        cv::Mat temp;
204
    cv::remap(code0, code0Rect, map0X, map0Y, cv::INTER_NEAREST);
172
        cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
205
    cv::remap(code1, code1Rect, map1X, map1Y, cv::INTER_NEAREST);
173
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
206
    cv::remap(frames0[0], color0Rect, map0X, map0Y, cv::INTER_CUBIC);
174
        cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
207
    cv::remap(frames1[0], color1Rect, map1X, map1Y, cv::INTER_CUBIC);
175
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
-
 
176
    }
208
 
177
 
-
 
178
    // color remaps
-
 
179
    cv::Mat color0Rect, color1Rect;
209
cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
180
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
210
cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
181
    cv::remap(frames1[0], color1Rect, map0X, map0Y, CV_INTER_CUBIC);
211
 
182
 
212
//cvtools::writeMat(color0Rect, "color0.mat", "color0");
183
    int frameRectRows = frames0Rect[0].rows;
213
//cvtools::writeMat(color1Rect, "color1.mat", "color1");
184
    int frameRectCols = frames0Rect[0].cols;
214
 
185
 
-
 
186
    // occlusion maps
-
 
187
    cv::Mat occlusion0Rect, occlusion1Rect;
-
 
188
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
215
    int nRows = code0Rect.rows;
189
    occlusion0Rect = occlusion0Rect > 50;
-
 
190
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
-
 
191
    occlusion1Rect = occlusion1Rect > 50;
-
 
192
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
-
 
193
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
-
 
194
 
216
    int nCols = code0Rect.cols;
195
    // decode patterns
-
 
196
    cv::Mat code0Rect(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);
-
 
199
    cv::add(code1Rect, 1, code1Rect, occlusion1Rect, CV_16S);
-
 
200
 
-
 
201
    for(int i=0; i<Nbits; i++){
-
 
202
        cv::Mat bit0;
-
 
203
        cv::subtract(frames0Rect[i*2+2], frames0Rect[i*2+3], bit0, cv::noArray(), CV_16S);
-
 
204
        bit0 = bit0 > 0;
-
 
205
//    cvtools::writeMat(bit0, "bit0.mat", "bit0");
-
 
206
        cv::add(code0Rect, bit0/255*powi(2,i), code0Rect, occlusion0Rect, CV_16S);
-
 
207
//    cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
-
 
208
        cv::Mat bit1;
-
 
209
        cv::subtract(frames1Rect[i*2+2], frames1Rect[i*2+3], bit1, cv::noArray(), CV_16S);
-
 
210
        bit1 = bit1 > 0;
-
 
211
        cv::add(code1Rect, bit1/255*powi(2,i), code1Rect, occlusion1Rect, CV_16S);
-
 
212
    }
-
 
213
 
-
 
214
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
-
 
215
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
217
 
216
 
218
    // matching
217
    // matching
219
    std::vector<cv::Vec2f> q0Rect, q1Rect;
218
    std::vector<cv::Vec2f> q0Rect, q1Rect;
220
    for(int row=0; row<nRows; row++){
219
    for(int row=0; row<frameRectRows; row++){
221
 
220
 
222
        std::vector<cv::Vec4f> edges0, edges1;
221
        std::vector<cv::Vec4f> edges0, edges1;
223
 
222
 
-
 
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
-
 
228
        // ...
-
 
229
 
227
        int i=0, j=0;
230
        int i=0, j=0;
228
        while(i<edges0.size() && j<edges1.size()){
231
        while(i<edges0.size() && j<edges1.size()){
229
 
232
 
230
            if(edges0[i][3] == edges1[j][3]){
233
            if(edges0[i][3] == edges1[j][3]){
231
                q0Rect.push_back(cv::Vec2f(edges0[i][0], row));
234
                q0Rect.push_back(cv::Vec2f(edges0[i][0], row));
232
                q1Rect.push_back(cv::Vec2f(edges1[j][0], row));
235
                q1Rect.push_back(cv::Vec2f(edges1[j][0], row));
233
                i += 1;
236
                i += 1;
234
                j += 1;
237
                j += 1;
235
            } else if(edges0[i][3] < edges1[j][3]){
238
            } else if(edges0[i][3] < edges1[j][3]){
236
                i += 1;
239
                i += 1;
237
            } else if(edges0[i][3] > edges1[j][3]){
240
            } else if(edges0[i][3] > edges1[j][3]){
238
                j += 1;
241
                j += 1;
239
            }
242
            }
240
        }
243
        }
241
 
244
 
242
 
245
 
243
    }
246
    }
244
 
247
 
245
    // retrieve color information
248
    // retrieve color information
246
    int nMatches = q0Rect.size();
249
    int nMatches = q0Rect.size();
247
    color.resize(nMatches);
250
    color.resize(nMatches);
248
    for(int i=0; i<nMatches; i++){
251
    for(int i=0; i<nMatches; i++){
249
 
252
 
250
        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]);
251
        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]);
252
 
255
 
253
        color[i] = 0.5*(c0 + c1);
256
        color[i] = 0.5*(c0 + c1);
254
    }
257
    }
255
 
258
 
256
    // triangulate points
259
    // triangulate points
257
    cv::Mat QMatHomogenous, QMat;
260
    cv::Mat QMatHomogenous, QMat;
258
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
261
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
259
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
262
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
260
    cvtools::matToPoints3f(QMat, Q);
263
    cvtools::matToPoints3f(QMat, Q);
261
}
264
}
262
 
265