Subversion Repositories seema-scanner

Rev

Rev 42 | Rev 44 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 42 Rev 43
Line 114... Line 114...
114
    const short *data = scanLine.ptr<const short>(0);
114
    const short *data = scanLine.ptr<const short>(0);
115
 
115
 
116
    short labelLeft;
116
    short labelLeft;
117
    short labelRight = data[0];
117
    short labelRight = data[0];
118
 
118
 
-
 
119
    // collect edges
119
    for(int col=1; col<nCols; col++){
120
    for(int col=1; col<nCols; col++){
120
 
121
 
121
        labelLeft = labelRight;
122
        labelLeft = labelRight;
122
        labelRight = data[col];
123
        labelRight = data[col];
123
 
124
 
124
        if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
125
        if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
125
 
-
 
126
            int orderingRelation = (2 << Nbits)*labelLeft + labelRight;
126
            int orderingRelation = (labelLeft << Nbits) + labelRight;
127
 
-
 
128
            edges.push_back(cv::Vec4f(col, labelLeft, labelRight, orderingRelation));
127
            edges.push_back(cv::Vec4f(col-0.5, labelLeft, labelRight, orderingRelation));
129
 
-
 
130
        }
128
        }
131
    }
129
    }
132
 
130
 
133
    // sort
131
    // sort
134
    std::sort(edges.begin(), edges.end(), sortingLarger);
132
    std::sort(edges.begin(), edges.end(), sortingLarger);
Line 145... Line 143...
145
    assert(frames1.size() == N);
143
    assert(frames1.size() == N);
146
 
144
 
147
    int frameRows = frames0[0].rows;
145
    int frameRows = frames0[0].rows;
148
    int frameCols = frames0[0].cols;
146
    int frameCols = frames0[0].cols;
149
 
147
 
150
    // convert to gray-scale
-
 
151
    std::vector<cv::Mat> frames0Gray(N);
-
 
152
    std::vector<cv::Mat> frames1Gray(N);
-
 
153
    for(int i=0; i<N; i++){
-
 
154
        cv::cvtColor(frames0[i], frames0Gray[i], CV_RGB2GRAY);
-
 
155
        cv::cvtColor(frames1[i], frames1Gray[i], CV_RGB2GRAY);
-
 
156
    }
-
 
157
 
-
 
158
    // occlusion maps
-
 
159
    cv::Mat occlusion0, occlusion1;
-
 
160
    cv::subtract(frames0Gray[0], frames0Gray[1], occlusion0);
-
 
161
    occlusion0 = occlusion0 > 50;
-
 
162
    cv::subtract(frames1Gray[0], frames1Gray[1], occlusion1);
-
 
163
    occlusion1 = occlusion1 > 50;
-
 
164
 
-
 
165
//    cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
-
 
166
//    cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
-
 
167
 
-
 
168
    // decoded patterns
-
 
169
    cv::Mat code0(frameRows, frameCols, CV_16S, cv::Scalar(-1)), code1(frameRows, frameCols, CV_16S, cv::Scalar(-1));
-
 
170
    cvtools::writeMat(code0, "code0.mat", "code0");
-
 
171
    cvtools::writeMat(code1, "code1.mat", "code1");
-
 
172
    for(int i=0; i<Nbits; i++){
-
 
173
        cv::Mat bit0;
-
 
174
        cv::subtract(frames0Gray[i*2+2], frames0Gray[i*2+3], bit0);
-
 
175
        bit0 = bit0 > 50;
-
 
176
//    cvtools::writeMat(bit0, "bit0.mat", "bit0");
-
 
177
        cv::add(code0, bit0/255*powi(2,i), code0, occlusion0, CV_16S);
-
 
178
//    cvtools::writeMat(code0, "code0.mat", "code0");
-
 
179
        cv::Mat bit1;
-
 
180
        cv::subtract(frames1Gray[i*2+2], frames1Gray[i*2+3], bit1);
-
 
181
        bit1 = bit1 > 50;
-
 
182
        cv::add(code1, bit1/255*powi(2,i), code1, occlusion1, CV_16S);
-
 
183
    }
-
 
184
 
-
 
185
    cvtools::writeMat(code0, "code0.mat", "code0");
-
 
186
    cvtools::writeMat(code1, "code1.mat", "code1");
-
 
187
 
-
 
188
    // rectifying homographies (rotation+projections)
148
    // rectifying homographies (rotation+projections)
189
    cv::Size frameSize(frameCols, frameRows);
149
    cv::Size frameSize(frameCols, frameRows);
190
    cv::Mat R, T;
150
    cv::Mat R, T;
191
    // stereoRectify segfaults unless R is double precision
151
    // stereoRectify segfaults unless R is double precision
192
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
152
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
193
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
153
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
194
    cv::Mat R0, R1, P0, P1, QRect;
154
    cv::Mat R0, R1, P0, P1, QRect;
195
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
155
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
196
 
156
 
-
 
157
    std::cout << "R0" << std::endl << R0 << std::endl;
-
 
158
    std::cout << "P0" << std::endl << P0 << std::endl;
-
 
159
    std::cout << "R1" << std::endl << R1 << std::endl;
-
 
160
    std::cout << "P1" << std::endl << P1 << std::endl;
-
 
161
 
197
    // interpolation maps
162
    // interpolation maps
198
    cv::Mat map0X, map0Y, map1X, map1Y;
163
    cv::Mat map0X, map0Y, map1X, map1Y;
199
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
164
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
200
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
165
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
201
 
166
 
202
    // remap
167
    // gray-scale and remap
-
 
168
    std::vector<cv::Mat> frames0Rect(N);
203
    cv::Mat code0Rect, code1Rect, color0Rect, color1Rect;
169
    std::vector<cv::Mat> frames1Rect(N);
-
 
170
    for(int i=0; i<N; i++){
-
 
171
        cv::Mat temp;
204
    cv::remap(code0, code0Rect, map0X, map0Y, cv::INTER_NEAREST);
172
        cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
205
    cv::remap(code1, code1Rect, map1X, map1Y, cv::INTER_NEAREST);
173
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
206
    cv::remap(frames0[0], color0Rect, map0X, map0Y, cv::INTER_CUBIC);
174
        cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
207
    cv::remap(frames1[0], color1Rect, map1X, map1Y, cv::INTER_CUBIC);
175
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
-
 
176
    }
208
 
177
 
-
 
178
    // color remaps
-
 
179
    cv::Mat color0Rect, color1Rect;
209
cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
180
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
210
cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
181
    cv::remap(frames1[0], color1Rect, map0X, map0Y, CV_INTER_CUBIC);
211
 
182
 
212
//cvtools::writeMat(color0Rect, "color0.mat", "color0");
183
    int frameRectRows = frames0Rect[0].rows;
213
//cvtools::writeMat(color1Rect, "color1.mat", "color1");
184
    int frameRectCols = frames0Rect[0].cols;
214
 
185
 
-
 
186
    // occlusion maps
-
 
187
    cv::Mat occlusion0Rect, occlusion1Rect;
-
 
188
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
215
    int nRows = code0Rect.rows;
189
    occlusion0Rect = occlusion0Rect > 50;
-
 
190
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
-
 
191
    occlusion1Rect = occlusion1Rect > 50;
-
 
192
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
-
 
193
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
-
 
194
 
216
    int nCols = code0Rect.cols;
195
    // decode patterns
-
 
196
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_16S, cv::Scalar(-1));
-
 
197
    cv::Mat code1Rect(frameRectRows, frameRectCols, CV_16S, cv::Scalar(-1));
-
 
198
    cv::add(code0Rect, 1, code0Rect, occlusion0Rect, CV_16S);
-
 
199
    cv::add(code1Rect, 1, code1Rect, occlusion1Rect, CV_16S);
-
 
200
 
-
 
201
    for(int i=0; i<Nbits; i++){
-
 
202
        cv::Mat bit0;
-
 
203
        cv::subtract(frames0Rect[i*2+2], frames0Rect[i*2+3], bit0, cv::noArray(), CV_16S);
-
 
204
        bit0 = bit0 > 0;
-
 
205
//    cvtools::writeMat(bit0, "bit0.mat", "bit0");
-
 
206
        cv::add(code0Rect, bit0/255*powi(2,i), code0Rect, occlusion0Rect, CV_16S);
-
 
207
//    cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
-
 
208
        cv::Mat bit1;
-
 
209
        cv::subtract(frames1Rect[i*2+2], frames1Rect[i*2+3], bit1, cv::noArray(), CV_16S);
-
 
210
        bit1 = bit1 > 0;
-
 
211
        cv::add(code1Rect, bit1/255*powi(2,i), code1Rect, occlusion1Rect, CV_16S);
-
 
212
    }
-
 
213
 
-
 
214
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
-
 
215
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
217
 
216
 
218
    // matching
217
    // matching
219
    std::vector<cv::Vec2f> q0Rect, q1Rect;
218
    std::vector<cv::Vec2f> q0Rect, q1Rect;
220
    for(int row=0; row<nRows; row++){
219
    for(int row=0; row<frameRectRows; row++){
221
 
220
 
222
        std::vector<cv::Vec4f> edges0, edges1;
221
        std::vector<cv::Vec4f> edges0, edges1;
223
 
222
 
-
 
223
        // sorted, unique edges
224
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
224
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
225
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
225
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
226
 
226
 
-
 
227
        // subpixel refinement
-
 
228
        // ...
-
 
229
 
227
        int i=0, j=0;
230
        int i=0, j=0;
228
        while(i<edges0.size() && j<edges1.size()){
231
        while(i<edges0.size() && j<edges1.size()){
229
 
232
 
230
            if(edges0[i][3] == edges1[j][3]){
233
            if(edges0[i][3] == edges1[j][3]){
231
                q0Rect.push_back(cv::Vec2f(edges0[i][0], row));
234
                q0Rect.push_back(cv::Vec2f(edges0[i][0], row));