Subversion Repositories seema-scanner

Rev

Rev 41 | Rev 43 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
41 jakw 1
#include "AlgorithmGrayCode.h"
4 jakw 2
#include <cmath>
42 jakw 3
#include "cvtools.h"
4 jakw 4
 
5
#ifndef log2f
6
#define log2f(x) (log(x)/log(2.0))
7
#endif
8
 
41 jakw 9
//using namespace std;
4 jakw 10
 
11
/*
12
 * The purpose of this function is to convert an unsigned
13
 * binary number to reflected binary Gray code.
14
 *
15
 * The operator >> is shift right. The operator ^ is exclusive or.
16
 * Source: http://en.wikipedia.org/wiki/Gray_code
17
 */
18
static unsigned int binaryToGray(unsigned int num) {
19
    return (num >> 1) ^ num;
20
}
21
 
22
/*
23
 * From Wikipedia: http://en.wikipedia.org/wiki/Gray_code
24
 * The purpose of this function is to convert a reflected binary
25
 * Gray code number to a binary number.
26
 */
27
static unsigned grayToBinary(unsigned num, unsigned numBits)
28
{
29
    for (unsigned shift = 1; shift < numBits; shift <<= 1){
30
        num ^= num >> shift;
31
    }
32
    return num;
33
}
34
 
35
/*
36
 * Function takes the decimal number
37
 * Function takes the Nth bit (1 to 31)
38
 * Return the value of Nth bit from decimal
39
 * Source: http://icfun.blogspot.com/2009/04/get-n-th-bit-value-of-any-integer.html
40
 */
41
static int get_bit(int decimal, int N){
42
 
43
    // Shifting the 1 for N-1 bits
44
    int constant = 1 << (N-1);
45
 
46
    // If the bit is set, return 1
47
    if( decimal & constant ){
48
        return 1;
49
    }
50
 
51
    // If the bit is not set, return 0
52
    return 0;
53
}
54
 
55
static inline int powi(int num, unsigned int exponent){
56
    // NOT EQUIVALENT TO pow()
57
    if(exponent == 0)
58
        return 1;
59
 
60
    float res = num;
61
    for(unsigned int i=0; i<exponent-1; i++)
62
        res *= num;
63
 
64
    return res;
65
}
66
 
36 jakw 67
// Algorithm
41 jakw 68
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodingDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
4 jakw 69
 
41 jakw 70
    // on/off patterns
42 jakw 71
    Nbits = ceilf(log2f((float)screenCols));
72
    N = 2 + Nbits*2;
41 jakw 73
 
74
    // all on pattern
42 jakw 75
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
76
    patterns.push_back(allOn);
41 jakw 77
 
78
    // all off pattern
42 jakw 79
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
80
    patterns.push_back(allOff);
41 jakw 81
 
4 jakw 82
 
42 jakw 83
    // horizontally encoding patterns
84
    for(unsigned int p=0; p<Nbits; p++){
85
        cv::Mat pattern(1, screenCols, CV_8UC3);
86
        cv::Mat patternInv(1, screenCols, CV_8UC3);
4 jakw 87
 
42 jakw 88
        for(unsigned int j=0; j<screenCols; j++){
4 jakw 89
 
42 jakw 90
            unsigned int jGray = binaryToGray(j);
91
            // Amplitude of channels
92
            int bit = get_bit(jGray, Nbits-p);
93
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
94
            int invBit = bit^1;
95
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
4 jakw 96
        }
42 jakw 97
        patterns.push_back(pattern);
98
        patterns.push_back(patternInv);
4 jakw 99
    }
42 jakw 100
 
101
 
4 jakw 102
}
103
 
36 jakw 104
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
4 jakw 105
    return patterns[depth];
106
}
107
 
108
 
41 jakw 109
bool sortingLarger(cv::Vec4f i,cv::Vec4f j){ return (i[3]<j[3]);}
42 jakw 110
bool sortingEqual(cv::Vec4f i,cv::Vec4f j){ return (i[3]==j[3]);}
41 jakw 111
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4f>& edges){
4 jakw 112
 
41 jakw 113
    int nCols = scanLine.cols;
42 jakw 114
    const short *data = scanLine.ptr<const short>(0);
41 jakw 115
 
42 jakw 116
    short labelLeft;
117
    short labelRight = data[0];
41 jakw 118
 
42 jakw 119
    for(int col=1; col<nCols; col++){
41 jakw 120
 
42 jakw 121
        labelLeft = labelRight;
122
        labelRight = data[col];
123
 
41 jakw 124
        if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
125
 
126
            int orderingRelation = (2 << Nbits)*labelLeft + labelRight;
127
 
128
            edges.push_back(cv::Vec4f(col, labelLeft, labelRight, orderingRelation));
129
 
130
        }
131
    }
132
 
42 jakw 133
    // sort
41 jakw 134
    std::sort(edges.begin(), edges.end(), sortingLarger);
42 jakw 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));
4 jakw 140
}
141
 
42 jakw 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){
4 jakw 143
 
41 jakw 144
    assert(frames0.size() == N);
145
    assert(frames1.size() == N);
4 jakw 146
 
42 jakw 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
 
41 jakw 158
    // occlusion maps
42 jakw 159
    cv::Mat occlusion0, occlusion1;
160
    cv::subtract(frames0Gray[0], frames0Gray[1], occlusion0);
161
    occlusion0 = occlusion0 > 50;
162
    cv::subtract(frames1Gray[0], frames1Gray[1], occlusion1);
163
    occlusion1 = occlusion1 > 50;
4 jakw 164
 
42 jakw 165
//    cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
166
//    cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
167
 
41 jakw 168
    // decoded patterns
42 jakw 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");
41 jakw 172
    for(int i=0; i<Nbits; i++){
42 jakw 173
        cv::Mat bit0;
174
        cv::subtract(frames0Gray[i*2+2], frames0Gray[i*2+3], bit0);
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;
180
        cv::subtract(frames1Gray[i*2+2], frames1Gray[i*2+3], bit1);
181
        bit1 = bit1 > 50;
182
        cv::add(code1, bit1/255*powi(2,i), code1, occlusion1, CV_16S);
41 jakw 183
    }
4 jakw 184
 
42 jakw 185
    cvtools::writeMat(code0, "code0.mat", "code0");
186
    cvtools::writeMat(code1, "code1.mat", "code1");
41 jakw 187
 
42 jakw 188
    // rectifying homographies (rotation+projections)
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);
194
    cv::Mat R0, R1, P0, P1, QRect;
195
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
196
 
41 jakw 197
    // interpolation maps
198
    cv::Mat map0X, map0Y, map1X, map1Y;
42 jakw 199
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
200
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
41 jakw 201
 
202
    // remap
42 jakw 203
    cv::Mat code0Rect, code1Rect, color0Rect, color1Rect;
204
    cv::remap(code0, code0Rect, map0X, map0Y, cv::INTER_NEAREST);
205
    cv::remap(code1, code1Rect, map1X, map1Y, cv::INTER_NEAREST);
206
    cv::remap(frames0[0], color0Rect, map0X, map0Y, cv::INTER_CUBIC);
207
    cv::remap(frames1[0], color1Rect, map1X, map1Y, cv::INTER_CUBIC);
41 jakw 208
 
42 jakw 209
cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
210
cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
41 jakw 211
 
42 jakw 212
//cvtools::writeMat(color0Rect, "color0.mat", "color0");
213
//cvtools::writeMat(color1Rect, "color1.mat", "color1");
214
 
215
    int nRows = code0Rect.rows;
216
    int nCols = code0Rect.cols;
217
 
41 jakw 218
    // matching
42 jakw 219
    std::vector<cv::Vec2f> q0Rect, q1Rect;
41 jakw 220
    for(int row=0; row<nRows; row++){
221
 
222
        std::vector<cv::Vec4f> edges0, edges1;
223
 
42 jakw 224
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
225
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
41 jakw 226
 
227
        int i=0, j=0;
228
        while(i<edges0.size() && j<edges1.size()){
229
 
230
            if(edges0[i][3] == edges1[j][3]){
42 jakw 231
                q0Rect.push_back(cv::Vec2f(edges0[i][0], row));
232
                q1Rect.push_back(cv::Vec2f(edges1[j][0], row));
41 jakw 233
                i += 1;
234
                j += 1;
42 jakw 235
            } else if(edges0[i][3] < edges1[j][3]){
41 jakw 236
                i += 1;
42 jakw 237
            } else if(edges0[i][3] > edges1[j][3]){
41 jakw 238
                j += 1;
239
            }
240
        }
241
 
242
 
243
    }
244
 
245
    // retrieve color information
42 jakw 246
    int nMatches = q0Rect.size();
41 jakw 247
    color.resize(nMatches);
248
    for(int i=0; i<nMatches; i++){
249
 
42 jakw 250
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
251
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
41 jakw 252
 
42 jakw 253
        color[i] = 0.5*(c0 + c1);
41 jakw 254
    }
255
 
42 jakw 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);
4 jakw 261
}