Subversion Repositories seema-scanner

Rev

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

Rev 36 Rev 37
Line 1... Line -...
1
#include "CodecGrayCode.h"
-
 
2
#include <cmath>
-
 
3
 
-
 
4
#ifndef log2f
-
 
5
#define log2f(x) (log(x)/log(2.0))
-
 
6
#endif
-
 
7
 
-
 
8
using namespace std;
-
 
9
 
-
 
10
/*
-
 
11
 * The purpose of this function is to convert an unsigned
-
 
12
 * binary number to reflected binary Gray code.
-
 
13
 *
-
 
14
 * The operator >> is shift right. The operator ^ is exclusive or.
-
 
15
 * Source: http://en.wikipedia.org/wiki/Gray_code
-
 
16
 */
-
 
17
static unsigned int binaryToGray(unsigned int num) {
-
 
18
    return (num >> 1) ^ num;
-
 
19
}
-
 
20
 
-
 
21
/*
-
 
22
 * From Wikipedia: http://en.wikipedia.org/wiki/Gray_code
-
 
23
 * The purpose of this function is to convert a reflected binary
-
 
24
 * Gray code number to a binary number.
-
 
25
 */
-
 
26
static unsigned grayToBinary(unsigned num, unsigned numBits)
-
 
27
{
-
 
28
    for (unsigned shift = 1; shift < numBits; shift <<= 1){
-
 
29
        num ^= num >> shift;
-
 
30
    }
-
 
31
    return num;
-
 
32
}
-
 
33
 
-
 
34
/*
-
 
35
 * Function takes the decimal number
-
 
36
 * Function takes the Nth bit (1 to 31)
-
 
37
 * Return the value of Nth bit from decimal
-
 
38
 * Source: http://icfun.blogspot.com/2009/04/get-n-th-bit-value-of-any-integer.html
-
 
39
 */
-
 
40
static int get_bit(int decimal, int N){
-
 
41
 
-
 
42
    // Shifting the 1 for N-1 bits
-
 
43
    int constant = 1 << (N-1);
-
 
44
 
-
 
45
    // If the bit is set, return 1
-
 
46
    if( decimal & constant ){
-
 
47
        return 1;
-
 
48
    }
-
 
49
 
-
 
50
    // If the bit is not set, return 0
-
 
51
    return 0;
-
 
52
}
-
 
53
 
-
 
54
static inline int powi(int num, unsigned int exponent){
-
 
55
    // NOT EQUIVALENT TO pow()
-
 
56
    if(exponent == 0)
-
 
57
        return 1;
-
 
58
 
-
 
59
    float res = num;
-
 
60
    for(unsigned int i=0; i<exponent-1; i++)
-
 
61
        res *= num;
-
 
62
 
-
 
63
    return res;
-
 
64
}
-
 
65
 
-
 
66
// Algorithm
-
 
67
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodecDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
-
 
68
 
-
 
69
    // Number of horizontal encoding patterns
-
 
70
    Nhorz = ceilf(log2f((float)screenCols));;
-
 
71
 
-
 
72
    // Number of vertical encoding patterns
-
 
73
    Nvert = ceilf(log2f((float)screenRows));;
-
 
74
 
-
 
75
    // Set total pattern number
-
 
76
    if(dir & CodecDirHorizontal)
-
 
77
        this->N += Nhorz;
-
 
78
 
-
 
79
    if(dir & CodecDirVertical)
-
 
80
        this->N += Nvert;
-
 
81
 
-
 
82
    if(dir & CodecDirHorizontal){
-
 
83
        // Precompute horizontally encoding patterns
-
 
84
        for(unsigned int p=0; p<Nhorz; p++){
-
 
85
            cv::Mat patternP(1, screenCols, CV_8UC3);
-
 
86
            // Loop through columns in first row
-
 
87
            for(unsigned int j=0; j<screenCols; j++){
-
 
88
                unsigned int jGray = binaryToGray(j);
-
 
89
                // Amplitude of channels
-
 
90
                float amp = get_bit(jGray, Nhorz-p);
-
 
91
                patternP.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
-
 
92
            }
-
 
93
            patterns.push_back(patternP);
-
 
94
        }
-
 
95
    }
-
 
96
    if(dir & CodecDirVertical){
-
 
97
        // Precompute vertical encoding patterns
-
 
98
        for(unsigned int p=0; p<Nvert; p++){
-
 
99
            cv::Mat patternP(screenRows, 1, CV_8UC3);
-
 
100
 
-
 
101
            // Loop through rows in first column
-
 
102
            for(unsigned int i=0; i<screenRows; i++){
-
 
103
 
-
 
104
                unsigned int iGray = binaryToGray(i);
-
 
105
 
-
 
106
                // Amplitude of channels
-
 
107
                float amp = get_bit(iGray, Nvert-p); // Nvert-p-1?
-
 
108
                patternP.at<cv::Vec3b>(i,0) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
-
 
109
            }
-
 
110
            patterns.push_back(patternP);
-
 
111
        }
-
 
112
    }
-
 
113
}
-
 
114
 
-
 
115
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
-
 
116
    return patterns[depth];
-
 
117
}
-
 
118
 
-
 
119
// Algorithm
-
 
120
AlgorithmGrayCode::AlgorithmGrayCode(CodecDir _dir, int _screenResX, int _screenResY) : Algorithm(_dir, _screenResX, _screenResY){
-
 
121
 
-
 
122
    // Number of horizontal encoding patterns
-
 
123
    Nhorz = ceilf(log2f((float)screenCols));;
-
 
124
 
-
 
125
    // Number of vertical encoding patterns
-
 
126
    Nvert = ceilf(log2f((float)screenRows));;
-
 
127
}
-
 
128
 
-
 
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){
-
 
130
 
-
 
131
 
-
 
132
 
-
 
133
 
-
 
134
 
-
 
135
}
-