Subversion Repositories seema-scanner

Rev

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

Rev 161 Rev 163
Line 6... Line 6...
6
#define log2f(x) (log(x)/log(2.0))
6
#define log2f(x) (log(x)/log(2.0))
7
#endif
7
#endif
8
 
8
 
9
//using namespace std;
9
//using namespace std;
10
 
10
 
-
 
11
namespace ggmax {
-
 
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
 *
15
 * The operator >> is shift right. The operator ^ is exclusive or.
17
 * The operator >> is shift right. The operator ^ is exclusive or.
Line 90... Line 92...
90
static inline unsigned int twopowi(unsigned int exponent){
92
static inline unsigned int twopowi(unsigned int exponent){
91
 
93
 
92
    return 1 << exponent;
94
    return 1 << exponent;
93
}
95
}
94
 
96
 
-
 
97
bool sortingLarger(cv::Vec4i i,cv::Vec4i j){ return (i[3]<j[3]);}
-
 
98
bool sortingEqual(cv::Vec4i i,cv::Vec4i j){ return (i[3]==j[3]);}
-
 
99
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4i>& edges){
-
 
100
 
-
 
101
    int nCols = scanLine.cols;
-
 
102
    const int *data = scanLine.ptr<const int>(0);
-
 
103
 
-
 
104
    int labelLeft;
-
 
105
    int labelRight = data[0];
-
 
106
 
-
 
107
    // collect edges
-
 
108
    for(int col=1; col<nCols; col++){
-
 
109
 
-
 
110
        labelLeft = labelRight;
-
 
111
        labelRight = data[col];
-
 
112
 
-
 
113
        // labels need to be non-background, and differ in exactly one bit
-
 
114
        if(labelLeft != -1 && labelRight != -1 && (ggmax::grayToBinary(labelRight) == ggmax::grayToBinary(labelLeft)+1)){
-
 
115
            int orderingRelation = (labelLeft << Nbits) + labelRight;
-
 
116
            // store left label column
-
 
117
            edges.push_back(cv::Vec4i(col-1, labelLeft, labelRight, orderingRelation));
-
 
118
        }
-
 
119
    }
-
 
120
 
-
 
121
    // sort
-
 
122
    std::sort(edges.begin(), edges.end(), sortingLarger);
-
 
123
 
-
 
124
    // remove duplicates
-
 
125
    std::vector<cv::Vec4i>::iterator it;
-
 
126
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
-
 
127
    edges.resize(std::distance(edges.begin(),it));
-
 
128
}
-
 
129
} // End ggmax namespace
-
 
130
 
95
// Algorithm
131
// Algorithm
96
AlgorithmGrayCodeMax::AlgorithmGrayCodeMax(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
132
AlgorithmGrayCodeMax::AlgorithmGrayCodeMax(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
97
 
133
 
98
    Nbits = ceilf(log2f((float)screenCols)) - 1;
134
    Nbits = ceilf(log2f((float)screenCols)) - 1;
99
    N = 2 + Nbits*2;
135
    N = 2 + Nbits*2;
Line 112... Line 148...
112
        cv::Mat pattern(1, screenCols, CV_8UC3);
148
        cv::Mat pattern(1, screenCols, CV_8UC3);
113
        cv::Mat patternInv(1, screenCols, CV_8UC3);
149
        cv::Mat patternInv(1, screenCols, CV_8UC3);
114
 
150
 
115
        for(unsigned int j=0; j<screenCols; j++){
151
        for(unsigned int j=0; j<screenCols; j++){
116
 
152
 
117
            unsigned int jGray = binaryToGray(j);
153
            unsigned int jGray = ggmax::binaryToGray(j);
118
            // Amplitude of channels
154
            // Amplitude of channels
119
            int bit = (int)getBit(jGray, Nbits-p+1);
155
            int bit = (int)ggmax::getBit(jGray, Nbits-p+1);
120
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
156
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
121
            int invBit = bit^1;
157
            int invBit = bit^1;
122
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
158
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
123
        }
159
        }
124
        patterns.push_back(pattern);
160
        patterns.push_back(pattern);
Line 130... Line 166...
130
 
166
 
131
cv::Mat AlgorithmGrayCodeMax::getEncodingPattern(unsigned int depth){
167
cv::Mat AlgorithmGrayCodeMax::getEncodingPattern(unsigned int depth){
132
    return patterns[depth];
168
    return patterns[depth];
133
}
169
}
134
 
170
 
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
    }
-
 
159
 
-
 
160
    // sort
-
 
161
    std::sort(edges.begin(), edges.end(), sortingLarger);
-
 
162
 
-
 
163
    // remove duplicates
-
 
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
}
-
 
168
 
-
 
169
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
171
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
170
    assert(!img.empty());
172
    assert(!img.empty());
171
    assert(img.channels() == 3);
173
    assert(img.channels() == 3);
172
 
174
 
173
    int x = (int)pt.x;
175
    int x = (int)pt.x;
Line 223... Line 225...
223
    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);
224
 
226
 
225
    // gray-scale and remap (Rasmus has adjusted here)
227
    // gray-scale and remap (Rasmus has adjusted here)
226
    std::vector<cv::Mat> frames0Rect(N);
228
    std::vector<cv::Mat> frames0Rect(N);
227
    std::vector<cv::Mat> frames1Rect(N);
229
    std::vector<cv::Mat> frames1Rect(N);
228
    for(int i=0; i<N; i++){
230
    for(size_t i=0; i<N; i++){
-
 
231
        cv::Mat temp;
229
        cv::Mat temp_col;
232
        cv::Mat temp_col;
230
        cv::cvtColor(frames0[i], temp_col, CV_BayerBG2RGB);
233
        cv::cvtColor(frames0[i], temp_col, CV_BayerBG2RGB);
231
		std::vector<cv::Mat> channels(3);
234
		std::vector<cv::Mat> channels(3);
232
		cv::split(temp_col, planes);
235
        cv::split(temp_col, channels);
233
		cv::Mat temp = cv::max(planes[2], cv::max(planes[1], planes[0]))); // Calculate max of the three channels
236
        temp = cv::max(channels[2], cv::max(channels[1], channels[0])); // Calculate max of the three channels
234
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
237
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
235
 
238
 
236
        cv::cvtColor(frames1[i], temp_col, CV_BayerBG2RGB);
239
        cv::cvtColor(frames1[i], temp_col, CV_BayerBG2RGB);
-
 
240
        channels.clear();
237
		cv::split(temp_col, planes);
241
        cv::split(temp_col, channels);
238
		cv::Mat temp = cv::max(planes[2], cv::max(planes[1], planes[0])));
242
        temp = cv::max(channels[2], cv::max(channels[1], channels[0]));
239
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
243
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
240
    }
244
    }
241
 
245
 
242
 
246
 
243
//    cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
247
//    cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
Line 331... Line 335...
331
    for(int i=0; i<Nbits; i++){
335
    for(int i=0; i<Nbits; i++){
332
        cv::Mat temp, bit0, bit1;
336
        cv::Mat temp, bit0, bit1;
333
 
337
 
334
        cv::compare(frames0Rect[i*2+2], frames0Rect[i*2+3], temp, cv::CMP_GT);
338
        cv::compare(frames0Rect[i*2+2], frames0Rect[i*2+3], temp, cv::CMP_GT);
335
        temp.convertTo(bit0, CV_32S, 1.0/255.0);
339
        temp.convertTo(bit0, CV_32S, 1.0/255.0);
336
        cv::add(code0Rect, bit0*twopowi(Nbits-i-1), code0Rect, cv::noArray(), CV_32S);
340
        cv::add(code0Rect, bit0*ggmax::twopowi(Nbits-i-1), code0Rect, cv::noArray(), CV_32S);
337
 
341
 
338
        cv::compare(frames1Rect[i*2+2], frames1Rect[i*2+3], temp, cv::CMP_GT);
342
        cv::compare(frames1Rect[i*2+2], frames1Rect[i*2+3], temp, cv::CMP_GT);
339
        temp.convertTo(bit1, CV_32S, 1.0/255.0);
343
        temp.convertTo(bit1, CV_32S, 1.0/255.0);
340
        cv::add(code1Rect, bit1*twopowi(Nbits-i-1), code1Rect, cv::noArray(), CV_32S);
344
        cv::add(code1Rect, bit1*ggmax::twopowi(Nbits-i-1), code1Rect, cv::noArray(), CV_32S);
341
    }
345
    }
342
 
346
 
343
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
347
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
344
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
348
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
345
 
349
 
Line 390... Line 394...
390
 
394
 
391
        // edge data structure containing [floor(column), labelLeft, labelRight, orderingRelation]
395
        // edge data structure containing [floor(column), labelLeft, labelRight, orderingRelation]
392
        std::vector<cv::Vec4i> edges0, edges1;
396
        std::vector<cv::Vec4i> edges0, edges1;
393
 
397
 
394
        // sorted, unique edges
398
        // sorted, unique edges
395
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
399
        ggmax::getEdgeLabels(code0Rect.row(row), Nbits, edges0);
396
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
400
        ggmax::getEdgeLabels(code1Rect.row(row), Nbits, edges1);
397
 
401
 
398
        // match edges
402
        // match edges
399
        std::vector<cv::Vec4i> matchedEdges0, matchedEdges1;
403
        std::vector<cv::Vec4i> matchedEdges0, matchedEdges1;
400
        int i=0, j=0;
404
        int i=0, j=0;
401
        while(i<edges0.size() && j<edges1.size()){
405
        while(i<edges0.size() && j<edges1.size()){
Line 414... Line 418...
414
 
418
 
415
        // crude subpixel refinement
419
        // crude subpixel refinement
416
        // finds the intersection of linear interpolants in the positive/negative pattern
420
        // finds the intersection of linear interpolants in the positive/negative pattern
417
        for(int i=0; i<matchedEdges0.size(); i++){
421
        for(int i=0; i<matchedEdges0.size(); i++){
418
 
422
 
419
            int level = Nbits - leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
423
            int level = Nbits - ggmax::leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
420
 
424
 
421
            // refine for camera 0
425
            // refine for camera 0
422
            float c0 = matchedEdges0[i][0];
426
            float c0 = matchedEdges0[i][0];
423
            float c1 = c0+1;
427
            float c1 = c0+1;
424
 
428