Subversion Repositories seema-scanner

Rev

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

Rev 134 Rev 167
Line 39... Line 39...
39
static bool getBit(int decimal, int N){
39
static bool getBit(int decimal, int N){
40
 
40
 
41
    return decimal & 1 << (N-1);
41
    return decimal & 1 << (N-1);
42
}
42
}
43
 
43
 
44
/*
44
///*
45
 * Return the number of bits set in an integer
45
// * Return the number of bits set in an integer
46
 */
46
// */
47
static int countBits(int n) {
47
//static int countBits(int n) {
48
  unsigned int c; // c accumulates the total bits set in v
48
//  unsigned int c; // c accumulates the total bits set in v
49
  for (c = 0; n>0; c++)
49
//  for (c = 0; n>0; c++)
50
    n &= n - 1; // clear the least significant bit set
50
//    n &= n - 1; // clear the least significant bit set
51
  return c;
51
//  return c;
52
}
52
//}
53
 
53
 
54
/*
54
///*
55
 * Return the position of the least significant bit that is set
55
// * Return the position of the least significant bit that is set
56
 */
56
// */
57
static int leastSignificantBitSet(int x){
57
//static int leastSignificantBitSet(int x){
58
  if(x == 0)
58
//  if(x == 0)
59
      return 0;
59
//      return 0;
60
 
60
 
61
  int val = 1;
61
//  int val = 1;
62
  while(x>>=1)
62
//  while(x>>=1)
63
      val++;
63
//      val++;
64
 
64
 
65
  return val;
65
//  return val;
66
}
66
//}
67
 
67
 
68
static inline unsigned int powi(int num, unsigned int exponent){
68
static inline unsigned int powi(int num, unsigned int exponent){
69
 
69
 
70
    if(exponent == 0)
70
    if(exponent == 0)
71
        return 1;
71
        return 1;
Line 133... Line 133...
133
 
133
 
134
cv::Mat AlgorithmLineShift::getEncodingPattern(unsigned int depth){
134
cv::Mat AlgorithmLineShift::getEncodingPattern(unsigned int depth){
135
    return patterns[depth];
135
    return patterns[depth];
136
}
136
}
137
 
137
 
138
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
138
//static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
139
    assert(!img.empty());
139
//    assert(!img.empty());
140
    assert(img.channels() == 3);
140
//    assert(img.channels() == 3);
141
 
141
 
142
    int x = (int)pt.x;
142
//    int x = (int)pt.x;
143
    int y = (int)pt.y;
143
//    int y = (int)pt.y;
144
 
144
 
145
    int x0 = cv::borderInterpolate(x,   img.cols, cv::BORDER_REFLECT_101);
145
//    int x0 = cv::borderInterpolate(x,   img.cols, cv::BORDER_REFLECT_101);
146
    int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
146
//    int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
147
    int y0 = cv::borderInterpolate(y,   img.rows, cv::BORDER_REFLECT_101);
147
//    int y0 = cv::borderInterpolate(y,   img.rows, cv::BORDER_REFLECT_101);
148
    int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);
148
//    int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);
149
 
149
 
150
    float a = pt.x - (float)x;
150
//    float a = pt.x - (float)x;
151
    float c = pt.y - (float)y;
151
//    float c = pt.y - (float)y;
152
 
152
 
153
    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)
153
//    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)
154
                           + (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
154
//                           + (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
155
    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)
155
//    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)
156
                           + (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
156
//                           + (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
157
    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)
157
//    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)
158
                           + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
158
//                           + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
159
 
159
 
160
    return cv::Vec3b(b, g, r);
160
//    return cv::Vec3b(b, g, r);
161
}
161
//}
162
 
162
 
163
void getlineCenters(const cv::Mat& linesScanLine, const cv::Mat& codeScanLine, std::vector<cv::Vec2f>& lineCenters){
163
void getlineCenters(const cv::Mat& linesScanLine, const cv::Mat& codeScanLine, std::vector<cv::Vec2f>& lineCenters){
164
 
164
 
165
    int nCols = linesScanLine.cols;
165
    int nCols = linesScanLine.cols;
166
 
166
 
Line 225... Line 225...
225
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
225
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
226
 
226
 
227
    // gray-scale and remap
227
    // gray-scale and remap
228
    std::vector<cv::Mat> frames0Rect(N);
228
    std::vector<cv::Mat> frames0Rect(N);
229
    std::vector<cv::Mat> frames1Rect(N);
229
    std::vector<cv::Mat> frames1Rect(N);
230
    for(int i=0; i<N; i++){
230
    for(unsigned int i=0; i<N; i++){
231
        cv::Mat temp;
231
        cv::Mat temp;
232
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
232
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
233
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_CUBIC);
233
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_CUBIC);
234
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
234
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
235
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_CUBIC);
235
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_CUBIC);
Line 292... Line 292...
292
    // decode patterns
292
    // decode patterns
293
    cv::Mat code0Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
293
    cv::Mat code0Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
294
    cv::Mat code1Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
294
    cv::Mat code1Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
295
 
295
 
296
    // into gray code
296
    // into gray code
297
    for(int i=0; i<nGrayBits; i++){
297
    for(unsigned int i=0; i<nGrayBits; i++){
298
        cv::Mat temp, bit0, bit1;
298
        cv::Mat temp, bit0, bit1;
299
 
299
 
300
        cv::compare(frames0GrayCode[i*2], frames0GrayCode[i*2+1], temp, cv::CMP_GT);
300
        cv::compare(frames0GrayCode[i*2], frames0GrayCode[i*2+1], temp, cv::CMP_GT);
301
        temp.convertTo(bit0, CV_32S, 1.0/255.0);
301
        temp.convertTo(bit0, CV_32S, 1.0/255.0);
302
        cv::add(code0Gray, bit0*twopowi(nGrayBits-i-1), code0Gray, cv::noArray(), CV_32S);
302
        cv::add(code0Gray, bit0*twopowi(nGrayBits-i-1), code0Gray, cv::noArray(), CV_32S);
Line 331... Line 331...
331
//cvtools::writeMat(code0Binary, "code0Binary.mat", "code0Binary");
331
//cvtools::writeMat(code0Binary, "code0Binary.mat", "code0Binary");
332
//cvtools::writeMat(code1Binary, "code1Binary.mat", "code1Binary");
332
//cvtools::writeMat(code1Binary, "code1Binary.mat", "code1Binary");
333
 
333
 
334
    // matching
334
    // matching
335
    std::vector<cv::Vec2f> q0Rect, q1Rect;
335
    std::vector<cv::Vec2f> q0Rect, q1Rect;
336
    for(int s=0; s<nLineShifts; s++){
336
    for(unsigned int s=0; s<nLineShifts; s++){
337
 
337
 
338
        cv::Mat lines0 = frames0LineShift[s];
338
        cv::Mat lines0 = frames0LineShift[s];
339
        cv::Mat lines1 = frames1LineShift[s];
339
        cv::Mat lines1 = frames1LineShift[s];
340
 
340
 
341
        for(int row=0; row<frameRectRows; row++){
341
        for(int row=0; row<frameRectRows; row++){
Line 356... Line 356...
356
//                cvtools::writeMat(code0Binary.row(row), "code0Binary.mat", "code0Binary");
356
//                cvtools::writeMat(code0Binary.row(row), "code0Binary.mat", "code0Binary");
357
//                cvtools::writeMat(code1Binary.row(row), "code1Binary.mat", "code1Binary");
357
//                cvtools::writeMat(code1Binary.row(row), "code1Binary.mat", "code1Binary");
358
//             }
358
//             }
359
 
359
 
360
            // match and store
360
            // match and store
361
            int i=0, j=0;
361
            unsigned int i=0, j=0;
362
            while(i<lineCenters0.size() && j<lineCenters1.size()){
362
            while(i<lineCenters0.size() && j<lineCenters1.size()){
363
 
363
 
364
                if(lineCenters0[i][1] == lineCenters1[j][1]){
364
                if(lineCenters0[i][1] == lineCenters1[j][1]){
365
                    q0Rect.push_back(cv::Point2f(lineCenters0[i][0], row));
365
                    q0Rect.push_back(cv::Point2f(lineCenters0[i][0], row));
366
                    q1Rect.push_back(cv::Point2f(lineCenters1[j][0], row));
366
                    q1Rect.push_back(cv::Point2f(lineCenters1[j][0], row));