41 |
jakw |
1 |
#include "AlgorithmGrayCode.h"
|
4 |
jakw |
2 |
#include <cmath>
|
|
|
3 |
|
|
|
4 |
#ifndef log2f
|
|
|
5 |
#define log2f(x) (log(x)/log(2.0))
|
|
|
6 |
#endif
|
|
|
7 |
|
41 |
jakw |
8 |
//using namespace std;
|
4 |
jakw |
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 |
|
36 |
jakw |
66 |
// Algorithm
|
41 |
jakw |
67 |
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodingDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
|
4 |
jakw |
68 |
|
|
|
69 |
// Number of horizontal encoding patterns
|
41 |
jakw |
70 |
Nhorz = ceilf(log2f((float)screenCols))*2;
|
4 |
jakw |
71 |
|
|
|
72 |
// Number of vertical encoding patterns
|
41 |
jakw |
73 |
Nvert = ceilf(log2f((float)screenRows))*2;
|
4 |
jakw |
74 |
|
41 |
jakw |
75 |
// on/off patterns
|
|
|
76 |
this->N = 2;
|
|
|
77 |
|
4 |
jakw |
78 |
// Set total pattern number
|
41 |
jakw |
79 |
if(dir & CodingDirHorizontal)
|
4 |
jakw |
80 |
this->N += Nhorz;
|
|
|
81 |
|
41 |
jakw |
82 |
if(dir & CodingDirVertical)
|
4 |
jakw |
83 |
this->N += Nvert;
|
|
|
84 |
|
41 |
jakw |
85 |
// all on pattern
|
|
|
86 |
cv::Mat pattern(1, 1, CV_8UC3);
|
|
|
87 |
pattern.setTo(cv::Vec3b(255.0,255.0,255.0));
|
|
|
88 |
patterns.push_back(pattern);
|
|
|
89 |
|
|
|
90 |
// all off pattern
|
|
|
91 |
pattern.setTo(cv::Vec3b(0.0,0.0,0.0));
|
|
|
92 |
patterns.push_back(pattern);
|
|
|
93 |
|
|
|
94 |
if(dir & CodingDirHorizontal){
|
4 |
jakw |
95 |
// Precompute horizontally encoding patterns
|
|
|
96 |
for(unsigned int p=0; p<Nhorz; p++){
|
|
|
97 |
cv::Mat patternP(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 |
}
|
41 |
jakw |
108 |
if(dir & CodingDirVertical){
|
4 |
jakw |
109 |
// Precompute vertical encoding patterns
|
|
|
110 |
for(unsigned int p=0; p<Nvert; p++){
|
|
|
111 |
cv::Mat patternP(screenRows, 1, CV_8UC3);
|
|
|
112 |
|
|
|
113 |
// Loop through rows in first column
|
|
|
114 |
for(unsigned int i=0; i<screenRows; i++){
|
|
|
115 |
|
|
|
116 |
unsigned int iGray = binaryToGray(i);
|
|
|
117 |
|
|
|
118 |
// Amplitude of channels
|
|
|
119 |
float amp = get_bit(iGray, Nvert-p); // Nvert-p-1?
|
|
|
120 |
patternP.at<cv::Vec3b>(i,0) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
|
|
|
121 |
}
|
|
|
122 |
patterns.push_back(patternP);
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
|
36 |
jakw |
127 |
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
|
4 |
jakw |
128 |
return patterns[depth];
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
|
41 |
jakw |
132 |
bool sortingLarger(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){
|
4 |
jakw |
134 |
|
41 |
jakw |
135 |
int nCols = scanLine.cols;
|
|
|
136 |
|
|
|
137 |
for(int col=1; col<nCols; nCols++){
|
|
|
138 |
|
|
|
139 |
int labelLeft = scanLine.at<int>(0,col-1);
|
|
|
140 |
int labelRight = scanLine.at<int>(0,col);
|
|
|
141 |
|
|
|
142 |
if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
|
|
|
143 |
|
|
|
144 |
int orderingRelation = (2 << Nbits)*labelLeft + labelRight;
|
|
|
145 |
|
|
|
146 |
edges.push_back(cv::Vec4f(col, labelLeft, labelRight, orderingRelation));
|
|
|
147 |
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
std::sort(edges.begin(), edges.end(), sortingLarger);
|
4 |
jakw |
152 |
}
|
|
|
153 |
|
41 |
jakw |
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){
|
4 |
jakw |
155 |
|
41 |
jakw |
156 |
assert(frames0.size() == N);
|
|
|
157 |
assert(frames1.size() == N);
|
4 |
jakw |
158 |
|
41 |
jakw |
159 |
// occlusion maps
|
|
|
160 |
cv::Mat occlusion0 = (frames0[0] - frames0[1]) > 0;
|
|
|
161 |
cv::Mat occlusion1 = (frames1[0] - frames1[1]) > 0;
|
4 |
jakw |
162 |
|
41 |
jakw |
163 |
// decoded patterns
|
|
|
164 |
cv::Mat code0, code1;
|
4 |
jakw |
165 |
|
41 |
jakw |
166 |
int Nbits = (N-2)/2;
|
|
|
167 |
for(int i=0; i<Nbits; i++){
|
|
|
168 |
cv::Mat bit0 = (frames0[i*2+2] - frames0[i*2+3]) > 0;
|
|
|
169 |
code0 += bit0*pow(2, i);
|
|
|
170 |
cv::Mat bit1 = (frames1[i*2+2] - frames1[i*2+3]) > 0;
|
|
|
171 |
code1 += bit1*pow(2, i);
|
|
|
172 |
}
|
4 |
jakw |
173 |
|
41 |
jakw |
174 |
// rectifying homographies
|
|
|
175 |
cv::Size frameSize(frames0[0].cols, frames0[0].rows);
|
|
|
176 |
cv::Mat R0, R1, P0, P1, Q;
|
|
|
177 |
cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, calibration.R1, calibration.T1, R0, R1, P0, P1, Q);
|
|
|
178 |
|
|
|
179 |
// interpolation maps
|
|
|
180 |
cv::Mat map0X, map0Y, map1X, map1Y;
|
|
|
181 |
cv::initUndistortRectifyMap(P0, calibration.k0, R0, cv::Mat(), frameSize, CV_32FC1, map0X, map0Y);
|
|
|
182 |
cv::initUndistortRectifyMap(P1, calibration.k1, R1, cv::Mat(), frameSize, CV_32FC1, map1X, map1Y);
|
|
|
183 |
|
|
|
184 |
// remap
|
|
|
185 |
cv::remap(occlusion0, occlusion0, map0X, map0Y, cv::INTER_CUBIC);
|
|
|
186 |
cv::remap(occlusion1, occlusion1, map1X, map1Y, cv::INTER_CUBIC);
|
|
|
187 |
cv::remap(code0, code0, map1X, map1Y, cv::INTER_CUBIC);
|
|
|
188 |
cv::remap(code1, code1, map1X, map1Y, cv::INTER_CUBIC);
|
|
|
189 |
|
|
|
190 |
int nRows = occlusion0.rows;
|
|
|
191 |
int nCols = occlusion0.cols;
|
|
|
192 |
|
|
|
193 |
// matching
|
|
|
194 |
for(int row=0; row<nRows; row++){
|
|
|
195 |
|
|
|
196 |
std::vector<cv::Vec4f> edges0, edges1;
|
|
|
197 |
|
|
|
198 |
getEdgeLabels(code0.row(row), Nbits, edges0);
|
|
|
199 |
getEdgeLabels(code1.row(row), Nbits, edges1);
|
|
|
200 |
|
|
|
201 |
// REMOVE DOUBLE ENTRIES!
|
|
|
202 |
|
|
|
203 |
int i=0, j=0;
|
|
|
204 |
while(i<edges0.size() && j<edges1.size()){
|
|
|
205 |
|
|
|
206 |
if(edges0[i][3] == edges1[j][3]){
|
|
|
207 |
q0.push_back(cv::Point2f(row, edges0[i][0]));
|
|
|
208 |
q1.push_back(cv::Point2f(row, edges1[j][0]));
|
|
|
209 |
i += 1;
|
|
|
210 |
j += 1;
|
|
|
211 |
} else if(edges0[i][3] < edges1[i][3]){
|
|
|
212 |
i += 1;
|
|
|
213 |
} else if(edges0[i][3] > edges1[i][3]){
|
|
|
214 |
j += 1;
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
// retrieve color information
|
|
|
222 |
int nMatches = q0.size();
|
|
|
223 |
color.resize(nMatches);
|
|
|
224 |
for(int i=0; i<nMatches; i++){
|
|
|
225 |
|
|
|
226 |
cv::Vec3f color0 = frames0[0].at<cv::Vec3f>(q0[i].y, q0[i].x);
|
|
|
227 |
cv::Vec3f color1 = frames1[0].at<cv::Vec3f>(q1[i].y, q1[i].x);
|
|
|
228 |
|
|
|
229 |
color[i] = 0.5*(color0 + color1);
|
|
|
230 |
}
|
|
|
231 |
|
4 |
jakw |
232 |
}
|