Subversion Repositories seema-scanner

Rev

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

Rev 45 Rev 47
Line 37... Line 37...
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
/*
-
 
43
 * Return the number of bits set in an integer
-
 
44
 */
-
 
45
static int countBits(int n) {
-
 
46
  unsigned int c; // c accumulates the total bits set in v
-
 
47
  for (c = 0; n>0; c++)
-
 
48
    n &= n - 1; // clear the least significant bit set
-
 
49
  return c;
-
 
50
}
-
 
51
 
-
 
52
/*
-
 
53
 * Return the position of the least significant bit that is set
-
 
54
 */
-
 
55
static int leastSignificantBitSet(int x){
-
 
56
  if(x == 0)
-
 
57
      return 0;
-
 
58
 
-
 
59
  int val = 1;
-
 
60
  while(x>>=1)
-
 
61
      val++;
-
 
62
 
-
 
63
  return val;
-
 
64
}
-
 
65
 
42
//static int get_bit(int decimal, int N){
66
//static int get_bit(int decimal, int N){
43
 
67
 
44
//    // Shifting the 1 for N-1 bits
68
//    // Shifting the 1 for N-1 bits
45
//    int constant = 1 << (N-1);
69
//    int constant = 1 << (N-1);
46
 
70
 
Line 108... Line 132...
108
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
132
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
109
    return patterns[depth];
133
    return patterns[depth];
110
}
134
}
111
 
135
 
112
 
136
 
113
bool sortingLarger(cv::Vec4f i,cv::Vec4f j){ return (i[3]<j[3]);}
137
bool sortingLarger(cv::Vec4i i,cv::Vec4i j){ return (i[3]<j[3]);}
114
bool sortingEqual(cv::Vec4f i,cv::Vec4f j){ return (i[3]==j[3]);}
138
bool sortingEqual(cv::Vec4i i,cv::Vec4i j){ return (i[3]==j[3]);}
115
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4f>& edges){
139
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4i>& edges){
116
 
140
 
117
    int nCols = scanLine.cols;
141
    int nCols = scanLine.cols;
118
    const int *data = scanLine.ptr<const int>(0);
142
    const int *data = scanLine.ptr<const int>(0);
119
 
143
 
120
    int labelLeft;
144
    int labelLeft;
Line 124... Line 148...
124
    for(int col=1; col<nCols; col++){
148
    for(int col=1; col<nCols; col++){
125
 
149
 
126
        labelLeft = labelRight;
150
        labelLeft = labelRight;
127
        labelRight = data[col];
151
        labelRight = data[col];
128
 
152
 
-
 
153
        // labels need to non-background, and differ in exactly one bit
129
        if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
154
        if(labelLeft != -1 && labelRight != -1 && countBits(labelLeft^labelRight) == 1){
130
            int orderingRelation = (labelLeft << Nbits) + labelRight;
155
            int orderingRelation = (labelLeft << Nbits) + labelRight;
-
 
156
            // store left label column
131
            edges.push_back(cv::Vec4f(col-0.5, labelLeft, labelRight, orderingRelation));
157
            edges.push_back(cv::Vec4i(col-1, labelLeft, labelRight, orderingRelation));
132
        }
158
        }
133
    }
159
    }
134
 
160
 
135
    // sort
161
    // sort
136
    std::sort(edges.begin(), edges.end(), sortingLarger);
162
    std::sort(edges.begin(), edges.end(), sortingLarger);
137
 
163
 
138
    // remove duplicates
164
    // remove duplicates
139
    std::vector<cv::Vec4f>::iterator it;
165
    std::vector<cv::Vec4i>::iterator it;
140
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
166
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
141
    edges.resize(std::distance(edges.begin(),it));
167
    edges.resize(std::distance(edges.begin(),it));
142
}
168
}
143
 
169
 
144
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
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){
145
 
171
 
146
    assert(frames0.size() == N);
172
    assert(frames0.size() == N);
147
    assert(frames1.size() == N);
173
    assert(frames1.size() == N);
148
 
174
 
149
    for(int i=0; i<1920; i++){
175
//    for(int i=0; i<1920; i++){
150
        std::cout << i << " " << binaryToGray(i) << " " << grayToBinary(binaryToGray(i)) << std::endl;
176
//        std::cout << i << " " << binaryToGray(i) << " " << grayToBinary(binaryToGray(i)) << std::endl;
151
    }
177
//    }
152
 
-
 
153
 
178
 
154
    int frameRows = frames0[0].rows;
179
    int frameRows = frames0[0].rows;
155
    int frameCols = frames0[0].cols;
180
    int frameCols = frames0[0].cols;
156
 
181
 
157
    // rectifying homographies (rotation+projections)
182
    // rectifying homographies (rotation+projections)
Line 161... Line 186...
161
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
186
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
162
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
187
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
163
    cv::Mat R0, R1, P0, P1, QRect;
188
    cv::Mat R0, R1, P0, P1, QRect;
164
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
189
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
165
 
190
 
166
    std::cout << "R0" << std::endl << R0 << std::endl;
191
//    std::cout << "R0" << std::endl << R0 << std::endl;
167
    std::cout << "P0" << std::endl << P0 << std::endl;
192
//    std::cout << "P0" << std::endl << P0 << std::endl;
168
    std::cout << "R1" << std::endl << R1 << std::endl;
193
//    std::cout << "R1" << std::endl << R1 << std::endl;
169
    std::cout << "P1" << std::endl << P1 << std::endl;
194
//    std::cout << "P1" << std::endl << P1 << std::endl;
170
 
195
 
171
    // interpolation maps
196
    // interpolation maps
172
    cv::Mat map0X, map0Y, map1X, map1Y;
197
    cv::Mat map0X, map0Y, map1X, map1Y;
173
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
198
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
174
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
199
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
Line 177... Line 202...
177
    std::vector<cv::Mat> frames0Rect(N);
202
    std::vector<cv::Mat> frames0Rect(N);
178
    std::vector<cv::Mat> frames1Rect(N);
203
    std::vector<cv::Mat> frames1Rect(N);
179
    for(int i=0; i<N; i++){
204
    for(int i=0; i<N; i++){
180
        cv::Mat temp;
205
        cv::Mat temp;
181
        cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
206
        cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
182
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
207
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_CUBIC);
183
        cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
208
        cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
184
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
209
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_CUBIC);
185
    }
210
    }
186
 
211
 
-
 
212
//    cv::imwrite("frames0Rect[N-1].png", frames0Rect[N-1]);
-
 
213
 
187
    // color remaps
214
    // color remaps
188
    cv::Mat color0Rect, color1Rect;
215
    cv::Mat color0Rect, color1Rect;
189
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
216
    cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
190
    cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
217
    cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
191
 
218
 
192
    int frameRectRows = frames0Rect[0].rows;
219
    int frameRectRows = frames0Rect[0].rows;
193
    int frameRectCols = frames0Rect[0].cols;
220
    int frameRectCols = frames0Rect[0].cols;
194
 
221
 
195
    // occlusion maps
222
    // occlusion masks
196
    cv::Mat occlusion0Rect, occlusion1Rect;
223
    cv::Mat occlusion0Rect, occlusion1Rect;
197
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
224
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
198
    occlusion0Rect = occlusion0Rect > 50;
225
    occlusion0Rect = occlusion0Rect > 50;
199
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
226
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
200
    occlusion1Rect = occlusion1Rect > 50;
227
    occlusion1Rect = occlusion1Rect > 50;
-
 
228
 
-
 
229
    // erode occlusion masks
-
 
230
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3,3));
-
 
231
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
-
 
232
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
-
 
233
 
201
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
234
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
202
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
235
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
203
 
236
 
204
    // decode patterns
237
    // decode patterns
205
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(-1));
238
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(-1));
Line 240... Line 273...
240
 
273
 
241
    // matching
274
    // matching
242
    std::vector<cv::Vec2f> q0Rect, q1Rect;
275
    std::vector<cv::Vec2f> q0Rect, q1Rect;
243
    for(int row=0; row<frameRectRows; row++){
276
    for(int row=0; row<frameRectRows; row++){
244
 
277
 
245
        std::vector<cv::Vec4f> edges0, edges1;
278
        std::vector<cv::Vec4i> edges0, edges1;
246
 
279
 
247
        // sorted, unique edges
280
        // sorted, unique edges
248
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
281
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
249
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
282
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
250
 
283
 
251
        // subpixel refinement
284
        // match edges
252
        // ...
285
        std::vector<cv::Vec4i> matchedEdges0, matchedEdges1;
253
 
-
 
254
        int i=0, j=0;
286
        int i=0, j=0;
255
        while(i<edges0.size() && j<edges1.size()){
287
        while(i<edges0.size() && j<edges1.size()){
256
 
288
 
257
            if(edges0[i][3] == edges1[j][3]){
289
            if(edges0[i][3] == edges1[j][3]){
258
                q0Rect.push_back(cv::Vec2f(edges0[i][0], row));
290
                matchedEdges0.push_back(edges0[i]);
259
                q1Rect.push_back(cv::Vec2f(edges1[j][0], row));
291
                matchedEdges1.push_back(edges1[j]);
260
                i += 1;
292
                i += 1;
261
                j += 1;
293
                j += 1;
262
            } else if(edges0[i][3] < edges1[j][3]){
294
            } else if(edges0[i][3] < edges1[j][3]){
263
                i += 1;
295
                i += 1;
264
            } else if(edges0[i][3] > edges1[j][3]){
296
            } else if(edges0[i][3] > edges1[j][3]){
265
                j += 1;
297
                j += 1;
266
            }
298
            }
267
        }
299
        }
268
 
300
 
-
 
301
        // crude subpixel refinement
-
 
302
        // finds the intersection of linear interpolants in the positive/negative pattern
-
 
303
        for(int i=0; i<matchedEdges0.size(); i++){
-
 
304
 
-
 
305
            int level = Nbits - leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
-
 
306
 
-
 
307
            // refine for camera 0
-
 
308
            float c0 = matchedEdges0[i][0];
-
 
309
            float c1 = c0+1;
-
 
310
 
-
 
311
            float pos0 = frames0Rect[2*level+2].at<char>(row, c0);
-
 
312
            float pos1 = frames0Rect[2*level+2].at<char>(row, c1);
-
 
313
            float neg0 = frames0Rect[2*level+3].at<char>(row, c0);
-
 
314
            float neg1 = frames0Rect[2*level+3].at<char>(row, c1);
-
 
315
 
-
 
316
            float col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
-
 
317
            q0Rect.push_back(cv::Point2f(col, row));
-
 
318
 
-
 
319
            // refine for camera 1
-
 
320
            c0 = matchedEdges1[i][0];
-
 
321
            c1 = c0+1;
-
 
322
 
-
 
323
            pos0 = frames1Rect[2*level+2].at<char>(row, c0);
-
 
324
            pos1 = frames1Rect[2*level+2].at<char>(row, c1);
-
 
325
            neg0 = frames1Rect[2*level+3].at<char>(row, c0);
-
 
326
            neg1 = frames1Rect[2*level+3].at<char>(row, c1);
-
 
327
 
-
 
328
            col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
-
 
329
            q1Rect.push_back(cv::Point2f(col, row));
-
 
330
 
-
 
331
        }
269
 
332
 
270
    }
333
    }
271
 
334
 
272
    // retrieve color information
335
    // retrieve color information
273
    int nMatches = q0Rect.size();
336
    int nMatches = q0Rect.size();