Subversion Repositories seema-scanner

Rev

Rev 122 | Rev 124 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 122 Rev 123
Line 1... Line 1...
1
#include "AlgorithmGrayCode.h"
1
#include "AlgorithmLineShift.h"
2
#include <cmath>
2
#include <cmath>
3
#include "cvtools.h"
3
#include "cvtools.h"
4
 
4
 
-
 
5
#include <opencv2/imgproc.hpp>
-
 
6
 
5
#ifndef log2f
7
#ifndef log2f
6
#define log2f(x) (log(x)/log(2.0))
8
#define log2f(x) (log(x)/log(2.0))
7
#endif
9
#endif
8
 
10
 
9
//using namespace std;
11
static unsigned int nLineShifts = 8; // number of columns over which each line is shifted
10
 
12
 
11
/*
13
/*
12
 * The purpose of this function is to convert an unsigned
14
 * The purpose of this function is to convert an unsigned
13
 * binary number to reflected binary Gray code.
15
 * binary number to reflected binary Gray code.
14
 *
16
 *
Line 61... Line 63...
61
      val++;
63
      val++;
62
 
64
 
63
  return val;
65
  return val;
64
}
66
}
65
 
67
 
66
//static int get_bit(int decimal, int N){
-
 
67
 
-
 
68
//    // Shifting the 1 for N-1 bits
-
 
69
//    int constant = 1 << (N-1);
-
 
70
 
-
 
71
//    // If the bit is set, return 1
-
 
72
//    if( decimal & constant )
-
 
73
//        return 1;
-
 
74
//    else
-
 
75
//        return 0;
-
 
76
//}
-
 
77
 
-
 
78
static inline unsigned int powi(int num, unsigned int exponent){
68
static inline unsigned int powi(int num, unsigned int exponent){
79
 
69
 
80
    if(exponent == 0)
70
    if(exponent == 0)
81
        return 1;
71
        return 1;
82
 
72
 
Line 91... Line 81...
91
 
81
 
92
    return 1 << exponent;
82
    return 1 << exponent;
93
}
83
}
94
 
84
 
95
// Algorithm
85
// Algorithm
96
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
86
AlgorithmLineShift::AlgorithmLineShift(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
-
 
87
 
-
 
88
    nGrayBits = ceilf(log2f((float)screenCols)) - floorf(log2f((float)nLineShifts));
97
 
89
 
98
    Nbits = ceilf(log2f((float)screenCols));
-
 
99
    N = 2 + Nbits*2;
90
    N = 2 + 2*nGrayBits + nLineShifts;
100
 
91
 
101
    // all on pattern
92
    // all on pattern
102
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
93
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
103
    patterns.push_back(allOn);
94
    patterns.push_back(allOn);
104
 
95
 
105
    // all off pattern
96
    // all off pattern
106
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
97
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
107
    patterns.push_back(allOff);
98
    patterns.push_back(allOff);
108
 
99
 
109
 
100
 
110
    // horizontally encoding patterns
101
    // Gray code patterns
111
    for(unsigned int p=0; p<Nbits; p++){
102
    for(unsigned int p=0; p<nGrayBits; p++){
112
        cv::Mat pattern(1, screenCols, CV_8UC3);
103
        cv::Mat pattern(1, screenCols, CV_8UC3);
113
        cv::Mat patternInv(1, screenCols, CV_8UC3);
104
        cv::Mat patternInv(1, screenCols, CV_8UC3);
114
 
105
 
115
        for(unsigned int j=0; j<screenCols; j++){
106
        for(unsigned int j=0; j<screenCols; j++){
116
 
107
 
117
            unsigned int jGray = binaryToGray(j);
108
            unsigned int jGray = binaryToGray(j);
118
            // Amplitude of channels
109
            // Amplitude of channels
119
            int bit = (int)getBit(jGray, Nbits-p);
110
            int bit = (int)getBit(jGray, nGrayBits-p);
120
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
111
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
121
            int invBit = bit^1;
112
            int invBit = bit^1;
122
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
113
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
123
        }
114
        }
124
        patterns.push_back(pattern);
115
        patterns.push_back(pattern);
125
        patterns.push_back(patternInv);
116
        patterns.push_back(patternInv);
126
    }
117
    }
127
 
118
 
-
 
119
    // line shifts
-
 
120
    for(unsigned int p=0; p<nLineShifts; p++){
-
 
121
        cv::Mat pattern(1, screenCols, CV_8UC3, cv::Vec3b(0, 0, 0));
128
 
122
 
129
}
-
 
-
 
123
        for(unsigned int j=p; j<screenCols; j+= nLineShifts)
-
 
124
            pattern.at<cv::Vec3b>(0, j) = cv::Vec3b(255, 255, 255);
130
 
125
 
131
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
-
 
132
    return patterns[depth];
126
        patterns.push_back(pattern);
133
}
127
    }
134
 
128
 
135
 
-
 
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]);}
-
 
138
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4i>& edges){
-
 
139
 
-
 
140
    int nCols = scanLine.cols;
-
 
141
    const int *data = scanLine.ptr<const int>(0);
-
 
142
 
-
 
143
    int labelLeft;
-
 
144
    int labelRight = data[0];
-
 
145
 
-
 
146
    // collect edges
-
 
147
    for(int col=1; col<nCols; col++){
-
 
148
 
-
 
149
        labelLeft = labelRight;
-
 
150
        labelRight = data[col];
-
 
151
 
-
 
152
        // labels need to be non-background, and differ in exactly one bit
-
 
153
        if(labelLeft != -1 && labelRight != -1 && (grayToBinary(labelRight) == grayToBinary(labelLeft)+1)){
-
 
154
            int orderingRelation = (labelLeft << Nbits) + labelRight;
-
 
155
            // store left label column
-
 
156
            edges.push_back(cv::Vec4i(col-1, labelLeft, labelRight, orderingRelation));
-
 
157
        }
-
 
158
    }
129
}
159
 
130
 
160
    // sort
-
 
161
    std::sort(edges.begin(), edges.end(), sortingLarger);
131
cv::Mat AlgorithmLineShift::getEncodingPattern(unsigned int depth){
162
 
-
 
163
    // remove duplicates
132
    return patterns[depth];
164
    std::vector<cv::Vec4i>::iterator it;
-
 
165
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
-
 
166
    edges.resize(std::distance(edges.begin(),it));
-
 
167
}
133
}
168
 
134
 
169
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
135
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
170
    assert(!img.empty());
136
    assert(!img.empty());
171
    assert(img.channels() == 3);
137
    assert(img.channels() == 3);
Line 189... Line 155...
189
                           + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
155
                           + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
190
 
156
 
191
    return cv::Vec3b(b, g, r);
157
    return cv::Vec3b(b, g, r);
192
}
158
}
193
 
159
 
-
 
160
void getlineCenters(const cv::Mat& linesScanLine, const cv::Mat& codeScanLine, std::vector<cv::Vec4i>& lineCenters){
-
 
161
 
-
 
162
    int nCols = linesScanLine.cols;
-
 
163
 
-
 
164
    // fourth order derivative filtering
-
 
165
    cv::Mat der(1, nCols, CV_8U);
-
 
166
    for(int i=2; i<nCols-2; i++)
-
 
167
        der.at<unsigned char>(0, i) = linesScanLine.at<unsigned char>(0, i-2)+linesScanLine.at<unsigned char>(0, i-1)-
-
 
168
                                      linesScanLine.at<unsigned char>(0, i+1)-linesScanLine.at<unsigned char>(0, i+2);
-
 
169
 
-
 
170
    for(int i=0; i<nCols; i++){
-
 
171
 
-
 
172
        if(der.at<unsigned char>(0, i) > 0 && der.at<unsigned char>(0, i+1) < 0){
-
 
173
 
-
 
174
            lineCenters.push_back(i, codeScanLine.at<unsigned char>(0, i));
-
 
175
            // TODO: subpixel interpolation, non-max suppression
-
 
176
        }
-
 
177
 
-
 
178
    }
-
 
179
 
-
 
180
}
-
 
181
 
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){
182
void AlgorithmLineShift::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){
195
 
183
 
196
    assert(frames0.size() == N);
184
    assert(frames0.size() == N);
197
    assert(frames1.size() == N);
185
    assert(frames1.size() == N);
198
 
186
 
199
//    for(int i=0; i<1920; i++){
-
 
200
//        std::cout << i << " " << binaryToGray(i) << " " << grayToBinary(binaryToGray(i)) << std::endl;
-
 
201
//    }
-
 
202
 
-
 
203
    int frameRows = frames0[0].rows;
187
    int frameRows = frames0[0].rows;
204
    int frameCols = frames0[0].cols;
188
    int frameCols = frames0[0].cols;
205
 
189
 
206
    // rectifying homographies (rotation+projections)
190
    // rectifying homographies (rotation+projections)
207
    cv::Size frameSize(frameCols, frameRows);
191
    cv::Size frameSize(frameCols, frameRows);
Line 210... Line 194...
210
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
194
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
211
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
195
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
212
    cv::Mat R0, R1, P0, P1, QRect;
196
    cv::Mat R0, R1, P0, P1, QRect;
213
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
197
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
214
 
198
 
215
//    std::cout << "R0" << std::endl << R0 << std::endl;
-
 
216
//    std::cout << "P0" << std::endl << P0 << std::endl;
-
 
217
//    std::cout << "R1" << std::endl << R1 << std::endl;
-
 
218
//    std::cout << "P1" << std::endl << P1 << std::endl;
-
 
219
 
-
 
220
    // interpolation maps
199
    // interpolation maps
221
    cv::Mat map0X, map0Y, map1X, map1Y;
200
    cv::Mat map0X, map0Y, map1X, map1Y;
222
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
201
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
223
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
202
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
224
 
203
 
Line 231... Line 210...
231
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
210
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
232
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
211
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
233
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
212
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
234
    }
213
    }
235
 
214
 
236
 
-
 
237
//    cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
215
    //cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
238
//    cvtools::writeMat(frames0[0], "frames0_0.mat", "frames0_0");
216
    //cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
239
 
-
 
240
//    cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
217
    //cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
241
//    cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
218
    //cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
242
 
-
 
243
//    cv::imwrite("frames0[0].png", frames0[0]);
-
 
244
//    cv::imwrite("frames0Rect[0].png", frames0Rect[0]);
-
 
245
 
-
 
246
//    cv::imwrite("frames1[0].png", frames1[0]);
-
 
247
//    cv::imwrite("frames1Rect[0].png", frames1Rect[0]);
-
 
248
 
219
 
249
    // color debayer and remap
220
    // color debayer and remap
250
    cv::Mat color0Rect, color1Rect;
221
    cv::Mat color0Rect, color1Rect;
251
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
-
 
252
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
222
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
253
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
223
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
254
 
224
 
255
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
-
 
256
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
225
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
257
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
226
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
258
 
227
 
259
    int frameRectRows = frames0Rect[0].rows;
228
    int frameRectRows = frames0Rect[0].rows;
260
    int frameRectCols = frames0Rect[0].cols;
229
    int frameRectCols = frames0Rect[0].cols;
261
 
230
 
262
//cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
-
 
263
//cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
-
 
264
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
-
 
265
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
-
 
266
 
-
 
267
    // occlusion masks
231
    // occlusion masks
268
    cv::Mat occlusion0Rect, occlusion1Rect;
232
    cv::Mat occlusion0Rect, occlusion1Rect;
269
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
233
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
270
    occlusion0Rect = (occlusion0Rect > 20) & (occlusion0Rect < 250);
234
    occlusion0Rect = (occlusion0Rect > 20) & (occlusion0Rect < 250);
271
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
235
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
272
    occlusion1Rect = (occlusion1Rect > 20) & (occlusion1Rect < 250);
236
    occlusion1Rect = (occlusion1Rect > 20) & (occlusion1Rect < 250);
273
 
237
 
-
 
238
    //cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
-
 
239
    //cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
-
 
240
 
274
    // erode occlusion masks
241
    // erode occlusion masks
275
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2,2));
242
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2,2));
276
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
243
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
277
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
244
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
278
 
245
 
279
//cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
-
 
280
//cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
-
 
281
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
-
 
282
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
-
 
283
 
-
 
284
//    // correct for projector inversion error
-
 
285
//    cv::Mat W;
-
 
286
//    cv::add(frames0Rect[0], frames0Rect[1], W, cv::noArray(), CV_32F);
-
 
287
//    for(int i=2; i<N; i+=2){
-
 
288
//        cv::Mat S, E;
-
 
289
//        cv::add(frames0Rect[i], frames0Rect[i+1], S, cv::noArray(), CV_32F);
-
 
290
//        cv::subtract(W, S, E, cv::noArray(), CV_32F);
-
 
291
//        E *= 0.5;
-
 
292
//        cv::add(frames0Rect[i], E, frames0Rect[i], cv::noArray(), CV_16UC1);
-
 
293
//        cv::add(frames0Rect[i+1], E, frames0Rect[i+1], cv::noArray(), CV_16UC1);
-
 
294
//    }
-
 
295
 
-
 
296
 
-
 
297
    // correct for texture modulation and ambient
246
    // correct for texture modulation and ambient
298
    cv::Mat A0 = frames0Rect[1];
247
    cv::Mat A0 = frames0Rect[1];
299
    cv::Mat M0 = frames0Rect[0]-frames0Rect[1];
248
    cv::Mat M0 = frames0Rect[0]-frames0Rect[1];
300
//cvtools::writeMat(A0, "A0.mat", "A0");
-
 
301
//cvtools::writeMat(M0, "M0.mat", "M0");
-
 
302
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
-
 
303
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
-
 
-
 
249
 
304
    cv::divide(256.0, M0, M0, CV_32F);
250
    cv::divide(256.0, M0, M0, CV_32F);
305
    cv::Mat A1 = frames1Rect[1];
251
    cv::Mat A1 = frames1Rect[1];
306
    cv::Mat M1 = frames1Rect[0]-frames1Rect[1];
252
    cv::Mat M1 = frames1Rect[0]-frames1Rect[1];
307
    cv::divide(256.0, M1, M1, CV_32F);
253
    cv::divide(256.0, M1, M1, CV_32F);
308
 
254
 
Line 312... Line 258...
312
    }
258
    }
313
 
259
 
314
//cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
260
    //cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
315
//cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
261
    //cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
316
 
262
 
-
 
263
    // divide into Gray coding frames and line shift frames
-
 
264
    std::vector<cv::Mat> frames0GrayCode(frames0Rect.begin()+2, frames0Rect.begin()+2+nGrayBits);
317
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
265
    std::vector<cv::Mat> frames0LineShift(frames0Rect.begin()+2+nGrayBits, frames0Rect.end());
-
 
266
    std::vector<cv::Mat> frames1GrayCode(frames1Rect.begin()+2, frames1Rect.begin()+2+nGrayBits);
318
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
267
    std::vector<cv::Mat> frames1LineShift(frames1Rect.begin()+2+nGrayBits, frames1Rect.end());
319
 
268
 
320
    // decode patterns
269
    // decode patterns
321
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
270
    cv::Mat code0Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
322
    cv::Mat code1Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
271
    cv::Mat code1Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
323
 
272
 
324
    // into gray code
273
    // into gray code
325
    for(int i=0; i<Nbits; i++){
274
    for(int i=0; i<nGrayBits; i++){
326
        cv::Mat temp, bit0, bit1;
275
        cv::Mat temp, bit0, bit1;
327
 
276
 
328
        cv::compare(frames0Rect[i*2+2], frames0Rect[i*2+3], temp, cv::CMP_GT);
277
        cv::compare(frames0GrayCode[i*2], frames0GrayCode[i*2+1], temp, cv::CMP_GT);
329
        temp.convertTo(bit0, CV_32S, 1.0/255.0);
278
        temp.convertTo(bit0, CV_32S, 1.0/255.0);
330
        cv::add(code0Rect, bit0*twopowi(Nbits-i-1), code0Rect, cv::noArray(), CV_32S);
279
        cv::add(code0Gray, bit0*twopowi(Nbits-i-1), code0Gray, cv::noArray(), CV_32S);
331
 
280
 
332
        cv::compare(frames1Rect[i*2+2], frames1Rect[i*2+3], temp, cv::CMP_GT);
281
        cv::compare(frames1GrayCode[i*2], frames1GrayCode[i*2+1], temp, cv::CMP_GT);
333
        temp.convertTo(bit1, CV_32S, 1.0/255.0);
282
        temp.convertTo(bit1, CV_32S, 1.0/255.0);
334
        cv::add(code1Rect, bit1*twopowi(Nbits-i-1), code1Rect, cv::noArray(), CV_32S);
283
        cv::add(code1Gray, bit1*twopowi(Nbits-i-1), code1Gray, cv::noArray(), CV_32S);
335
    }
284
    }
336
 
285
 
337
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
-
 
338
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
-
 
339
 
-
 
340
 
-
 
341
//    // convert to standard binary
-
 
342
//    cv::Mat code0Binary(code0Rect.rows, code0Rect.cols, CV_32F);
-
 
343
//    cv::Mat code1Binary(code1Rect.rows, code1Rect.cols, CV_32F);
-
 
344
//    for(int r=0; r<frameRectRows; r++){
-
 
345
//        for(int c=0; c<frameRectCols; c++){
-
 
346
//            if(code0Rect.at<int>(r,c) != -1)
-
 
347
//                code0Binary.at<float>(r,c) = grayToBinary(code0Rect.at<int>(r,c));
-
 
348
//            if(code1Rect.at<int>(r,c) != -1)
-
 
349
//                code1Binary.at<float>(r,c) = grayToBinary(code1Rect.at<int>(r,c));
-
 
350
//        }
-
 
351
//    }
-
 
352
 
-
 
353
//cvtools::writeMat(code0Binary, "code0Binary.mat", "code0Binary");
-
 
354
//cvtools::writeMat(code1Binary, "code1Binary.mat", "code1Binary");
-
 
355
 
-
 
356
//    // threshold on vertical discontinuities (due to imperfect rectification)
-
 
357
//    cv::Mat edges0;
-
 
358
//    cv::Sobel(code0Binary, edges0, -1, 0, 1, 5);
-
 
359
//    occlusion0Rect = occlusion0Rect & (abs(edges0) < 50);
-
 
360
 
-
 
361
//    cv::Mat edges1;
-
 
362
//    cv::Sobel(code1Binary, edges1, -1, 0, 1, 5);
-
 
363
//    occlusion1Rect = occlusion1Rect & (abs(edges1) < 50);
-
 
364
 
-
 
365
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
-
 
366
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
-
 
367
 
-
 
368
    // set occluded pixels to -1
286
    // set occluded pixels to -1
369
    for(int r=0; r<frameRectRows; r++){
287
    for(int r=0; r<frameRectRows; r++){
370
        for(int c=0; c<frameRectCols; c++){
288
        for(int c=0; c<frameRectCols; c++){
371
            if(occlusion0Rect.at<unsigned char>(r,c) == 0)
289
            if(occlusion0Rect.at<unsigned char>(r,c) == 0)
372
                code0Rect.at<int>(r,c) = -1;
290
                code0Gray.at<int>(r,c) = -1;
373
            if(occlusion1Rect.at<unsigned char>(r,c) == 0)
291
            if(occlusion1Rect.at<unsigned char>(r,c) == 0)
374
                code1Rect.at<int>(r,c) = -1;
292
                code1Gray.at<int>(r,c) = -1;
-
 
293
        }
-
 
294
    }
-
 
295
 
-
 
296
    // convert to standard binary
-
 
297
    cv::Mat code0Binary(code0Gray.rows, code0Gray.cols, CV_32F);
-
 
298
    cv::Mat code1Binary(code1Gray.rows, code1Gray.cols, CV_32F);
-
 
299
    for(int r=0; r<frameRectRows; r++){
-
 
300
        for(int c=0; c<frameRectCols; c++){
-
 
301
            if(code0Gray.at<int>(r,c) != -1)
-
 
302
                code0Binary.at<float>(r,c) = grayToBinary(code0Gray.at<int>(r,c));
-
 
303
            if(code1Gray.at<int>(r,c) != -1)
-
 
304
                code1Binary.at<float>(r,c) = grayToBinary(code1Gray.at<int>(r,c));
375
        }
305
        }
376
    }
306
    }
377
 
307
 
378
//    cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
308
    //cvtools::writeMat(code0Gray, "code0Gray.mat", "code0Gray");
379
//    cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
309
    //cvtools::writeMat(code1Gray, "code1Gray.mat", "code1Gray");
-
 
310
 
-
 
311
    // TODO: iterate through all line frames
-
 
312
    cv::Mat lines0 = frames0LineShift[0];
-
 
313
    cv::Mat lines1 = frames1LineShift[0];
380
 
314
 
381
    // matching
315
    // matching
382
    std::vector<cv::Vec2f> q0Rect, q1Rect;
316
    std::vector<cv::Vec2f> q0Rect, q1Rect;
383
    for(int row=0; row<frameRectRows; row++){
317
    for(int row=0; row<frameRectRows; row++){
384
 
318
 
385
        // edge data structure containing [floor(column), labelLeft, labelRight, orderingRelation]
319
        // line center data structure containing [x-coordinate (sub-px), region-code]
386
        std::vector<cv::Vec4i> edges0, edges1;
320
        std::vector<cv::Vec2f> lineCenters0, lineCenters1;
387
 
321
 
388
        // sorted, unique edges
322
        // sorted, unique line centers
389
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
323
        getlineCenters(lines0.row(row), code0Binary.row(row), lineCenters0);
390
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
324
        getlineCenters(lines1.row(row), code1Binary.row(row), lineCenters1);
391
 
325
 
392
        // match edges
326
        // match and store
393
        std::vector<cv::Vec4i> matchedEdges0, matchedEdges1;
-
 
394
        int i=0, j=0;
327
        int i=0, j=0;
395
        while(i<edges0.size() && j<edges1.size()){
328
        while(i<lineCenters0.size() && j<lineCenters1.size()){
396
 
329
 
397
            if(edges0[i][3] == edges1[j][3]){
330
            if(lineCenters0[i][1] == lineCenters1[j][1]){
398
                matchedEdges0.push_back(edges0[i]);
331
                q0Rect.push_back(cv::Point2f(lineCenters0[0][0], row));
399
                matchedEdges1.push_back(edges1[j]);
332
                q1Rect.push_back(cv::Point2f(lineCenters1[0][0], row));
400
                i += 1;
333
                i += 1;
401
                j += 1;
334
                j += 1;
402
            } else if(edges0[i][3] < edges1[j][3]){
335
            } else if(lineCenters0[i][1] < lineCenters1[j][1]){
403
                i += 1;
336
                i += 1;
404
            } else if(edges0[i][3] > edges1[j][3]){
337
            } else if(lineCenters0[i][1] > lineCenters1[j][1]){
405
                j += 1;
338
                j += 1;
406
            }
339
            }
407
        }
340
        }
408
 
341
 
409
        // crude subpixel refinement
-
 
410
        // finds the intersection of linear interpolants in the positive/negative pattern
-
 
411
        for(int i=0; i<matchedEdges0.size(); i++){
-
 
412
 
-
 
413
            int level = Nbits - leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
-
 
414
 
-
 
415
            // refine for camera 0
-
 
416
            float c0 = matchedEdges0[i][0];
-
 
417
            float c1 = c0+1;
-
 
418
 
-
 
419
            float pos0 = frames0Rect[2*level+2].at<unsigned char>(row, c0);
-
 
420
            float pos1 = frames0Rect[2*level+2].at<unsigned char>(row, c1);
-
 
421
            float neg0 = frames0Rect[2*level+3].at<unsigned char>(row, c0);
-
 
422
            float neg1 = frames0Rect[2*level+3].at<unsigned char>(row, c1);
-
 
423
 
-
 
424
            float col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
-
 
425
            q0Rect.push_back(cv::Point2f(col, row));
-
 
426
 
-
 
427
            // refine for camera 1
-
 
428
            c0 = matchedEdges1[i][0];
-
 
429
            c1 = c0+1;
-
 
430
 
-
 
431
            pos0 = frames1Rect[2*level+2].at<unsigned char>(row, c0);
-
 
432
            pos1 = frames1Rect[2*level+2].at<unsigned char>(row, c1);
-
 
433
            neg0 = frames1Rect[2*level+3].at<unsigned char>(row, c0);
-
 
434
            neg1 = frames1Rect[2*level+3].at<unsigned char>(row, c1);
-
 
435
 
-
 
436
            col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
-
 
437
            q1Rect.push_back(cv::Point2f(col, row));
-
 
438
 
-
 
439
        }
-
 
440
 
-
 
441
    }
342
    }
442
 
343
 
443
    int nMatches = q0Rect.size();
344
    int nMatches = q0Rect.size();
444
 
345
 
445
    if(nMatches < 1){
346
    if(nMatches < 1){
Line 461... Line 362...
461
        color[i] = 0.5*c0 + 0.5*c1;
362
        color[i] = 0.5*c0 + 0.5*c1;
462
    }
363
    }
463
 
364
 
464
    // triangulate points
365
    // triangulate points
465
    cv::Mat QMatHomogenous, QMat;
366
    cv::Mat QMatHomogenous, QMat;
466
//    cv::Mat C0 = P0.clone();
-
 
467
//    cv::Mat C1 = P1.clone();
-
 
468
//    C0.colRange(0, 3) = C0.colRange(0, 3)*R0;
-
 
469
//    C1.colRange(0, 3) = C1.colRange(0, 3)*R1.t();
-
 
-
 
367
 
470
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
368
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
471
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
369
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
472
 
370
 
473
    // undo rectifying rotation
371
    // undo rectifying rotation
474
    cv::Mat R0Inv;
372
    cv::Mat R0Inv;