Subversion Repositories seema-scanner

Rev

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

Rev 39 Rev 41
Line 1... Line 1...
1
#include "CodecGrayCode.h"
1
#include "AlgorithmGrayCode.h"
2
#include <cmath>
2
#include <cmath>
3
 
3
 
4
#ifndef log2f
4
#ifndef log2f
5
#define log2f(x) (log(x)/log(2.0))
5
#define log2f(x) (log(x)/log(2.0))
6
#endif
6
#endif
7
 
7
 
8
using namespace std;
8
//using namespace std;
9
 
9
 
10
/*
10
/*
11
 * The purpose of this function is to convert an unsigned
11
 * The purpose of this function is to convert an unsigned
12
 * binary number to reflected binary Gray code.
12
 * binary number to reflected binary Gray code.
13
 *
13
 *
Line 62... Line 62...
62
 
62
 
63
    return res;
63
    return res;
64
}
64
}
65
 
65
 
66
// Algorithm
66
// Algorithm
67
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodecDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
67
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodingDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
68
 
68
 
69
    // Number of horizontal encoding patterns
69
    // Number of horizontal encoding patterns
70
    Nhorz = ceilf(log2f((float)screenCols));;
70
    Nhorz = ceilf(log2f((float)screenCols))*2;
71
 
71
 
72
    // Number of vertical encoding patterns
72
    // Number of vertical encoding patterns
73
    Nvert = ceilf(log2f((float)screenRows));;
73
    Nvert = ceilf(log2f((float)screenRows))*2;
-
 
74
 
-
 
75
    // on/off patterns
-
 
76
    this->N = 2;
74
 
77
 
75
    // Set total pattern number
78
    // Set total pattern number
76
    if(dir & CodecDirHorizontal)
79
    if(dir & CodingDirHorizontal)
77
        this->N += Nhorz;
80
        this->N += Nhorz;
78
 
81
 
79
    if(dir & CodecDirVertical)
82
    if(dir & CodingDirVertical)
80
        this->N += Nvert;
83
        this->N += Nvert;
81
 
84
 
-
 
85
    // all on pattern
-
 
86
    cv::Mat pattern(1, 1, CV_8UC3);
-
 
87
    pattern.setTo(cv::Vec3b(255.0,255.0,255.0));
-
 
88
    patterns.push_back(pattern);
-
 
89
 
-
 
90
    // all off pattern
-
 
91
    pattern.setTo(cv::Vec3b(0.0,0.0,0.0));
-
 
92
    patterns.push_back(pattern);
-
 
93
 
82
    if(dir & CodecDirHorizontal){
94
    if(dir & CodingDirHorizontal){
83
        // Precompute horizontally encoding patterns
95
        // Precompute horizontally encoding patterns
84
        for(unsigned int p=0; p<Nhorz; p++){
96
        for(unsigned int p=0; p<Nhorz; p++){
85
            cv::Mat patternP(1, screenCols, CV_8UC3);
97
            cv::Mat patternP(1, screenCols, CV_8UC3);
86
            // Loop through columns in first row
98
            // Loop through columns in first row
87
            for(unsigned int j=0; j<screenCols; j++){
99
            for(unsigned int j=0; j<screenCols; j++){
Line 91... Line 103...
91
                patternP.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
103
                patternP.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
92
            }
104
            }
93
            patterns.push_back(patternP);
105
            patterns.push_back(patternP);
94
        }
106
        }
95
    }
107
    }
96
    if(dir & CodecDirVertical){
108
    if(dir & CodingDirVertical){
97
        // Precompute vertical encoding patterns
109
        // Precompute vertical encoding patterns
98
        for(unsigned int p=0; p<Nvert; p++){
110
        for(unsigned int p=0; p<Nvert; p++){
99
            cv::Mat patternP(screenRows, 1, CV_8UC3);
111
            cv::Mat patternP(screenRows, 1, CV_8UC3);
100
 
112
 
101
            // Loop through rows in first column
113
            // Loop through rows in first column
Line 114... Line 126...
114
 
126
 
115
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
127
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
116
    return patterns[depth];
128
    return patterns[depth];
117
}
129
}
118
 
130
 
119
// Algorithm
-
 
120
AlgorithmGrayCode::AlgorithmGrayCode(CodecDir _dir, int _screenResX, int _screenResY) : Algorithm(_dir, _screenResX, _screenResY){
-
 
121
 
131
 
122
    // Number of horizontal encoding patterns
132
bool sortingLarger(cv::Vec4f i,cv::Vec4f j){ return (i[3]<j[3]);}
123
    Nhorz = ceilf(log2f((float)screenCols));;
133
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4f>& edges){
124
 
134
 
-
 
135
    int nCols = scanLine.cols;
-
 
136
 
125
    // Number of vertical encoding patterns
137
    for(int col=1; col<nCols; nCols++){
-
 
138
 
126
    Nvert = ceilf(log2f((float)screenRows));;
139
        int labelLeft = scanLine.at<int>(0,col-1);
-
 
140
        int labelRight = scanLine.at<int>(0,col);
-
 
141
 
-
 
142
        if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
-
 
143
 
-
 
144
            int orderingRelation = (2 << Nbits)*labelLeft + labelRight;
-
 
145
 
-
 
146
            edges.push_back(cv::Vec4f(col, labelLeft, labelRight, orderingRelation));
-
 
147
 
-
 
148
        }
-
 
149
    }
-
 
150
 
-
 
151
    std::sort(edges.begin(), edges.end(), sortingLarger);
127
}
152
}
128
 
153
 
129
void AlgorithmGrayCode::getCorrespondences(const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point2f>& q0, std::vector<cv::Point2f>& q1, std::vector<cv::Point3f>& color){
154
void AlgorithmGrayCode::getCorrespondences(SMCalibrationParameters calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point2f>& q0, std::vector<cv::Point2f>& q1, std::vector<cv::Point3f>& color){
-
 
155
 
-
 
156
    assert(frames0.size() == N);
-
 
157
    assert(frames1.size() == N);
-
 
158
 
-
 
159
    // occlusion maps
-
 
160
    cv::Mat occlusion0 = (frames0[0] - frames0[1]) > 0;
-
 
161
    cv::Mat occlusion1 = (frames1[0] - frames1[1]) > 0;
-
 
162
 
-
 
163
    // decoded patterns
-
 
164
    cv::Mat code0, code1;
-
 
165
 
-
 
166
    int Nbits = (N-2)/2;
-
 
167
    for(int i=0; i<Nbits; i++){
-
 
168
        cv::Mat bit0 = (frames0[i*2+2] - frames0[i*2+3]) > 0;
-
 
169
        code0 += bit0*pow(2, i);
-
 
170
        cv::Mat bit1 = (frames1[i*2+2] - frames1[i*2+3]) > 0;
-
 
171
        code1 += bit1*pow(2, i);
-
 
172
    }
-
 
173
 
-
 
174
    // rectifying homographies
-
 
175
    cv::Size frameSize(frames0[0].cols, frames0[0].rows);
-
 
176
    cv::Mat R0, R1, P0, P1, Q;
-
 
177
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, calibration.R1, calibration.T1, R0, R1, P0, P1, Q);
-
 
178
 
-
 
179
    // interpolation maps
-
 
180
    cv::Mat map0X, map0Y, map1X, map1Y;
-
 
181
    cv::initUndistortRectifyMap(P0, calibration.k0, R0, cv::Mat(), frameSize, CV_32FC1, map0X, map0Y);
-
 
182
    cv::initUndistortRectifyMap(P1, calibration.k1, R1, cv::Mat(), frameSize, CV_32FC1, map1X, map1Y);
-
 
183
 
-
 
184
    // remap
-
 
185
    cv::remap(occlusion0, occlusion0, map0X, map0Y, cv::INTER_CUBIC);
-
 
186
    cv::remap(occlusion1, occlusion1, map1X, map1Y, cv::INTER_CUBIC);
-
 
187
    cv::remap(code0, code0, map1X, map1Y, cv::INTER_CUBIC);
-
 
188
    cv::remap(code1, code1, map1X, map1Y, cv::INTER_CUBIC);
-
 
189
 
-
 
190
    int nRows = occlusion0.rows;
-
 
191
    int nCols = occlusion0.cols;
-
 
192
 
-
 
193
    // matching
-
 
194
    for(int row=0; row<nRows; row++){
-
 
195
 
-
 
196
        std::vector<cv::Vec4f> edges0, edges1;
-
 
197
 
-
 
198
        getEdgeLabels(code0.row(row), Nbits, edges0);
-
 
199
        getEdgeLabels(code1.row(row), Nbits, edges1);
-
 
200
 
-
 
201
        // REMOVE DOUBLE ENTRIES!
-
 
202
 
-
 
203
        int i=0, j=0;
-
 
204
        while(i<edges0.size() && j<edges1.size()){
-
 
205
 
-
 
206
            if(edges0[i][3] == edges1[j][3]){
-
 
207
                q0.push_back(cv::Point2f(row, edges0[i][0]));
-
 
208
                q1.push_back(cv::Point2f(row, edges1[j][0]));
-
 
209
                i += 1;
-
 
210
                j += 1;
-
 
211
            } else if(edges0[i][3] < edges1[i][3]){
-
 
212
                i += 1;
-
 
213
            } else if(edges0[i][3] > edges1[i][3]){
-
 
214
                j += 1;
-
 
215
            }
-
 
216
        }
-
 
217
 
130
 
218
 
-
 
219
    }
131
 
220
 
-
 
221
    // retrieve color information
-
 
222
    int nMatches = q0.size();
-
 
223
    color.resize(nMatches);
-
 
224
    for(int i=0; i<nMatches; i++){
132
 
225
 
-
 
226
        cv::Vec3f color0 = frames0[0].at<cv::Vec3f>(q0[i].y, q0[i].x);
-
 
227
        cv::Vec3f color1 = frames1[0].at<cv::Vec3f>(q1[i].y, q1[i].x);
133
 
228
 
-
 
229
        color[i] = 0.5*(color0 + color1);
-
 
230
    }
134
 
231
 
135
}
232
}