Subversion Repositories seema-scanner

Rev

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

Rev Author Line No. Line
4 jakw 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
// Encoder
67
EncoderGrayCode::EncoderGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodecDir _dir) : Encoder(_screenCols, _screenRows, _dir){
68
 
69
    // Number of horizontal encoding patterns
27 jakw 70
    Nhorz = 10;
4 jakw 71
 
72
    // Number of vertical encoding patterns
27 jakw 73
    Nvert = 8;
4 jakw 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 EncoderGrayCode::getEncodingPattern(unsigned int depth){
116
    return patterns[depth];
117
}
118
 
119
// Decoder
27 jakw 120
DecoderGrayCode::DecoderGrayCode(CodecDir _dir) : Decoder(_dir){
4 jakw 121
 
122
    // Number of horizontal encoding patterns
27 jakw 123
    Nhorz = 10;
4 jakw 124
 
125
    // Number of vertical encoding patterns
27 jakw 126
    Nvert = 8;
4 jakw 127
 
128
}
129
 
27 jakw 130
void DecoderGrayCode::decodeFrames(const std::vector<cv::Mat> frames, cv::Mat &up, cv::Mat &vp, cv::Mat &mask, cv::Mat &shading){
4 jakw 131
 
132
    // Get shading (max) image
133
    shading = cv::Scalar(0);
134
    for(unsigned int f=0; f<frames.size(); f++){
135
        shading = cv::max(shading, frames[f]);
136
    }
137
 
138
    // Get min image
139
    cv::Mat minImage(shading.size(), CV_8U, cv::Scalar(255));
140
    for(unsigned int f=0; f<frames.size(); f++){
141
        minImage = cv::min(minImage, frames[f]);
142
    }
143
 
144
    // Threshold shading image for mask
145
    mask = (shading/minImage) > 2;
146
 
147
    // Binarize frames. TODO: subpixel interpolation.
148
    vector<cv::Mat> framesBinary(frames.size());
149
    for(unsigned int i=0; i<frames.size(); i++){
150
        // Foreground pixels 1, background 0
151
        //cv::threshold(frames[i], framesBinary[i], 80, 1, cv::THRESH_BINARY);
152
        framesBinary[i].create(frames[0].size(), CV_8U);
153
        framesBinary[i] = cv::abs(shading-frames[i]) < cv::abs(frames[i]-minImage);
154
        cv::threshold(framesBinary[i], framesBinary[i], 1, 1, cv::THRESH_BINARY);
155
    }
156
 
157
    if(dir & CodecDirHorizontal){
158
        vector<cv::Mat> framesHorz(framesBinary.begin(), framesBinary.begin()+Nhorz);
159
 
160
        // Construct up image.
161
        for(int i = 0; i < up.rows; i++){
162
            for(int j = 0; j < up.cols; j++){
163
                unsigned int enc = 0;
164
                for(unsigned int f=0; f<framesHorz.size(); f++){
165
                    // Gray decimal
166
                    enc += powi(2, Nhorz-f-1)*framesHorz[f].at<unsigned char>(i,j);
167
                }
168
                // Standard decimal
169
                enc = grayToBinary(enc, Nhorz);
170
                up.at<float>(i,j) = enc;
171
            }
172
        }
173
    }
174
    if(dir & CodecDirVertical){
175
        vector<cv::Mat> framesVert(framesBinary.end()-Nvert, framesBinary.end());
176
 
177
        // Construct vp image.
178
        for(int i = 0; i < vp.rows; i++){
179
            for(int j = 0; j < vp.cols; j++){
180
                unsigned int enc = 0;
181
                for(unsigned int f=0; f<framesVert.size(); f++){
182
                    // Gray decimal
183
                    enc += powi(2, Nvert-f-1)*framesVert[f].at<unsigned char>(i,j);
184
                }
185
                // Standard decimal
186
                enc = grayToBinary(enc, Nvert);
187
                vp.at<float>(i,j) = enc;
188
            }
189
        }
190
    }
191
}