Subversion Repositories seema-scanner

Rev

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

Rev 45 Rev 47
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 int grayToBinary(unsigned int num){
27
static unsigned int grayToBinary(unsigned int num){
28
    unsigned int mask;
28
    unsigned int mask;
29
    for(mask = num >> 1; mask != 0; mask = mask >> 1)
29
    for(mask = num >> 1; mask != 0; mask = mask >> 1)
30
        num = num ^ mask;
30
        num = num ^ mask;
31
    return num;
31
    return num;
32
}
32
}
33
 
33
 
34
/*
34
/*
35
 * Return the Nth bit of an unsigned integer number
35
 * Return the Nth bit of an unsigned integer number
36
 */
36
 */
37
static bool getBit(int decimal, int N){
37
static bool getBit(int decimal, int N){
38
 
38
 
39
    return decimal & 1 << (N-1);
39
    return decimal & 1 << (N-1);
40
}
40
}
41
 
41
 
-
 
42
/*
-
 
43
 * Return the number of bits set in an integer
-
 
44
 */
-
 
45
static int countBits(int n) {
-
 
46
  unsigned int c; // c accumulates the total bits set in v
-
 
47
  for (c = 0; n>0; c++)
-
 
48
    n &= n - 1; // clear the least significant bit set
-
 
49
  return c;
-
 
50
}
-
 
51
 
-
 
52
/*
-
 
53
 * Return the position of the least significant bit that is set
-
 
54
 */
-
 
55
static int leastSignificantBitSet(int x){
-
 
56
  if(x == 0)
-
 
57
      return 0;
-
 
58
 
-
 
59
  int val = 1;
-
 
60
  while(x>>=1)
-
 
61
      val++;
-
 
62
 
-
 
63
  return val;
-
 
64
}
-
 
65
 
42
//static int get_bit(int decimal, int N){
66
//static int get_bit(int decimal, int N){
43
 
67
 
44
//    // Shifting the 1 for N-1 bits
68
//    // Shifting the 1 for N-1 bits
45
//    int constant = 1 << (N-1);
69
//    int constant = 1 << (N-1);
46
 
70
 
47
//    // If the bit is set, return 1
71
//    // If the bit is set, return 1
48
//    if( decimal & constant )
72
//    if( decimal & constant )
49
//        return 1;
73
//        return 1;
50
//    else
74
//    else
51
//        return 0;
75
//        return 0;
52
//}
76
//}
53
 
77
 
54
static inline unsigned int powi(int num, unsigned int exponent){
78
static inline unsigned int powi(int num, unsigned int exponent){
55
 
79
 
56
    if(exponent == 0)
80
    if(exponent == 0)
57
        return 1;
81
        return 1;
58
 
82
 
59
    float res = num;
83
    float res = num;
60
    for(unsigned int i=0; i<exponent-1; i++)
84
    for(unsigned int i=0; i<exponent-1; i++)
61
        res *= num;
85
        res *= num;
62
 
86
 
63
    return res;
87
    return res;
64
}
88
}
65
 
89
 
66
static inline unsigned int twopowi(unsigned int exponent){
90
static inline unsigned int twopowi(unsigned int exponent){
67
 
91
 
68
    return 1 << exponent;
92
    return 1 << exponent;
69
}
93
}
70
 
94
 
71
// Algorithm
95
// Algorithm
72
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodingDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
96
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodingDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
73
 
97
 
74
    // on/off patterns
98
    // on/off patterns
75
    Nbits = ceilf(log2f((float)screenCols));
99
    Nbits = ceilf(log2f((float)screenCols));
76
    N = 2 + Nbits*2;
100
    N = 2 + Nbits*2;
77
 
101
 
78
    // all on pattern
102
    // all on pattern
79
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
103
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
80
    patterns.push_back(allOn);
104
    patterns.push_back(allOn);
81
 
105
 
82
    // all off pattern
106
    // all off pattern
83
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
107
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
84
    patterns.push_back(allOff);
108
    patterns.push_back(allOff);
85
 
109
 
86
 
110
 
87
    // horizontally encoding patterns
111
    // horizontally encoding patterns
88
    for(unsigned int p=0; p<Nbits; p++){
112
    for(unsigned int p=0; p<Nbits; p++){
89
        cv::Mat pattern(1, screenCols, CV_8UC3);
113
        cv::Mat pattern(1, screenCols, CV_8UC3);
90
        cv::Mat patternInv(1, screenCols, CV_8UC3);
114
        cv::Mat patternInv(1, screenCols, CV_8UC3);
91
 
115
 
92
        for(unsigned int j=0; j<screenCols; j++){
116
        for(unsigned int j=0; j<screenCols; j++){
93
 
117
 
94
            unsigned int jGray = binaryToGray(j);
118
            unsigned int jGray = binaryToGray(j);
95
            // Amplitude of channels
119
            // Amplitude of channels
96
            int bit = (int)getBit(jGray, Nbits-p);
120
            int bit = (int)getBit(jGray, Nbits-p);
97
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
121
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
98
            int invBit = bit^1;
122
            int invBit = bit^1;
99
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
123
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
100
        }
124
        }
101
        patterns.push_back(pattern);
125
        patterns.push_back(pattern);
102
        patterns.push_back(patternInv);
126
        patterns.push_back(patternInv);
103
    }
127
    }
104
 
128
 
105
 
129
 
106
}
130
}
107
 
131
 
108
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
132
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
109
    return patterns[depth];
133
    return patterns[depth];
110
}
134
}
111
 
135
 
112
 
136
 
113
bool sortingLarger(cv::Vec4f i,cv::Vec4f j){ return (i[3]<j[3]);}
137
bool sortingLarger(cv::Vec4i i,cv::Vec4i j){ return (i[3]<j[3]);}
114
bool sortingEqual(cv::Vec4f i,cv::Vec4f j){ return (i[3]==j[3]);}
138
bool sortingEqual(cv::Vec4i i,cv::Vec4i j){ return (i[3]==j[3]);}
115
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4f>& edges){
139
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4i>& edges){
116
 
140
 
117
    int nCols = scanLine.cols;
141
    int nCols = scanLine.cols;
118
    const int *data = scanLine.ptr<const int>(0);
142
    const int *data = scanLine.ptr<const int>(0);
119
 
143
 
120
    int labelLeft;
144
    int labelLeft;
121
    int labelRight = data[0];
145
    int labelRight = data[0];
122
 
146
 
123
    // collect edges
147
    // collect edges
124
    for(int col=1; col<nCols; col++){
148
    for(int col=1; col<nCols; col++){
125
 
149
 
126
        labelLeft = labelRight;
150
        labelLeft = labelRight;
127
        labelRight = data[col];
151
        labelRight = data[col];
128
 
152
 
-
 
153
        // labels need to non-background, and differ in exactly one bit
129
        if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
154
        if(labelLeft != -1 && labelRight != -1 && countBits(labelLeft^labelRight) == 1){
130
            int orderingRelation = (labelLeft << Nbits) + labelRight;
155
            int orderingRelation = (labelLeft << Nbits) + labelRight;
-
 
156
            // store left label column
131
            edges.push_back(cv::Vec4f(col-0.5, labelLeft, labelRight, orderingRelation));
157
            edges.push_back(cv::Vec4i(col-1, labelLeft, labelRight, orderingRelation));
132
        }
158
        }
133
    }
159
    }
134
 
160
 
135
    // sort
161
    // sort
136
    std::sort(edges.begin(), edges.end(), sortingLarger);
162
    std::sort(edges.begin(), edges.end(), sortingLarger);
137
 
163
 
138
    // remove duplicates
164
    // remove duplicates
139
    std::vector<cv::Vec4f>::iterator it;
165
    std::vector<cv::Vec4i>::iterator it;
140
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
166
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
141
    edges.resize(std::distance(edges.begin(),it));
167
    edges.resize(std::distance(edges.begin(),it));
142
}
168
}
143
 
169
 
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){
170
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){
145
 
171
 
146
    assert(frames0.size() == N);
172
    assert(frames0.size() == N);
147
    assert(frames1.size() == N);
173
    assert(frames1.size() == N);
148
 
174
 
149
    for(int i=0; i<1920; i++){
175
//    for(int i=0; i<1920; i++){
150
        std::cout << i << " " << binaryToGray(i) << " " << grayToBinary(binaryToGray(i)) << std::endl;
176
//        std::cout << i << " " << binaryToGray(i) << " " << grayToBinary(binaryToGray(i)) << std::endl;
151
    }
177
//    }
152
 
-
 
153
 
178
 
154
    int frameRows = frames0[0].rows;
179
    int frameRows = frames0[0].rows;
155
    int frameCols = frames0[0].cols;
180
    int frameCols = frames0[0].cols;
156
 
181
 
157
    // rectifying homographies (rotation+projections)
182
    // rectifying homographies (rotation+projections)
158
    cv::Size frameSize(frameCols, frameRows);
183
    cv::Size frameSize(frameCols, frameRows);
159
    cv::Mat R, T;
184
    cv::Mat R, T;
160
    // stereoRectify segfaults unless R is double precision
185
    // stereoRectify segfaults unless R is double precision
161
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
186
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
162
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
187
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
163
    cv::Mat R0, R1, P0, P1, QRect;
188
    cv::Mat R0, R1, P0, P1, QRect;
164
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
189
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
165
 
190
 
166
    std::cout << "R0" << std::endl << R0 << std::endl;
191
//    std::cout << "R0" << std::endl << R0 << std::endl;
167
    std::cout << "P0" << std::endl << P0 << std::endl;
192
//    std::cout << "P0" << std::endl << P0 << std::endl;
168
    std::cout << "R1" << std::endl << R1 << std::endl;
193
//    std::cout << "R1" << std::endl << R1 << std::endl;
169
    std::cout << "P1" << std::endl << P1 << std::endl;
194
//    std::cout << "P1" << std::endl << P1 << std::endl;
170
 
195
 
171
    // interpolation maps
196
    // interpolation maps
172
    cv::Mat map0X, map0Y, map1X, map1Y;
197
    cv::Mat map0X, map0Y, map1X, map1Y;
173
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
198
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
174
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
199
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
175
 
200
 
176
    // gray-scale and remap
201
    // gray-scale and remap
177
    std::vector<cv::Mat> frames0Rect(N);
202
    std::vector<cv::Mat> frames0Rect(N);
178
    std::vector<cv::Mat> frames1Rect(N);
203
    std::vector<cv::Mat> frames1Rect(N);
179
    for(int i=0; i<N; i++){
204
    for(int i=0; i<N; i++){
180
        cv::Mat temp;
205
        cv::Mat temp;
181
        cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
206
        cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
182
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
207
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_CUBIC);
183
        cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
208
        cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
184
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
209
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_CUBIC);
185
    }
210
    }
186
 
211
 
-
 
212
//    cv::imwrite("frames0Rect[N-1].png", frames0Rect[N-1]);
-
 
213
 
187
    // color remaps
214
    // color remaps
188
    cv::Mat color0Rect, color1Rect;
215
    cv::Mat color0Rect, color1Rect;
189
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
216
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
190
    cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
217
    cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
191
 
218
 
192
    int frameRectRows = frames0Rect[0].rows;
219
    int frameRectRows = frames0Rect[0].rows;
193
    int frameRectCols = frames0Rect[0].cols;
220
    int frameRectCols = frames0Rect[0].cols;
194
 
221
 
195
    // occlusion maps
222
    // occlusion masks
196
    cv::Mat occlusion0Rect, occlusion1Rect;
223
    cv::Mat occlusion0Rect, occlusion1Rect;
197
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
224
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
198
    occlusion0Rect = occlusion0Rect > 50;
225
    occlusion0Rect = occlusion0Rect > 50;
199
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
226
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
200
    occlusion1Rect = occlusion1Rect > 50;
227
    occlusion1Rect = occlusion1Rect > 50;
-
 
228
 
-
 
229
    // erode occlusion masks
-
 
230
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3,3));
-
 
231
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
-
 
232
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
-
 
233
 
201
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
234
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
202
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
235
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
203
 
236
 
204
    // decode patterns
237
    // decode patterns
205
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(-1));
238
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(-1));
206
    cv::Mat code1Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(-1));
239
    cv::Mat code1Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(-1));
207
    cv::add(code0Rect, 1, code0Rect, occlusion0Rect, CV_32S);
240
    cv::add(code0Rect, 1, code0Rect, occlusion0Rect, CV_32S);
208
    cv::add(code1Rect, 1, code1Rect, occlusion1Rect, CV_32S);
241
    cv::add(code1Rect, 1, code1Rect, occlusion1Rect, CV_32S);
209
 
242
 
210
    // into gray code
243
    // into gray code
211
    for(int i=0; i<Nbits; i++){
244
    for(int i=0; i<Nbits; i++){
212
        cv::Mat bit0;
245
        cv::Mat bit0;
213
        cv::subtract(frames0Rect[i*2+2], frames0Rect[i*2+3], bit0);
246
        cv::subtract(frames0Rect[i*2+2], frames0Rect[i*2+3], bit0);
214
        bit0 = bit0 > 0;
247
        bit0 = bit0 > 0;
215
        bit0.convertTo(bit0, CV_32S, 1.0/255.0);
248
        bit0.convertTo(bit0, CV_32S, 1.0/255.0);
216
//      cvtools::writeMat(bit0, "bit0.mat", "bit0");
249
//      cvtools::writeMat(bit0, "bit0.mat", "bit0");
217
        cv::add(code0Rect, bit0*twopowi(Nbits-i-1), code0Rect, occlusion0Rect, CV_32S);
250
        cv::add(code0Rect, bit0*twopowi(Nbits-i-1), code0Rect, occlusion0Rect, CV_32S);
218
        cv::Mat bit1;
251
        cv::Mat bit1;
219
        cv::subtract(frames1Rect[i*2+2], frames1Rect[i*2+3], bit1);
252
        cv::subtract(frames1Rect[i*2+2], frames1Rect[i*2+3], bit1);
220
        bit1 = bit1 > 0;
253
        bit1 = bit1 > 0;
221
        bit1.convertTo(bit1, CV_32S, 1.0/255.0);
254
        bit1.convertTo(bit1, CV_32S, 1.0/255.0);
222
        cv::add(code1Rect, bit1*twopowi(Nbits-i-1), code1Rect, occlusion1Rect, CV_32S);
255
        cv::add(code1Rect, bit1*twopowi(Nbits-i-1), code1Rect, occlusion1Rect, CV_32S);
223
    }
256
    }
224
 
257
 
225
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
258
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
226
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
259
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
227
 
260
 
228
//    // convert to standard binary
261
//    // convert to standard binary
229
//    for(int r=0; r<frameRectRows; r++){
262
//    for(int r=0; r<frameRectRows; r++){
230
//        for(int c=0; c<frameRectCols; c++){
263
//        for(int c=0; c<frameRectCols; c++){
231
//            if(code0Rect.at<int>(r,c) != -1)
264
//            if(code0Rect.at<int>(r,c) != -1)
232
//                code0Rect.at<int>(r,c) = grayToBinary(code0Rect.at<int>(r,c));
265
//                code0Rect.at<int>(r,c) = grayToBinary(code0Rect.at<int>(r,c));
233
//            if(code1Rect.at<int>(r,c) != -1)
266
//            if(code1Rect.at<int>(r,c) != -1)
234
//                code1Rect.at<int>(r,c) = grayToBinary(code1Rect.at<int>(r,c));
267
//                code1Rect.at<int>(r,c) = grayToBinary(code1Rect.at<int>(r,c));
235
//        }
268
//        }
236
//    }
269
//    }
237
 
270
 
238
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
271
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
239
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
272
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
240
 
273
 
241
    // matching
274
    // matching
242
    std::vector<cv::Vec2f> q0Rect, q1Rect;
275
    std::vector<cv::Vec2f> q0Rect, q1Rect;
243
    for(int row=0; row<frameRectRows; row++){
276
    for(int row=0; row<frameRectRows; row++){
244
 
277
 
245
        std::vector<cv::Vec4f> edges0, edges1;
278
        std::vector<cv::Vec4i> edges0, edges1;
246
 
279
 
247
        // sorted, unique edges
280
        // sorted, unique edges
248
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
281
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
249
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
282
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
250
 
283
 
251
        // subpixel refinement
284
        // match edges
252
        // ...
285
        std::vector<cv::Vec4i> matchedEdges0, matchedEdges1;
253
 
-
 
254
        int i=0, j=0;
286
        int i=0, j=0;
255
        while(i<edges0.size() && j<edges1.size()){
287
        while(i<edges0.size() && j<edges1.size()){
256
 
288
 
257
            if(edges0[i][3] == edges1[j][3]){
289
            if(edges0[i][3] == edges1[j][3]){
258
                q0Rect.push_back(cv::Vec2f(edges0[i][0], row));
290
                matchedEdges0.push_back(edges0[i]);
259
                q1Rect.push_back(cv::Vec2f(edges1[j][0], row));
291
                matchedEdges1.push_back(edges1[j]);
260
                i += 1;
292
                i += 1;
261
                j += 1;
293
                j += 1;
262
            } else if(edges0[i][3] < edges1[j][3]){
294
            } else if(edges0[i][3] < edges1[j][3]){
263
                i += 1;
295
                i += 1;
264
            } else if(edges0[i][3] > edges1[j][3]){
296
            } else if(edges0[i][3] > edges1[j][3]){
265
                j += 1;
297
                j += 1;
266
            }
298
            }
267
        }
299
        }
268
 
300
 
-
 
301
        // crude subpixel refinement
-
 
302
        // finds the intersection of linear interpolants in the positive/negative pattern
-
 
303
        for(int i=0; i<matchedEdges0.size(); i++){
-
 
304
 
-
 
305
            int level = Nbits - leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
-
 
306
 
-
 
307
            // refine for camera 0
-
 
308
            float c0 = matchedEdges0[i][0];
-
 
309
            float c1 = c0+1;
-
 
310
 
-
 
311
            float pos0 = frames0Rect[2*level+2].at<char>(row, c0);
-
 
312
            float pos1 = frames0Rect[2*level+2].at<char>(row, c1);
-
 
313
            float neg0 = frames0Rect[2*level+3].at<char>(row, c0);
-
 
314
            float neg1 = frames0Rect[2*level+3].at<char>(row, c1);
-
 
315
 
-
 
316
            float col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
-
 
317
            q0Rect.push_back(cv::Point2f(col, row));
-
 
318
 
-
 
319
            // refine for camera 1
-
 
320
            c0 = matchedEdges1[i][0];
-
 
321
            c1 = c0+1;
-
 
322
 
-
 
323
            pos0 = frames1Rect[2*level+2].at<char>(row, c0);
-
 
324
            pos1 = frames1Rect[2*level+2].at<char>(row, c1);
-
 
325
            neg0 = frames1Rect[2*level+3].at<char>(row, c0);
-
 
326
            neg1 = frames1Rect[2*level+3].at<char>(row, c1);
-
 
327
 
-
 
328
            col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
-
 
329
            q1Rect.push_back(cv::Point2f(col, row));
-
 
330
 
-
 
331
        }
269
 
332
 
270
    }
333
    }
271
 
334
 
272
    // retrieve color information
335
    // retrieve color information
273
    int nMatches = q0Rect.size();
336
    int nMatches = q0Rect.size();
274
    color.resize(nMatches);
337
    color.resize(nMatches);
275
    for(int i=0; i<nMatches; i++){
338
    for(int i=0; i<nMatches; i++){
276
 
339
 
277
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
340
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
278
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
341
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
279
 
342
 
280
        color[i] = 0.5*c0 + 0.5*c1;
343
        color[i] = 0.5*c0 + 0.5*c1;
281
    }
344
    }
282
 
345
 
283
    // triangulate points
346
    // triangulate points
284
    cv::Mat QMatHomogenous, QMat;
347
    cv::Mat QMatHomogenous, QMat;
285
//    cv::Mat C0 = P0.clone();
348
//    cv::Mat C0 = P0.clone();
286
//    cv::Mat C1 = P1.clone();
349
//    cv::Mat C1 = P1.clone();
287
//    C0.colRange(0, 3) = C0.colRange(0, 3)*R0;
350
//    C0.colRange(0, 3) = C0.colRange(0, 3)*R0;
288
//    C1.colRange(0, 3) = C1.colRange(0, 3)*R1.t();
351
//    C1.colRange(0, 3) = C1.colRange(0, 3)*R1.t();
289
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
352
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
290
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
353
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
291
 
354
 
292
    // undo rectification
355
    // undo rectification
293
    cv::Mat R0Inv;
356
    cv::Mat R0Inv;
294
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
357
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
295
    QMat = R0Inv*QMat;
358
    QMat = R0Inv*QMat;
296
 
359
 
297
    cvtools::matToPoints3f(QMat, Q);
360
    cvtools::matToPoints3f(QMat, Q);
298
 
361
 
299
}
362
}
300
 
363