Subversion Repositories seema-scanner

Rev

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

Rev 41 Rev 42
Line 1... Line 1...
1
#include "AlgorithmGrayCode.h"
1
#include "AlgorithmGrayCode.h"
2
#include <cmath>
2
#include <cmath>
-
 
3
#include "cvtools.h"
3
 
4
 
4
#ifndef log2f
5
#ifndef log2f
5
#define log2f(x) (log(x)/log(2.0))
6
#define log2f(x) (log(x)/log(2.0))
6
#endif
7
#endif
7
 
8
 
Line 64... Line 65...
64
}
65
}
65
 
66
 
66
// Algorithm
67
// Algorithm
67
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodingDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
68
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodingDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
68
 
69
 
69
    // Number of horizontal encoding patterns
-
 
70
    Nhorz = ceilf(log2f((float)screenCols))*2;
-
 
71
 
-
 
72
    // Number of vertical encoding patterns
-
 
73
    Nvert = ceilf(log2f((float)screenRows))*2;
-
 
74
 
-
 
75
    // on/off patterns
70
    // on/off patterns
76
    this->N = 2;
-
 
77
 
-
 
78
    // Set total pattern number
71
    Nbits = ceilf(log2f((float)screenCols));
79
    if(dir & CodingDirHorizontal)
-
 
80
        this->N += Nhorz;
-
 
81
 
-
 
82
    if(dir & CodingDirVertical)
-
 
83
        this->N += Nvert;
72
    N = 2 + Nbits*2;
84
 
73
 
85
    // all on pattern
74
    // all on pattern
86
    cv::Mat pattern(1, 1, CV_8UC3);
75
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
87
    pattern.setTo(cv::Vec3b(255.0,255.0,255.0));
-
 
88
    patterns.push_back(pattern);
76
    patterns.push_back(allOn);
89
 
77
 
90
    // all off pattern
78
    // all off pattern
91
    pattern.setTo(cv::Vec3b(0.0,0.0,0.0));
79
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
92
    patterns.push_back(pattern);
80
    patterns.push_back(allOff);
93
 
81
 
94
    if(dir & CodingDirHorizontal){
-
 
-
 
82
 
95
        // Precompute horizontally encoding patterns
83
    // horizontally encoding patterns
96
        for(unsigned int p=0; p<Nhorz; p++){
84
    for(unsigned int p=0; p<Nbits; p++){
97
            cv::Mat patternP(1, screenCols, CV_8UC3);
85
        cv::Mat pattern(1, screenCols, CV_8UC3);
98
            // Loop through columns in first row
-
 
99
            for(unsigned int j=0; j<screenCols; j++){
-
 
100
                unsigned int jGray = binaryToGray(j);
-
 
101
                // Amplitude of channels
-
 
102
                float amp = get_bit(jGray, Nhorz-p);
-
 
103
                patternP.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
-
 
104
            }
-
 
105
            patterns.push_back(patternP);
-
 
106
        }
-
 
107
    }
-
 
108
    if(dir & CodingDirVertical){
-
 
109
        // Precompute vertical encoding patterns
-
 
110
        for(unsigned int p=0; p<Nvert; p++){
-
 
111
            cv::Mat patternP(screenRows, 1, CV_8UC3);
86
        cv::Mat patternInv(1, screenCols, CV_8UC3);
112
 
87
 
113
            // Loop through rows in first column
-
 
114
            for(unsigned int i=0; i<screenRows; i++){
88
        for(unsigned int j=0; j<screenCols; j++){
115
 
89
 
116
                unsigned int iGray = binaryToGray(i);
90
            unsigned int jGray = binaryToGray(j);
117
 
-
 
118
                // Amplitude of channels
91
            // Amplitude of channels
119
                float amp = get_bit(iGray, Nvert-p); // Nvert-p-1?
92
            int bit = get_bit(jGray, Nbits-p);
120
                patternP.at<cv::Vec3b>(i,0) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
93
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
121
            }
94
            int invBit = bit^1;
122
            patterns.push_back(patternP);
95
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
123
        }
96
        }
-
 
97
        patterns.push_back(pattern);
-
 
98
        patterns.push_back(patternInv);
124
    }
99
    }
-
 
100
 
-
 
101
 
125
}
102
}
126
 
103
 
127
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
104
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
128
    return patterns[depth];
105
    return patterns[depth];
129
}
106
}
130
 
107
 
131
 
108
 
132
bool sortingLarger(cv::Vec4f i,cv::Vec4f j){ return (i[3]<j[3]);}
109
bool sortingLarger(cv::Vec4f i,cv::Vec4f j){ return (i[3]<j[3]);}
-
 
110
bool sortingEqual(cv::Vec4f i,cv::Vec4f j){ return (i[3]==j[3]);}
133
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4f>& edges){
111
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4f>& edges){
134
 
112
 
135
    int nCols = scanLine.cols;
113
    int nCols = scanLine.cols;
-
 
114
    const short *data = scanLine.ptr<const short>(0);
136
 
115
 
-
 
116
    short labelLeft;
137
    for(int col=1; col<nCols; nCols++){
117
    short labelRight = data[0];
138
 
118
 
-
 
119
    for(int col=1; col<nCols; col++){
-
 
120
 
139
        int labelLeft = scanLine.at<int>(0,col-1);
121
        labelLeft = labelRight;
140
        int labelRight = scanLine.at<int>(0,col);
122
        labelRight = data[col];
141
 
123
 
142
        if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
124
        if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
143
 
125
 
144
            int orderingRelation = (2 << Nbits)*labelLeft + labelRight;
126
            int orderingRelation = (2 << Nbits)*labelLeft + labelRight;
145
 
127
 
146
            edges.push_back(cv::Vec4f(col, labelLeft, labelRight, orderingRelation));
128
            edges.push_back(cv::Vec4f(col, labelLeft, labelRight, orderingRelation));
147
 
129
 
148
        }
130
        }
149
    }
131
    }
150
 
132
 
-
 
133
    // sort
151
    std::sort(edges.begin(), edges.end(), sortingLarger);
134
    std::sort(edges.begin(), edges.end(), sortingLarger);
-
 
135
 
-
 
136
    // remove duplicates
-
 
137
    std::vector<cv::Vec4f>::iterator it;
-
 
138
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
-
 
139
    edges.resize(std::distance(edges.begin(),it));
152
}
140
}
153
 
141
 
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){
142
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){
155
 
143
 
156
    assert(frames0.size() == N);
144
    assert(frames0.size() == N);
157
    assert(frames1.size() == N);
145
    assert(frames1.size() == N);
158
 
146
 
-
 
147
    int frameRows = frames0[0].rows;
-
 
148
    int frameCols = frames0[0].cols;
-
 
149
 
-
 
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
 
159
    // occlusion maps
158
    // occlusion maps
-
 
159
    cv::Mat occlusion0, occlusion1;
160
    cv::Mat occlusion0 = (frames0[0] - frames0[1]) > 0;
160
    cv::subtract(frames0Gray[0], frames0Gray[1], occlusion0);
-
 
161
    occlusion0 = occlusion0 > 50;
161
    cv::Mat occlusion1 = (frames1[0] - frames1[1]) > 0;
162
    cv::subtract(frames1Gray[0], frames1Gray[1], occlusion1);
-
 
163
    occlusion1 = occlusion1 > 50;
162
 
164
 
163
    // decoded patterns
165
//    cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
164
    cv::Mat code0, code1;
166
//    cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
165
 
167
 
166
    int Nbits = (N-2)/2;
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");
167
    for(int i=0; i<Nbits; i++){
172
    for(int i=0; i<Nbits; i++){
-
 
173
        cv::Mat bit0;
168
        cv::Mat bit0 = (frames0[i*2+2] - frames0[i*2+3]) > 0;
174
        cv::subtract(frames0Gray[i*2+2], frames0Gray[i*2+3], bit0);
169
        code0 += bit0*pow(2, i);
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;
170
        cv::Mat bit1 = (frames1[i*2+2] - frames1[i*2+3]) > 0;
180
        cv::subtract(frames1Gray[i*2+2], frames1Gray[i*2+3], bit1);
171
        code1 += bit1*pow(2, i);
181
        bit1 = bit1 > 50;
-
 
182
        cv::add(code1, bit1/255*powi(2,i), code1, occlusion1, CV_16S);
172
    }
183
    }
173
 
184
 
-
 
185
    cvtools::writeMat(code0, "code0.mat", "code0");
-
 
186
    cvtools::writeMat(code1, "code1.mat", "code1");
-
 
187
 
174
    // rectifying homographies
188
    // rectifying homographies (rotation+projections)
175
    cv::Size frameSize(frames0[0].cols, frames0[0].rows);
189
    cv::Size frameSize(frameCols, frameRows);
-
 
190
    cv::Mat R, T;
-
 
191
    // stereoRectify segfaults unless R is double precision
-
 
192
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
-
 
193
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
176
    cv::Mat R0, R1, P0, P1, Q;
194
    cv::Mat R0, R1, P0, P1, QRect;
177
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, calibration.R1, calibration.T1, R0, R1, P0, P1, Q);
195
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
178
 
196
 
179
    // interpolation maps
197
    // interpolation maps
180
    cv::Mat map0X, map0Y, map1X, map1Y;
198
    cv::Mat map0X, map0Y, map1X, map1Y;
181
    cv::initUndistortRectifyMap(P0, calibration.k0, R0, cv::Mat(), frameSize, CV_32FC1, map0X, map0Y);
199
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
182
    cv::initUndistortRectifyMap(P1, calibration.k1, R1, cv::Mat(), frameSize, CV_32FC1, map1X, map1Y);
200
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
183
 
201
 
184
    // remap
202
    // remap
-
 
203
    cv::Mat code0Rect, code1Rect, color0Rect, color1Rect;
185
    cv::remap(occlusion0, occlusion0, map0X, map0Y, cv::INTER_CUBIC);
204
    cv::remap(code0, code0Rect, map0X, map0Y, cv::INTER_NEAREST);
186
    cv::remap(occlusion1, occlusion1, map1X, map1Y, cv::INTER_CUBIC);
205
    cv::remap(code1, code1Rect, map1X, map1Y, cv::INTER_NEAREST);
187
    cv::remap(code0, code0, map1X, map1Y, cv::INTER_CUBIC);
206
    cv::remap(frames0[0], color0Rect, map0X, map0Y, cv::INTER_CUBIC);
188
    cv::remap(code1, code1, map1X, map1Y, cv::INTER_CUBIC);
207
    cv::remap(frames1[0], color1Rect, map1X, map1Y, cv::INTER_CUBIC);
-
 
208
 
-
 
209
cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
-
 
210
cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
189
 
211
 
-
 
212
//cvtools::writeMat(color0Rect, "color0.mat", "color0");
-
 
213
//cvtools::writeMat(color1Rect, "color1.mat", "color1");
-
 
214
 
190
    int nRows = occlusion0.rows;
215
    int nRows = code0Rect.rows;
191
    int nCols = occlusion0.cols;
216
    int nCols = code0Rect.cols;
192
 
217
 
193
    // matching
218
    // matching
-
 
219
    std::vector<cv::Vec2f> q0Rect, q1Rect;
194
    for(int row=0; row<nRows; row++){
220
    for(int row=0; row<nRows; row++){
195
 
221
 
196
        std::vector<cv::Vec4f> edges0, edges1;
222
        std::vector<cv::Vec4f> edges0, edges1;
197
 
223
 
198
        getEdgeLabels(code0.row(row), Nbits, edges0);
224
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
199
        getEdgeLabels(code1.row(row), Nbits, edges1);
225
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
200
 
-
 
201
        // REMOVE DOUBLE ENTRIES!
-
 
202
 
226
 
203
        int i=0, j=0;
227
        int i=0, j=0;
204
        while(i<edges0.size() && j<edges1.size()){
228
        while(i<edges0.size() && j<edges1.size()){
205
 
229
 
206
            if(edges0[i][3] == edges1[j][3]){
230
            if(edges0[i][3] == edges1[j][3]){
207
                q0.push_back(cv::Point2f(row, edges0[i][0]));
231
                q0Rect.push_back(cv::Vec2f(edges0[i][0], row));
208
                q1.push_back(cv::Point2f(row, edges1[j][0]));
232
                q1Rect.push_back(cv::Vec2f(edges1[j][0], row));
209
                i += 1;
233
                i += 1;
210
                j += 1;
234
                j += 1;
211
            } else if(edges0[i][3] < edges1[i][3]){
235
            } else if(edges0[i][3] < edges1[j][3]){
212
                i += 1;
236
                i += 1;
213
            } else if(edges0[i][3] > edges1[i][3]){
237
            } else if(edges0[i][3] > edges1[j][3]){
214
                j += 1;
238
                j += 1;
215
            }
239
            }
216
        }
240
        }
217
 
241
 
218
 
242
 
219
    }
243
    }
220
 
244
 
221
    // retrieve color information
245
    // retrieve color information
222
    int nMatches = q0.size();
246
    int nMatches = q0Rect.size();
223
    color.resize(nMatches);
247
    color.resize(nMatches);
224
    for(int i=0; i<nMatches; i++){
248
    for(int i=0; i<nMatches; i++){
225
 
249
 
226
        cv::Vec3f color0 = frames0[0].at<cv::Vec3f>(q0[i].y, q0[i].x);
250
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
227
        cv::Vec3f color1 = frames1[0].at<cv::Vec3f>(q1[i].y, q1[i].x);
251
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
228
 
252
 
229
        color[i] = 0.5*(color0 + color1);
253
        color[i] = 0.5*(c0 + c1);
230
    }
254
    }
231
 
255
 
-
 
256
    // triangulate points
-
 
257
    cv::Mat QMatHomogenous, QMat;
-
 
258
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
-
 
259
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
-
 
260
    cvtools::matToPoints3f(QMat, Q);
232
}
261
}