Subversion Repositories seema-scanner

Rev

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

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