Subversion Repositories seema-scanner

Rev

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

Rev 207 Rev 231
1
//
1
//
2
// Gray Code Structured Light
2
// Gray Code Structured Light
3
//
3
//
4
// This implementation closely follows Henrik Aanaes, "Lecture Notes on Computer Vision" (2014).
4
// This implementation closely follows Henrik Aanaes, "Lecture Notes on Computer Vision" (2014).
5
//
5
//
6
 
6
 
7
#include "AlgorithmGrayCode.h"
7
#include "AlgorithmGrayCode.h"
8
#include <cmath>
8
#include <cmath>
9
#include "cvtools.h"
9
#include "cvtools.h"
10
#include "algorithmtools.h"
10
#include "algorithmtools.h"
11
 
11
 
12
// Algorithm
12
// Algorithm
13
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
13
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
14
 
14
 
15
    Nbits = ceilf(log2f((float)screenCols)) - 1;
15
    Nbits = ceilf(log2f((float)screenCols)) - 1;
16
    N = 2 + Nbits*2;
16
    N = 2 + Nbits*2;
17
 
17
 
18
    // all on pattern
18
    // all on pattern
19
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
19
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
20
    patterns.push_back(allOn);
20
    patterns.push_back(allOn);
21
 
21
 
22
    // all off pattern
22
    // all off pattern
23
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
23
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
24
    patterns.push_back(allOff);
24
    patterns.push_back(allOff);
25
 
25
 
26
 
26
 
27
    // horizontally encoding patterns
27
    // horizontally encoding patterns
28
    for(unsigned int p=0; p<Nbits; p++){
28
    for(unsigned int p=0; p<Nbits; p++){
29
        cv::Mat pattern(1, screenCols, CV_8UC3);
29
        cv::Mat pattern(1, screenCols, CV_8UC3);
30
        cv::Mat patternInv(1, screenCols, CV_8UC3);
30
        cv::Mat patternInv(1, screenCols, CV_8UC3);
31
 
31
 
32
        for(unsigned int j=0; j<screenCols; j++){
32
        for(unsigned int j=0; j<screenCols; j++){
33
 
33
 
34
            unsigned int jGray = binaryToGray(j);
34
            unsigned int jGray = binaryToGray(j);
35
            // Amplitude of channels
35
            // Amplitude of channels
36
            int bit = (int)getBit(jGray, Nbits-p+1);
36
            int bit = (int)getBit(jGray, Nbits-p+1);
37
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
37
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
38
            int invBit = bit^1;
38
            int invBit = bit^1;
39
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
39
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
40
        }
40
        }
41
        patterns.push_back(pattern);
41
        patterns.push_back(pattern);
42
        patterns.push_back(patternInv);
42
        patterns.push_back(patternInv);
43
    }
43
    }
44
 
44
 
45
 
45
 
46
}
46
}
47
 
47
 
48
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
48
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
49
    return patterns[depth];
49
    return patterns[depth];
50
}
50
}
51
 
51
 
52
 
52
 
53
bool sortingLarger(cv::Vec4i i,cv::Vec4i j){ return (i[3]<j[3]);}
53
bool sortingLarger(cv::Vec4i i,cv::Vec4i j){ return (i[3]<j[3]);}
54
bool sortingEqual(cv::Vec4i i,cv::Vec4i j){ return (i[3]==j[3]);}
54
bool sortingEqual(cv::Vec4i i,cv::Vec4i j){ return (i[3]==j[3]);}
55
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4i>& edges){
55
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4i>& edges){
56
 
56
 
57
    int nCols = scanLine.cols;
57
    int nCols = scanLine.cols;
58
    const int *data = scanLine.ptr<const int>(0);
58
    const int *data = scanLine.ptr<const int>(0);
59
 
59
 
60
    int labelLeft;
60
    int labelLeft;
61
    int labelRight = data[0];
61
    int labelRight = data[0];
62
 
62
 
63
    // collect edges
63
    // collect edges
64
    for(int col=1; col<nCols; col++){
64
    for(int col=1; col<nCols; col++){
65
 
65
 
66
        labelLeft = labelRight;
66
        labelLeft = labelRight;
67
        labelRight = data[col];
67
        labelRight = data[col];
68
 
68
 
69
        // labels need to be non-background, and differ in exactly one bit
69
        // labels need to be non-background, and differ in exactly one bit
70
        if(labelLeft != -1 && labelRight != -1 && (grayToBinary(labelRight) == grayToBinary(labelLeft)+1)){
70
        if(labelLeft != -1 && labelRight != -1 && (grayToBinary(labelRight) == grayToBinary(labelLeft)+1)){
71
            int orderingRelation = (labelLeft << Nbits) + labelRight;
71
            int orderingRelation = (labelLeft << Nbits) + labelRight;
72
            // store left label column
72
            // store left label column
73
            edges.push_back(cv::Vec4i(col-1, labelLeft, labelRight, orderingRelation));
73
            edges.push_back(cv::Vec4i(col-1, labelLeft, labelRight, orderingRelation));
74
        }
74
        }
75
    }
75
    }
76
 
76
 
77
    // sort
77
    // sort
78
    std::sort(edges.begin(), edges.end(), sortingLarger);
78
    std::sort(edges.begin(), edges.end(), sortingLarger);
79
 
79
 
80
    // remove duplicates
80
    // remove duplicates
81
    std::vector<cv::Vec4i>::iterator it;
81
    std::vector<cv::Vec4i>::iterator it;
82
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
82
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
83
    edges.resize(std::distance(edges.begin(),it));
83
    edges.resize(std::distance(edges.begin(),it));
84
}
84
}
85
 
85
 
86
void AlgorithmGrayCode::get3DPoints(const SMCalibrationParameters &calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point3f>& Q, std::vector<cv::Vec3b>& color){
86
void AlgorithmGrayCode::get3DPoints(const SMCalibrationParameters &calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point3f>& Q, std::vector<cv::Vec3b>& color){
87
 
87
 
88
    assert(frames0.size() == N);
88
    assert(frames0.size() == N);
89
    assert(frames1.size() == N);
89
    assert(frames1.size() == N);
90
 
90
 
91
//    for(int i=0; i<1920; i++){
91
//    for(int i=0; i<1920; i++){
92
//        std::cout << i << " " << binaryToGray(i) << " " << grayToBinary(binaryToGray(i)) << std::endl;
92
//        std::cout << i << " " << binaryToGray(i) << " " << grayToBinary(binaryToGray(i)) << std::endl;
93
//    }
93
//    }
94
 
94
 
95
    int frameRows = frames0[0].rows;
95
    int frameRows = frames0[0].rows;
96
    int frameCols = frames0[0].cols;
96
    int frameCols = frames0[0].cols;
97
 
97
 
98
    // rectifying homographies (rotation+projections)
98
    // rectifying homographies (rotation+projections)
99
    cv::Size frameSize(frameCols, frameRows);
99
    cv::Size frameSize(frameCols, frameRows);
100
    cv::Mat R, T;
100
    cv::Mat R, T;
101
    // stereoRectify segfaults unless R is double precision
101
    // stereoRectify segfaults unless R is double precision
102
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
102
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
103
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
103
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
104
    cv::Mat R0, R1, P0, P1, QRect;
104
    cv::Mat R0, R1, P0, P1, QRect;
105
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
105
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
106
 
106
 
107
//    std::cout << "R0" << std::endl << R0 << std::endl;
107
//    std::cout << "R0" << std::endl << R0 << std::endl;
108
//    std::cout << "P0" << std::endl << P0 << std::endl;
108
//    std::cout << "P0" << std::endl << P0 << std::endl;
109
//    std::cout << "R1" << std::endl << R1 << std::endl;
109
//    std::cout << "R1" << std::endl << R1 << std::endl;
110
//    std::cout << "P1" << std::endl << P1 << std::endl;
110
//    std::cout << "P1" << std::endl << P1 << std::endl;
111
 
111
 
112
    // interpolation maps
112
    // interpolation maps
113
    cv::Mat map0X, map0Y, map1X, map1Y;
113
    cv::Mat map0X, map0Y, map1X, map1Y;
114
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
114
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
115
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
115
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
116
 
116
 
117
    // gray-scale and remap
117
    // gray-scale and remap
118
    std::vector<cv::Mat> frames0Rect(N);
118
    std::vector<cv::Mat> frames0Rect(N);
119
    std::vector<cv::Mat> frames1Rect(N);
119
    std::vector<cv::Mat> frames1Rect(N);
120
    for(unsigned int i=0; i<N; i++){
120
    for(unsigned int i=0; i<N; i++){
121
        cv::Mat temp;
121
        cv::Mat temp;
122
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
122
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
123
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
123
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
124
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
124
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
125
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
125
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
126
    }
126
    }
127
 
127
 
128
 
128
 
129
//    cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
129
//    cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
130
//    cvtools::writeMat(frames0[0], "frames0_0.mat", "frames0_0");
130
//    cvtools::writeMat(frames0[0], "frames0_0.mat", "frames0_0");
131
 
131
 
132
//    cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
132
//    cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
133
//    cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
133
//    cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
134
 
134
 
135
//    cv::imwrite("frames0[0].png", frames0[0]);
135
//    cv::imwrite("frames0[0].png", frames0[0]);
136
//    cv::imwrite("frames0Rect[0].png", frames0Rect[0]);
136
//    cv::imwrite("frames0Rect[0].png", frames0Rect[0]);
137
 
137
 
138
//    cv::imwrite("frames1[0].png", frames1[0]);
138
//    cv::imwrite("frames1[0].png", frames1[0]);
139
//    cv::imwrite("frames1Rect[0].png", frames1Rect[0]);
139
//    cv::imwrite("frames1Rect[0].png", frames1Rect[0]);
140
 
140
 
141
    // color debayer and remap
141
    // color debayer and remap
142
    cv::Mat color0Rect, color1Rect;
142
    cv::Mat color0Rect, color1Rect;
143
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
143
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
144
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
144
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
145
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
145
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
146
 
146
 
147
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
147
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
148
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
148
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
149
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
149
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
150
 
150
 
151
    int frameRectRows = frames0Rect[0].rows;
151
    int frameRectRows = frames0Rect[0].rows;
152
    int frameRectCols = frames0Rect[0].cols;
152
    int frameRectCols = frames0Rect[0].cols;
153
 
153
 
154
//cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
154
//cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
155
//cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
155
//cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
156
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
156
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
157
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
157
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
158
 
158
 
159
    // occlusion masks
159
    // occlusion masks
160
    cv::Mat occlusion0Rect, occlusion1Rect;
160
    cv::Mat occlusion0Rect, occlusion1Rect;
161
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
161
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
162
    occlusion0Rect = (occlusion0Rect > 10) & (occlusion0Rect < 250);
162
    occlusion0Rect = (occlusion0Rect > 10) & (occlusion0Rect < 250);
163
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
163
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
164
    occlusion1Rect = (occlusion1Rect > 10) & (occlusion1Rect < 250);
164
    occlusion1Rect = (occlusion1Rect > 10) & (occlusion1Rect < 250);
165
 
165
 
166
    // erode occlusion masks
166
    // erode occlusion masks
167
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2,2));
167
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2,2));
168
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
168
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
169
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
169
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
170
 
170
 
171
//cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
171
//cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
172
//cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
172
//cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
173
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
173
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
174
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
174
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
175
 
175
 
176
//    // correct for projector inversion error
176
//    // correct for projector inversion error
177
//    cv::Mat W;
177
//    cv::Mat W;
178
//    cv::add(frames0Rect[0], frames0Rect[1], W, cv::noArray(), CV_32F);
178
//    cv::add(frames0Rect[0], frames0Rect[1], W, cv::noArray(), CV_32F);
179
//    for(int i=2; i<N; i+=2){
179
//    for(int i=2; i<N; i+=2){
180
//        cv::Mat S, E;
180
//        cv::Mat S, E;
181
//        cv::add(frames0Rect[i], frames0Rect[i+1], S, cv::noArray(), CV_32F);
181
//        cv::add(frames0Rect[i], frames0Rect[i+1], S, cv::noArray(), CV_32F);
182
//        cv::subtract(W, S, E, cv::noArray(), CV_32F);
182
//        cv::subtract(W, S, E, cv::noArray(), CV_32F);
183
//        E *= 0.5;
183
//        E *= 0.5;
184
//        cv::add(frames0Rect[i], E, frames0Rect[i], cv::noArray(), CV_16UC1);
184
//        cv::add(frames0Rect[i], E, frames0Rect[i], cv::noArray(), CV_16UC1);
185
//        cv::add(frames0Rect[i+1], E, frames0Rect[i+1], cv::noArray(), CV_16UC1);
185
//        cv::add(frames0Rect[i+1], E, frames0Rect[i+1], cv::noArray(), CV_16UC1);
186
//    }
186
//    }
187
 
187
 
188
 
188
 
189
//    // correct for texture modulation and ambient
189
//    // correct for texture modulation and ambient
190
//    cv::Mat A0 = frames0Rect[1];
190
//    cv::Mat A0 = frames0Rect[1];
191
//    cv::Mat M0 = frames0Rect[0]-frames0Rect[1];
191
//    cv::Mat M0 = frames0Rect[0]-frames0Rect[1];
192
////cvtools::writeMat(A0, "A0.mat", "A0");
192
////cvtools::writeMat(A0, "A0.mat", "A0");
193
////cvtools::writeMat(M0, "M0.mat", "M0");
193
////cvtools::writeMat(M0, "M0.mat", "M0");
194
////cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
194
////cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
195
////cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
195
////cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
196
//    cv::divide(256.0, M0, M0, CV_32F);
196
//    cv::divide(256.0, M0, M0, CV_32F);
197
//    cv::Mat A1 = frames1Rect[1];
197
//    cv::Mat A1 = frames1Rect[1];
198
//    cv::Mat M1 = frames1Rect[0]-frames1Rect[1];
198
//    cv::Mat M1 = frames1Rect[0]-frames1Rect[1];
199
//    cv::divide(256.0, M1, M1, CV_32F);
199
//    cv::divide(256.0, M1, M1, CV_32F);
200
 
200
 
201
//    for(int i=2; i<N; i++){
201
//    for(int i=2; i<N; i++){
202
//        cv::multiply(frames0Rect[i]-A0, M0, frames0Rect[i], 1.0, CV_8UC1);
202
//        cv::multiply(frames0Rect[i]-A0, M0, frames0Rect[i], 1.0, CV_8UC1);
203
//        cv::multiply(frames1Rect[i]-A1, M1, frames1Rect[i], 1.0, CV_8UC1);
203
//        cv::multiply(frames1Rect[i]-A1, M1, frames1Rect[i], 1.0, CV_8UC1);
204
//    }
204
//    }
205
 
205
 
206
//cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
206
//cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
207
//cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
207
//cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
208
 
208
 
209
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
209
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
210
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
210
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
211
 
211
 
212
    // decode patterns
212
    // decode patterns
213
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
213
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
214
    cv::Mat code1Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
214
    cv::Mat code1Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
215
 
215
 
216
    // into gray code
216
    // into gray code
217
    for(unsigned int i=0; i<Nbits; i++){
217
    for(unsigned int i=0; i<Nbits; i++){
218
        cv::Mat temp, bit0, bit1;
218
        cv::Mat temp, bit0, bit1;
219
 
219
 
220
        cv::compare(frames0Rect[i*2+2], frames0Rect[i*2+3], temp, cv::CMP_GT);
220
        cv::compare(frames0Rect[i*2+2], frames0Rect[i*2+3], temp, cv::CMP_GT);
221
        temp.convertTo(bit0, CV_32S, 1.0/255.0);
221
        temp.convertTo(bit0, CV_32S, 1.0/255.0);
222
        cv::add(code0Rect, bit0*twopowi(Nbits-i-1), code0Rect, cv::noArray(), CV_32S);
222
        cv::add(code0Rect, bit0*twopowi(Nbits-i-1), code0Rect, cv::noArray(), CV_32S);
223
 
223
 
224
        cv::compare(frames1Rect[i*2+2], frames1Rect[i*2+3], temp, cv::CMP_GT);
224
        cv::compare(frames1Rect[i*2+2], frames1Rect[i*2+3], temp, cv::CMP_GT);
225
        temp.convertTo(bit1, CV_32S, 1.0/255.0);
225
        temp.convertTo(bit1, CV_32S, 1.0/255.0);
226
        cv::add(code1Rect, bit1*twopowi(Nbits-i-1), code1Rect, cv::noArray(), CV_32S);
226
        cv::add(code1Rect, bit1*twopowi(Nbits-i-1), code1Rect, cv::noArray(), CV_32S);
227
    }
227
    }
228
 
228
 
229
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
229
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
230
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
230
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
231
 
231
 
232
 
232
 
233
//    // convert to standard binary
233
//    // convert to standard binary
234
//    cv::Mat code0Binary(code0Rect.rows, code0Rect.cols, CV_32F);
234
//    cv::Mat code0Binary(code0Rect.rows, code0Rect.cols, CV_32F);
235
//    cv::Mat code1Binary(code1Rect.rows, code1Rect.cols, CV_32F);
235
//    cv::Mat code1Binary(code1Rect.rows, code1Rect.cols, CV_32F);
236
//    for(int r=0; r<frameRectRows; r++){
236
//    for(int r=0; r<frameRectRows; r++){
237
//        for(int c=0; c<frameRectCols; c++){
237
//        for(int c=0; c<frameRectCols; c++){
238
//            if(code0Rect.at<int>(r,c) != -1)
238
//            if(code0Rect.at<int>(r,c) != -1)
239
//                code0Binary.at<float>(r,c) = grayToBinary(code0Rect.at<int>(r,c));
239
//                code0Binary.at<float>(r,c) = grayToBinary(code0Rect.at<int>(r,c));
240
//            if(code1Rect.at<int>(r,c) != -1)
240
//            if(code1Rect.at<int>(r,c) != -1)
241
//                code1Binary.at<float>(r,c) = grayToBinary(code1Rect.at<int>(r,c));
241
//                code1Binary.at<float>(r,c) = grayToBinary(code1Rect.at<int>(r,c));
242
//        }
242
//        }
243
//    }
243
//    }
244
 
244
 
245
//cvtools::writeMat(code0Binary, "code0Binary.mat", "code0Binary");
245
//cvtools::writeMat(code0Binary, "code0Binary.mat", "code0Binary");
246
//cvtools::writeMat(code1Binary, "code1Binary.mat", "code1Binary");
246
//cvtools::writeMat(code1Binary, "code1Binary.mat", "code1Binary");
247
 
247
 
248
//    // threshold on vertical discontinuities (due to imperfect rectification)
248
//    // threshold on vertical discontinuities (due to imperfect rectification)
249
//    cv::Mat edges0;
249
//    cv::Mat edges0;
250
//    cv::Sobel(code0Binary, edges0, -1, 0, 1, 5);
250
//    cv::Sobel(code0Binary, edges0, -1, 0, 1, 5);
251
//    occlusion0Rect = occlusion0Rect & (abs(edges0) < 50);
251
//    occlusion0Rect = occlusion0Rect & (abs(edges0) < 50);
252
 
252
 
253
//    cv::Mat edges1;
253
//    cv::Mat edges1;
254
//    cv::Sobel(code1Binary, edges1, -1, 0, 1, 5);
254
//    cv::Sobel(code1Binary, edges1, -1, 0, 1, 5);
255
//    occlusion1Rect = occlusion1Rect & (abs(edges1) < 50);
255
//    occlusion1Rect = occlusion1Rect & (abs(edges1) < 50);
256
 
256
 
257
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
257
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
258
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
258
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
259
 
259
 
260
    // set occluded pixels to -1
260
    // set occluded pixels to -1
261
    for(int r=0; r<frameRectRows; r++){
261
    for(int r=0; r<frameRectRows; r++){
262
        for(int c=0; c<frameRectCols; c++){
262
        for(int c=0; c<frameRectCols; c++){
263
            if(occlusion0Rect.at<unsigned char>(r,c) == 0)
263
            if(occlusion0Rect.at<unsigned char>(r,c) == 0)
264
                code0Rect.at<int>(r,c) = -1;
264
                code0Rect.at<int>(r,c) = -1;
265
            if(occlusion1Rect.at<unsigned char>(r,c) == 0)
265
            if(occlusion1Rect.at<unsigned char>(r,c) == 0)
266
                code1Rect.at<int>(r,c) = -1;
266
                code1Rect.at<int>(r,c) = -1;
267
        }
267
        }
268
    }
268
    }
269
 
269
 
270
//    cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
270
//    cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
271
//    cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
271
//    cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
272
 
272
 
273
    // matching
273
    // matching
274
    std::vector<cv::Vec2f> q0Rect, q1Rect;
274
    std::vector<cv::Vec2f> q0Rect, q1Rect;
275
    for(int row=0; row<frameRectRows; row++){
275
    for(int row=0; row<frameRectRows; row++){
276
 
276
 
277
        // edge data structure containing [floor(column), labelLeft, labelRight, orderingRelation]
277
        // edge data structure containing [floor(column), labelLeft, labelRight, orderingRelation]
278
        std::vector<cv::Vec4i> edges0, edges1;
278
        std::vector<cv::Vec4i> edges0, edges1;
279
 
279
 
280
        // sorted, unique edges
280
        // sorted, unique edges
281
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
281
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
282
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
282
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
283
 
283
 
284
        // match edges
284
        // match edges
285
        std::vector<cv::Vec4i> matchedEdges0, matchedEdges1;
285
        std::vector<cv::Vec4i> matchedEdges0, matchedEdges1;
286
        unsigned int i=0, j=0;
286
        unsigned int i=0, j=0;
287
        while(i<edges0.size() && j<edges1.size()){
287
        while(i<edges0.size() && j<edges1.size()){
288
 
288
 
289
            if(edges0[i][3] == edges1[j][3]){
289
            if(edges0[i][3] == edges1[j][3]){
290
                matchedEdges0.push_back(edges0[i]);
290
                matchedEdges0.push_back(edges0[i]);
291
                matchedEdges1.push_back(edges1[j]);
291
                matchedEdges1.push_back(edges1[j]);
292
                i += 1;
292
                i += 1;
293
                j += 1;
293
                j += 1;
294
            } else if(edges0[i][3] < edges1[j][3]){
294
            } else if(edges0[i][3] < edges1[j][3]){
295
                i += 1;
295
                i += 1;
296
            } else if(edges0[i][3] > edges1[j][3]){
296
            } else if(edges0[i][3] > edges1[j][3]){
297
                j += 1;
297
                j += 1;
298
            }
298
            }
299
        }
299
        }
300
 
300
 
301
        // crude subpixel refinement
301
        // crude subpixel refinement
302
        // finds the intersection of linear interpolants in the positive/negative pattern
302
        // finds the intersection of linear interpolants in the positive/negative pattern
303
        for(unsigned int i=0; i<matchedEdges0.size(); i++){
303
        for(unsigned int i=0; i<matchedEdges0.size(); i++){
304
 
304
 
305
            int level = Nbits - leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
305
            int level = Nbits - leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
306
 
306
 
307
            // refine for camera 0
307
            // refine for camera 0
308
            float c0 = matchedEdges0[i][0];
308
            float c0 = matchedEdges0[i][0];
309
            float c1 = c0+1;
309
            float c1 = c0+1;
310
 
310
 
311
            float pos0 = frames0Rect[2*level+2].at<unsigned char>(row, c0);
311
            float pos0 = frames0Rect[2*level+2].at<unsigned char>(row, c0);
312
            float pos1 = frames0Rect[2*level+2].at<unsigned char>(row, c1);
312
            float pos1 = frames0Rect[2*level+2].at<unsigned char>(row, c1);
313
            float neg0 = frames0Rect[2*level+3].at<unsigned char>(row, c0);
313
            float neg0 = frames0Rect[2*level+3].at<unsigned char>(row, c0);
314
            float neg1 = frames0Rect[2*level+3].at<unsigned char>(row, c1);
314
            float neg1 = frames0Rect[2*level+3].at<unsigned char>(row, c1);
315
 
315
 
316
            float col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
316
            float col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
317
            q0Rect.push_back(cv::Point2f(col, row));
317
            q0Rect.push_back(cv::Point2f(col, row));
318
 
318
 
319
            // refine for camera 1
319
            // refine for camera 1
320
            c0 = matchedEdges1[i][0];
320
            c0 = matchedEdges1[i][0];
321
            c1 = c0+1;
321
            c1 = c0+1;
322
 
322
 
323
            pos0 = frames1Rect[2*level+2].at<unsigned char>(row, c0);
323
            pos0 = frames1Rect[2*level+2].at<unsigned char>(row, c0);
324
            pos1 = frames1Rect[2*level+2].at<unsigned char>(row, c1);
324
            pos1 = frames1Rect[2*level+2].at<unsigned char>(row, c1);
325
            neg0 = frames1Rect[2*level+3].at<unsigned char>(row, c0);
325
            neg0 = frames1Rect[2*level+3].at<unsigned char>(row, c0);
326
            neg1 = frames1Rect[2*level+3].at<unsigned char>(row, c1);
326
            neg1 = frames1Rect[2*level+3].at<unsigned char>(row, c1);
327
 
327
 
328
            col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
328
            col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
329
            q1Rect.push_back(cv::Point2f(col, row));
329
            q1Rect.push_back(cv::Point2f(col, row));
330
 
330
 
331
        }
331
        }
332
 
332
 
333
    }
333
    }
334
 
334
 
335
    int nMatches = q0Rect.size();
335
    int nMatches = q0Rect.size();
336
 
336
 
337
    if(nMatches < 1){
337
    if(nMatches < 1){
338
        Q.resize(0);
338
        Q.resize(0);
339
        color.resize(0);
339
        color.resize(0);
340
 
340
 
341
        return;
341
        return;
342
    }
342
    }
343
 
343
 
344
    // retrieve color information (at integer coordinates)
344
    // retrieve color information (at integer coordinates)
345
    color.resize(nMatches);
345
    color.resize(nMatches);
346
    for(int i=0; i<nMatches; i++){
346
    for(int i=0; i<nMatches; i++){
347
 
347
 
348
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
348
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
349
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
349
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
350
 
350
 
351
        color[i] = 0.5*c0 + 0.5*c1;
351
        color[i] = 0.5*c0 + 0.5*c1;
352
    }
352
    }
353
 
353
 
354
    // triangulate points
354
//    // Triangulate points
355
    cv::Mat QMatHomogenous, QMat;
355
//    cv::Mat QMatHomogenous, QMat;
356
//    cv::Mat C0 = P0.clone();
356
////    cv::Mat C0 = P0.clone();
357
//    cv::Mat C1 = P1.clone();
357
////    cv::Mat C1 = P1.clone();
358
//    C0.colRange(0, 3) = C0.colRange(0, 3)*R0;
358
////    C0.colRange(0, 3) = C0.colRange(0, 3)*R0;
359
//    C1.colRange(0, 3) = C1.colRange(0, 3)*R1.t();
359
////    C1.colRange(0, 3) = C1.colRange(0, 3)*R1.t();
360
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
360
//    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
361
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
361
//    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
362
 
-
 
363
    // undo rectifying rotation
-
 
364
    cv::Mat R0Inv;
-
 
365
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
-
 
366
    QMat = R0Inv*QMat;
-
 
367
 
362
 
-
 
363
//    // undo rectifying rotation
-
 
364
//    cv::Mat R0Inv;
-
 
365
//    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
-
 
366
//    QMat = R0Inv*QMat;
-
 
367
 
368
    cvtools::matToPoints3f(QMat, Q);
368
//    cvtools::matToPoints3f(QMat, Q);
-
 
369
 
-
 
370
 
-
 
371
    // Triangulate by means of disparity projection
-
 
372
    Q.resize(q0Rect.size());
-
 
373
    cv::Matx44f QRectx = cv::Matx44f(QRect);
-
 
374
    cv::Matx33f R0invx = cv::Matx33f(cv::Mat(R0.t()));
-
 
375
 
-
 
376
    #pragma omp parallel for
-
 
377
    for(unsigned int i=0; i<q0Rect.size(); i++){
-
 
378
        float disparity = q0Rect[i][0]-q1Rect[i][0];
-
 
379
        cv::Vec4f Qih = QRectx*cv::Vec4f(q0Rect[i][0], q0Rect[i][1], disparity, 1.0);
-
 
380
        float winv = float(1.0)/Qih[3];
-
 
381
        Q[i] = R0invx * cv::Point3f(Qih[0]*winv, Qih[1]*winv, Qih[2]*winv);
-
 
382
    }
369
 
383
 
370
}
384
}
371
 
385