Subversion Repositories seema-scanner

Rev

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

Rev 91 Rev 95
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 be 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
cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
-
 
170
    assert(!img.empty());
-
 
171
    assert(img.channels() == 3);
-
 
172
 
-
 
173
    int x = (int)pt.x;
-
 
174
    int y = (int)pt.y;
-
 
175
 
-
 
176
    int x0 = cv::borderInterpolate(x,   img.cols, cv::BORDER_REFLECT_101);
-
 
177
    int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
-
 
178
    int y0 = cv::borderInterpolate(y,   img.rows, cv::BORDER_REFLECT_101);
-
 
179
    int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);
-
 
180
 
-
 
181
    float a = pt.x - (float)x;
-
 
182
    float c = pt.y - (float)y;
-
 
183
 
-
 
184
    uchar b = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[0] * a) * (1.f - c)
-
 
185
                           + (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
-
 
186
    uchar g = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[1] * a) * (1.f - c)
-
 
187
                           + (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
-
 
188
    uchar r = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[2] * a) * (1.f - c)
-
 
189
                           + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
-
 
190
 
-
 
191
    return cv::Vec3b(b, g, r);
-
 
192
}
-
 
193
 
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){
194
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
 
195
 
171
    assert(frames0.size() == N);
196
    assert(frames0.size() == N);
172
    assert(frames1.size() == N);
197
    assert(frames1.size() == N);
173
 
198
 
174
//    for(int i=0; i<1920; i++){
199
//    for(int i=0; i<1920; i++){
175
//        std::cout << i << " " << binaryToGray(i) << " " << grayToBinary(binaryToGray(i)) << std::endl;
200
//        std::cout << i << " " << binaryToGray(i) << " " << grayToBinary(binaryToGray(i)) << std::endl;
176
//    }
201
//    }
177
 
202
 
178
    int frameRows = frames0[0].rows;
203
    int frameRows = frames0[0].rows;
179
    int frameCols = frames0[0].cols;
204
    int frameCols = frames0[0].cols;
180
 
205
 
181
    // rectifying homographies (rotation+projections)
206
    // rectifying homographies (rotation+projections)
182
    cv::Size frameSize(frameCols, frameRows);
207
    cv::Size frameSize(frameCols, frameRows);
183
    cv::Mat R, T;
208
    cv::Mat R, T;
184
    // stereoRectify segfaults unless R is double precision
209
    // stereoRectify segfaults unless R is double precision
185
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
210
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
186
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
211
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
187
    cv::Mat R0, R1, P0, P1, QRect;
212
    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);
213
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
189
 
214
 
190
//    std::cout << "R0" << std::endl << R0 << std::endl;
215
//    std::cout << "R0" << std::endl << R0 << std::endl;
191
//    std::cout << "P0" << std::endl << P0 << std::endl;
216
//    std::cout << "P0" << std::endl << P0 << std::endl;
192
//    std::cout << "R1" << std::endl << R1 << std::endl;
217
//    std::cout << "R1" << std::endl << R1 << std::endl;
193
//    std::cout << "P1" << std::endl << P1 << std::endl;
218
//    std::cout << "P1" << std::endl << P1 << std::endl;
194
 
219
 
195
    // interpolation maps
220
    // interpolation maps
196
    cv::Mat map0X, map0Y, map1X, map1Y;
221
    cv::Mat map0X, map0Y, map1X, map1Y;
197
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
222
    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);
223
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
199
 
224
 
200
    // gray-scale and remap
225
    // gray-scale and remap
201
    std::vector<cv::Mat> frames0Rect(N);
226
    std::vector<cv::Mat> frames0Rect(N);
202
    std::vector<cv::Mat> frames1Rect(N);
227
    std::vector<cv::Mat> frames1Rect(N);
203
    for(int i=0; i<N; i++){
228
    for(int i=0; i<N; i++){
204
        cv::Mat temp;
229
        cv::Mat temp;
205
        cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
230
        cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
206
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_CUBIC);
231
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_CUBIC);
207
        cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
232
        cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
208
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_CUBIC);
233
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_CUBIC);
209
    }
234
    }
210
 
235
 
211
//    cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
236
//    cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
212
//    cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
237
//    cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
213
 
238
 
214
//    cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
239
//    cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
215
//    cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
240
//    cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
216
 
241
 
217
//    cv::imwrite("frames0[0].png", frames0[0]);
242
//    cv::imwrite("frames0[0].png", frames0[0]);
218
//    cv::imwrite("frames0Rect[0].png", frames0Rect[0]);
243
//    cv::imwrite("frames0Rect[0].png", frames0Rect[0]);
219
 
244
 
220
//    cv::imwrite("frames1[0].png", frames1[0]);
245
//    cv::imwrite("frames1[0].png", frames1[0]);
221
//    cv::imwrite("frames1Rect[0].png", frames1Rect[0]);
246
//    cv::imwrite("frames1Rect[0].png", frames1Rect[0]);
222
 
247
 
223
 
248
 
224
    // color remaps
249
    // color remaps
225
    cv::Mat color0Rect, color1Rect;
250
    cv::Mat color0Rect, color1Rect;
226
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
251
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
227
    cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
252
    cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
228
 
253
 
229
    int frameRectRows = frames0Rect[0].rows;
254
    int frameRectRows = frames0Rect[0].rows;
230
    int frameRectCols = frames0Rect[0].cols;
255
    int frameRectCols = frames0Rect[0].cols;
231
 
256
 
232
    // occlusion masks
257
    // occlusion masks
233
    cv::Mat occlusion0Rect, occlusion1Rect;
258
    cv::Mat occlusion0Rect, occlusion1Rect;
234
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
259
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
235
    occlusion0Rect = occlusion0Rect > 25;
260
    occlusion0Rect = occlusion0Rect > 25;
236
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
261
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
237
    occlusion1Rect = occlusion1Rect > 25;
262
    occlusion1Rect = occlusion1Rect > 25;
238
 
263
 
239
    // erode occlusion masks
264
    // erode occlusion masks
240
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3,3));
265
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3,3));
241
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
266
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
242
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
267
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
243
 
268
 
244
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
269
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
245
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
270
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
246
 
271
 
247
    // decode patterns
272
    // decode patterns
248
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
273
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
249
    cv::Mat code1Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
274
    cv::Mat code1Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
250
 
275
 
251
    // into gray code
276
    // into gray code
252
    for(int i=0; i<Nbits; i++){
277
    for(int i=0; i<Nbits; i++){
253
        cv::Mat bit0;
278
        cv::Mat bit0;
254
        cv::subtract(frames0Rect[i*2+2], frames0Rect[i*2+3], bit0);
279
        cv::subtract(frames0Rect[i*2+2], frames0Rect[i*2+3], bit0);
255
        bit0 = bit0 > 0;
280
        bit0 = bit0 > 0;
256
        bit0.convertTo(bit0, CV_32S, 1.0/255.0);
281
        bit0.convertTo(bit0, CV_32S, 1.0/255.0);
257
//      cvtools::writeMat(bit0, "bit0.mat", "bit0");
282
//      cvtools::writeMat(bit0, "bit0.mat", "bit0");
258
        cv::add(code0Rect, bit0*twopowi(Nbits-i-1), code0Rect, cv::Mat(), CV_32S);
283
        cv::add(code0Rect, bit0*twopowi(Nbits-i-1), code0Rect, cv::Mat(), CV_32S);
259
        cv::Mat bit1;
284
        cv::Mat bit1;
260
        cv::subtract(frames1Rect[i*2+2], frames1Rect[i*2+3], bit1);
285
        cv::subtract(frames1Rect[i*2+2], frames1Rect[i*2+3], bit1);
261
        bit1 = bit1 > 0;
286
        bit1 = bit1 > 0;
262
        bit1.convertTo(bit1, CV_32S, 1.0/255.0);
287
        bit1.convertTo(bit1, CV_32S, 1.0/255.0);
263
        cv::add(code1Rect, bit1*twopowi(Nbits-i-1), code1Rect, cv::Mat(), CV_32S);
288
        cv::add(code1Rect, bit1*twopowi(Nbits-i-1), code1Rect, cv::Mat(), CV_32S);
264
    }
289
    }
265
 
290
 
266
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
291
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
267
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
292
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
268
 
293
 
269
 
294
 
270
//    // convert to standard binary
295
//    // convert to standard binary
271
//    cv::Mat code0Binary(code0Rect.rows, code0Rect.cols, CV_32F);
296
//    cv::Mat code0Binary(code0Rect.rows, code0Rect.cols, CV_32F);
272
//    cv::Mat code1Binary(code1Rect.rows, code1Rect.cols, CV_32F);
297
//    cv::Mat code1Binary(code1Rect.rows, code1Rect.cols, CV_32F);
273
//    for(int r=0; r<frameRectRows; r++){
298
//    for(int r=0; r<frameRectRows; r++){
274
//        for(int c=0; c<frameRectCols; c++){
299
//        for(int c=0; c<frameRectCols; c++){
275
//            if(code0Rect.at<int>(r,c) != -1)
300
//            if(code0Rect.at<int>(r,c) != -1)
276
//                code0Binary.at<float>(r,c) = grayToBinary(code0Rect.at<int>(r,c));
301
//                code0Binary.at<float>(r,c) = grayToBinary(code0Rect.at<int>(r,c));
277
//            if(code1Rect.at<int>(r,c) != -1)
302
//            if(code1Rect.at<int>(r,c) != -1)
278
//                code1Binary.at<float>(r,c) = grayToBinary(code1Rect.at<int>(r,c));
303
//                code1Binary.at<float>(r,c) = grayToBinary(code1Rect.at<int>(r,c));
279
//        }
304
//        }
280
//    }
305
//    }
281
 
306
 
282
//cvtools::writeMat(code0Binary, "code0Binary.mat", "code0Binary");
307
//cvtools::writeMat(code0Binary, "code0Binary.mat", "code0Binary");
283
//cvtools::writeMat(code1Binary, "code1Binary.mat", "code1Binary");
308
//cvtools::writeMat(code1Binary, "code1Binary.mat", "code1Binary");
284
 
309
 
285
//    // threshold on vertical discontinuities (due to imperfect rectification)
310
//    // threshold on vertical discontinuities (due to imperfect rectification)
286
//    cv::Mat edges0;
311
//    cv::Mat edges0;
287
//    cv::Sobel(code0Binary, edges0, -1, 0, 1, 5);
312
//    cv::Sobel(code0Binary, edges0, -1, 0, 1, 5);
288
//    occlusion0Rect = occlusion0Rect & (abs(edges0) < 50);
313
//    occlusion0Rect = occlusion0Rect & (abs(edges0) < 50);
289
 
314
 
290
//    cv::Mat edges1;
315
//    cv::Mat edges1;
291
//    cv::Sobel(code1Binary, edges1, -1, 0, 1, 5);
316
//    cv::Sobel(code1Binary, edges1, -1, 0, 1, 5);
292
//    occlusion1Rect = occlusion1Rect & (abs(edges1) < 50);
317
//    occlusion1Rect = occlusion1Rect & (abs(edges1) < 50);
293
 
318
 
294
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
319
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
295
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
320
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
296
 
321
 
297
    // set occluded pixels to -1
322
    // set occluded pixels to -1
298
    for(int r=0; r<frameRectRows; r++){
323
    for(int r=0; r<frameRectRows; r++){
299
        for(int c=0; c<frameRectCols; c++){
324
        for(int c=0; c<frameRectCols; c++){
300
            if(occlusion0Rect.at<char>(r,c) == 0)
325
            if(occlusion0Rect.at<char>(r,c) == 0)
301
                code0Rect.at<float>(r,c) = -1;
326
                code0Rect.at<float>(r,c) = -1;
302
            if(occlusion1Rect.at<char>(r,c) == 0)
327
            if(occlusion1Rect.at<char>(r,c) == 0)
303
                code1Rect.at<float>(r,c) = -1;
328
                code1Rect.at<float>(r,c) = -1;
304
        }
329
        }
305
    }
330
    }
306
 
331
 
307
    // matching
332
    // matching
308
    std::vector<cv::Vec2f> q0Rect, q1Rect;
333
    std::vector<cv::Vec2f> q0Rect, q1Rect;
309
    for(int row=0; row<frameRectRows; row++){
334
    for(int row=0; row<frameRectRows; row++){
310
 
335
 
-
 
336
        // edge data structure containing [floor(column), labelLeft, labelRight, orderingRelation]
311
        std::vector<cv::Vec4i> edges0, edges1;
337
        std::vector<cv::Vec4i> edges0, edges1;
312
 
338
 
313
        // sorted, unique edges
339
        // sorted, unique edges
314
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
340
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
315
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
341
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
316
 
342
 
317
        // match edges
343
        // match edges
318
        std::vector<cv::Vec4i> matchedEdges0, matchedEdges1;
344
        std::vector<cv::Vec4i> matchedEdges0, matchedEdges1;
319
        int i=0, j=0;
345
        int i=0, j=0;
320
        while(i<edges0.size() && j<edges1.size()){
346
        while(i<edges0.size() && j<edges1.size()){
321
 
347
 
322
            if(edges0[i][3] == edges1[j][3]){
348
            if(edges0[i][3] == edges1[j][3]){
323
                matchedEdges0.push_back(edges0[i]);
349
                matchedEdges0.push_back(edges0[i]);
324
                matchedEdges1.push_back(edges1[j]);
350
                matchedEdges1.push_back(edges1[j]);
325
                i += 1;
351
                i += 1;
326
                j += 1;
352
                j += 1;
327
            } else if(edges0[i][3] < edges1[j][3]){
353
            } else if(edges0[i][3] < edges1[j][3]){
328
                i += 1;
354
                i += 1;
329
            } else if(edges0[i][3] > edges1[j][3]){
355
            } else if(edges0[i][3] > edges1[j][3]){
330
                j += 1;
356
                j += 1;
331
            }
357
            }
332
        }
358
        }
333
 
359
 
334
        // crude subpixel refinement
360
        // crude subpixel refinement
335
        // finds the intersection of linear interpolants in the positive/negative pattern
361
        // finds the intersection of linear interpolants in the positive/negative pattern
336
        for(int i=0; i<matchedEdges0.size(); i++){
362
        for(int i=0; i<matchedEdges0.size(); i++){
337
 
363
 
338
            int level = Nbits - leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
364
            int level = Nbits - leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
339
 
365
 
340
            // refine for camera 0
366
            // refine for camera 0
341
            float c0 = matchedEdges0[i][0];
367
            float c0 = matchedEdges0[i][0];
342
            float c1 = c0+1;
368
            float c1 = c0+1;
343
 
369
 
344
            float pos0 = frames0Rect[2*level+2].at<char>(row, c0);
370
            float pos0 = frames0Rect[2*level+2].at<char>(row, c0);
345
            float pos1 = frames0Rect[2*level+2].at<char>(row, c1);
371
            float pos1 = frames0Rect[2*level+2].at<char>(row, c1);
346
            float neg0 = frames0Rect[2*level+3].at<char>(row, c0);
372
            float neg0 = frames0Rect[2*level+3].at<char>(row, c0);
347
            float neg1 = frames0Rect[2*level+3].at<char>(row, c1);
373
            float neg1 = frames0Rect[2*level+3].at<char>(row, c1);
348
 
374
 
349
            float col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
375
            float col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
350
            q0Rect.push_back(cv::Point2f(col, row));
376
            q0Rect.push_back(cv::Point2f(col, row));
351
 
377
 
352
            // refine for camera 1
378
            // refine for camera 1
353
            c0 = matchedEdges1[i][0];
379
            c0 = matchedEdges1[i][0];
354
            c1 = c0+1;
380
            c1 = c0+1;
355
 
381
 
356
            pos0 = frames1Rect[2*level+2].at<char>(row, c0);
382
            pos0 = frames1Rect[2*level+2].at<char>(row, c0);
357
            pos1 = frames1Rect[2*level+2].at<char>(row, c1);
383
            pos1 = frames1Rect[2*level+2].at<char>(row, c1);
358
            neg0 = frames1Rect[2*level+3].at<char>(row, c0);
384
            neg0 = frames1Rect[2*level+3].at<char>(row, c0);
359
            neg1 = frames1Rect[2*level+3].at<char>(row, c1);
385
            neg1 = frames1Rect[2*level+3].at<char>(row, c1);
360
 
386
 
361
            col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
387
            col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
362
            q1Rect.push_back(cv::Point2f(col, row));
388
            q1Rect.push_back(cv::Point2f(col, row));
363
 
389
 
364
        }
390
        }
365
 
391
 
366
    }
392
    }
367
 
393
 
368
    int nMatches = q0Rect.size();
394
    int nMatches = q0Rect.size();
369
 
395
 
370
    if(nMatches < 1){
396
    if(nMatches < 1){
371
        Q.resize(0);
397
        Q.resize(0);
372
        color.resize(0);
398
        color.resize(0);
373
 
399
 
374
        return;
400
        return;
375
    }
401
    }
376
 
402
 
377
    // retrieve color information
403
    // retrieve color information (at integer coordinates)
378
    color.resize(nMatches);
404
    color.resize(nMatches);
379
    for(int i=0; i<nMatches; i++){
405
    for(int i=0; i<nMatches; i++){
380
 
406
 
381
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
407
        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]);
408
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
-
 
409
//        cv::Vec3b c0 = getColorSubpix(color0Rect, q0Rect[i]);
-
 
410
//        cv::Vec3b c1 = getColorSubpix(color1Rect, q0Rect[i]);
383
 
411
 
384
        color[i] = 0.5*c0 + 0.5*c1;
412
        color[i] = 0.5*c0 + 0.5*c1;
385
    }
413
    }
386
 
414
 
387
    // triangulate points
415
    // triangulate points
388
    cv::Mat QMatHomogenous, QMat;
416
    cv::Mat QMatHomogenous, QMat;
389
//    cv::Mat C0 = P0.clone();
417
//    cv::Mat C0 = P0.clone();
390
//    cv::Mat C1 = P1.clone();
418
//    cv::Mat C1 = P1.clone();
391
//    C0.colRange(0, 3) = C0.colRange(0, 3)*R0;
419
//    C0.colRange(0, 3) = C0.colRange(0, 3)*R0;
392
//    C1.colRange(0, 3) = C1.colRange(0, 3)*R1.t();
420
//    C1.colRange(0, 3) = C1.colRange(0, 3)*R1.t();
393
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
421
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
394
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
422
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
395
 
423
 
396
    // undo rectification
424
    // undo rectifying rotation
397
    cv::Mat R0Inv;
425
    cv::Mat R0Inv;
398
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
426
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
399
    QMat = R0Inv*QMat;
427
    QMat = R0Inv*QMat;
400
 
428
 
401
    cvtools::matToPoints3f(QMat, Q);
429
    cvtools::matToPoints3f(QMat, Q);
402
 
430
 
403
}
431
}
404
 
432