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
|
|
|
70 |
// Encode every pixel column
|
|
|
71 |
Nhorz = ceilf(log2f((float)screenCols));
|
|
|
72 |
|
|
|
73 |
// Number of vertical encoding patterns
|
|
|
74 |
Nvert = ceilf(log2f((float)screenRows));
|
|
|
75 |
|
|
|
76 |
// Set total pattern number
|
|
|
77 |
if(dir & CodecDirHorizontal)
|
|
|
78 |
this->N += Nhorz;
|
|
|
79 |
|
|
|
80 |
if(dir & CodecDirVertical)
|
|
|
81 |
this->N += Nvert;
|
|
|
82 |
|
|
|
83 |
if(dir & CodecDirHorizontal){
|
|
|
84 |
// Precompute horizontally encoding patterns
|
|
|
85 |
for(unsigned int p=0; p<Nhorz; p++){
|
|
|
86 |
cv::Mat patternP(1, screenCols, CV_8UC3);
|
|
|
87 |
// Loop through columns in first row
|
|
|
88 |
for(unsigned int j=0; j<screenCols; j++){
|
|
|
89 |
unsigned int jGray = binaryToGray(j);
|
|
|
90 |
// Amplitude of channels
|
|
|
91 |
float amp = get_bit(jGray, Nhorz-p);
|
|
|
92 |
patternP.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
|
|
|
93 |
}
|
|
|
94 |
patterns.push_back(patternP);
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
if(dir & CodecDirVertical){
|
|
|
98 |
// Precompute vertical encoding patterns
|
|
|
99 |
for(unsigned int p=0; p<Nvert; p++){
|
|
|
100 |
cv::Mat patternP(screenRows, 1, CV_8UC3);
|
|
|
101 |
|
|
|
102 |
// Loop through rows in first column
|
|
|
103 |
for(unsigned int i=0; i<screenRows; i++){
|
|
|
104 |
|
|
|
105 |
unsigned int iGray = binaryToGray(i);
|
|
|
106 |
|
|
|
107 |
// Amplitude of channels
|
|
|
108 |
float amp = get_bit(iGray, Nvert-p); // Nvert-p-1?
|
|
|
109 |
patternP.at<cv::Vec3b>(i,0) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
|
|
|
110 |
}
|
|
|
111 |
patterns.push_back(patternP);
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
cv::Mat EncoderGrayCode::getEncodingPattern(unsigned int depth){
|
|
|
117 |
return patterns[depth];
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
// Decoder
|
|
|
121 |
DecoderGrayCode::DecoderGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodecDir _dir) : Decoder(_screenCols, _screenRows, _dir){
|
|
|
122 |
|
|
|
123 |
// Number of horizontal encoding patterns
|
|
|
124 |
Nhorz = ceilf(log2f((float)screenCols));
|
|
|
125 |
|
|
|
126 |
// Number of vertical encoding patterns
|
|
|
127 |
Nvert = ceilf(log2f((float)screenRows));
|
|
|
128 |
|
|
|
129 |
if(dir & CodecDirHorizontal)
|
|
|
130 |
this->N += Nhorz;
|
|
|
131 |
|
|
|
132 |
if(dir & CodecDirVertical)
|
|
|
133 |
this->N += Nvert;
|
|
|
134 |
|
|
|
135 |
frames.resize(N);
|
|
|
136 |
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
void DecoderGrayCode::setFrame(unsigned int depth, const cv::Mat frame){
|
|
|
140 |
frames[depth] = frame;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
void DecoderGrayCode::decodeFrames(cv::Mat &up, cv::Mat &vp, cv::Mat &mask, cv::Mat &shading){
|
|
|
144 |
|
|
|
145 |
// Get shading (max) image
|
|
|
146 |
shading = cv::Scalar(0);
|
|
|
147 |
for(unsigned int f=0; f<frames.size(); f++){
|
|
|
148 |
shading = cv::max(shading, frames[f]);
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
// Get min image
|
|
|
152 |
cv::Mat minImage(shading.size(), CV_8U, cv::Scalar(255));
|
|
|
153 |
for(unsigned int f=0; f<frames.size(); f++){
|
|
|
154 |
minImage = cv::min(minImage, frames[f]);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
// Threshold shading image for mask
|
|
|
158 |
mask = (shading/minImage) > 2;
|
|
|
159 |
|
|
|
160 |
// Binarize frames. TODO: subpixel interpolation.
|
|
|
161 |
vector<cv::Mat> framesBinary(frames.size());
|
|
|
162 |
for(unsigned int i=0; i<frames.size(); i++){
|
|
|
163 |
// Foreground pixels 1, background 0
|
|
|
164 |
//cv::threshold(frames[i], framesBinary[i], 80, 1, cv::THRESH_BINARY);
|
|
|
165 |
framesBinary[i].create(frames[0].size(), CV_8U);
|
|
|
166 |
framesBinary[i] = cv::abs(shading-frames[i]) < cv::abs(frames[i]-minImage);
|
|
|
167 |
cv::threshold(framesBinary[i], framesBinary[i], 1, 1, cv::THRESH_BINARY);
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
if(dir & CodecDirHorizontal){
|
|
|
171 |
vector<cv::Mat> framesHorz(framesBinary.begin(), framesBinary.begin()+Nhorz);
|
|
|
172 |
|
|
|
173 |
// Construct up image.
|
|
|
174 |
for(int i = 0; i < up.rows; i++){
|
|
|
175 |
for(int j = 0; j < up.cols; j++){
|
|
|
176 |
unsigned int enc = 0;
|
|
|
177 |
for(unsigned int f=0; f<framesHorz.size(); f++){
|
|
|
178 |
// Gray decimal
|
|
|
179 |
enc += powi(2, Nhorz-f-1)*framesHorz[f].at<unsigned char>(i,j);
|
|
|
180 |
}
|
|
|
181 |
// Standard decimal
|
|
|
182 |
enc = grayToBinary(enc, Nhorz);
|
|
|
183 |
up.at<float>(i,j) = enc;
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
if(dir & CodecDirVertical){
|
|
|
188 |
vector<cv::Mat> framesVert(framesBinary.end()-Nvert, framesBinary.end());
|
|
|
189 |
|
|
|
190 |
// Construct vp image.
|
|
|
191 |
for(int i = 0; i < vp.rows; i++){
|
|
|
192 |
for(int j = 0; j < vp.cols; j++){
|
|
|
193 |
unsigned int enc = 0;
|
|
|
194 |
for(unsigned int f=0; f<framesVert.size(); f++){
|
|
|
195 |
// Gray decimal
|
|
|
196 |
enc += powi(2, Nvert-f-1)*framesVert[f].at<unsigned char>(i,j);
|
|
|
197 |
}
|
|
|
198 |
// Standard decimal
|
|
|
199 |
enc = grayToBinary(enc, Nvert);
|
|
|
200 |
vp.at<float>(i,j) = enc;
|
|
|
201 |
}
|
|
|
202 |
}
|
|
|
203 |
}
|
|
|
204 |
}
|