Subversion Repositories seema-scanner

Rev

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

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