Subversion Repositories seema-scanner

Rev

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

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