-
Notifications
You must be signed in to change notification settings - Fork 64
/
sl_util.cpp
415 lines (336 loc) · 11.3 KB
/
sl_util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// sl_util.cpp
// helper functions for structured light
// by John De Witt
#include "sl_util.h"
#define DBUG 0
using namespace std;
using namespace cv;
// Convert a 10-bit Gray code value into a binary integer representation
// Thanks to http://www.dspguru.com/dsp/tricks/gray-code-conversion
uint16_t gray2bin(uint16_t gray){
uint16_t temp = gray ^ (gray>>8);
// Our error condition should propagate
if(gray == UINT16_MAX) return -1;
temp ^= (temp>>4);
temp ^= (temp>>2);
temp ^= (temp>>1);
return temp;
}
uint16_t bin2gray( uint16_t num ) {
return (num>>1) ^ num;
}
void printbin( int v ){
printf("[");
for (int i=0; i<32; i++) {
printf("%c", (v>>(32-i-1))&1?'1':'0');
if((i+1)%4==0) printf(" ");
}
printf("]");
}
//// convert unsigned binary to gray code value
//unsigned int bin2gray( unsigned int num ) {
// return (num>>1) ^ num;
//}
//
//// Convert a 10-bit Gray code value into a binary integer representation
//// Thanks to http://www.dspguru.com/dsp/tricks/gray-code-conversion
//int gray2bin(int gray){
// unsigned int temp = gray ^ (gray>>8);
//
// // Our error condition should propagate
// if(gray == -1) return -1;
//
// temp ^= (temp>>4);
// temp ^= (temp>>2);
// temp ^= (temp>>1);
// return temp;
//}
void generatePattern( Mat img, Size sz, int axis, int used_bit, bool inv ){
img.create(sz,CV_8UC3);
uint8_t* arr = NULL;
for(int r=0; r<img.rows; r++){
arr = img.ptr<uint8_t>(r);
for(int c=0; c<img.cols; c++){
uint16_t v = 0;
switch(axis){
case AXIS_X:
v = c;
break;
case AXIS_Y:
v = r;
break;
}
uint8_t p = ( (bin2gray(v) >> used_bit) & 1 ) == 1 ? 255 : 0;
arr[c*3+0] = p;
arr[c*3+1] = p;
arr[c*3+2] = p;
}
}
}
cv::Mat get_gray_image(const std::string & filename){
cv::Mat rgb_image = cv::imread(filename);
if(rgb_image.rows>0 && rgb_image.cols>0)
{
cv::Mat gray_image;
cvtColor(rgb_image, gray_image, COLOR_BGR2GRAY);
return gray_image;
}
return cv::Mat();
}
// create a two channel grayscale image with global/direct components
cv::Mat get_direct_global(const std::vector<cv::Mat>& images, float b)
{
static const unsigned MAXIMG = 10;
unsigned count = images.size();
if (count<1)
{ // no images
return cv::Mat();
}
// std::cout << "--- get_direct_global START ---" << endl;
if( MAXIMG < count )
{
// std::cout << "WARNING: Using only " << MAXIMG << " of "<< count << std::endl;
count = MAXIMG;
}
for(int i=0; i<count; i++){
if( images.at(i).type() != CV_8UC1 ){
// std::cout << "only grayscale images are supported" << std::endl;
return cv::Mat();
}
}
cv::Size size = images.at(0).size();
cv::Mat light(size,CV_8UC2);
double b1 = 1.0/(1.0 - b);
double b2 = 2.0/(1.0 - b*b);
for(int r=0; r<size.height; r++)
{
// get pointer to all input images
const uint8_t* row[MAXIMG];
for(int i=0; i<count; i++){
row[i] = images.at(i).ptr<uint8_t>(r);
}
cv::Vec2b* row_light = light.ptr<cv::Vec2b>(r);
for(int c=0; c<size.width; c++)
{
unsigned Lmax = row[0][c];
unsigned Lmin = row[0][c];
for(int i=0; i<count; i++)
{
if(row[i][c] > Lmax) Lmax = row[i][c];
if(row[i][c] < Lmin) Lmin = row[i][c];
}
int Ld = static_cast<int>(b1*(Lmax - Lmin) + 0.5);
int Lg = static_cast<int>(b2*(Lmin - b*Lmax) + 0.5);
row_light[c][0] = (Lg>0 ? static_cast<unsigned>(Ld) : Lmax);
row_light[c][1] = (Lg>0 ? static_cast<unsigned>(Lg) : 0);
}
}
// std::cout << "--- get_direct_global END ---" << endl;
return light;
}
// Gray Code Binary Decoding
// input: 2x 8UC3, 1 image of binary pattern, 1 image of binary pattern inverse/complement
// output 1x 8UC1, of per-pixel decoded bitfield
// output 1x 32FC4, of per-pixel observed color (accumulates)
double decodeImagePair( Mat &src,
Mat &inv,
Mat &out_rgb,
Mat &out_bit,
int threshold)
{
double diff_tot = 0;
int diff_n = 0;
int ch_src = src.channels();
int ch_inv = inv.channels();
if(ch_src!=ch_inv)
{
printf("decodeImagePair error: channel number mismatch for input images [%d],[%d]\n",ch_src,ch_inv);
return -1;
}
// rgb image input (convert each pixel triplet into monochrome)
if(ch_src == 3)
{
for(int r=0; r<src.rows; r++)
{
const Vec3b* src_px = src.ptr<Vec3b>(r);
const Vec3b* inv_px = inv.ptr<Vec3b>(r);
Vec4f* rgb_px = out_rgb.ptr<Vec4f>(r);
uchar* bit_px = out_bit.ptr<uchar>(r);
for(int c=0; c<src.cols; c++)
{
int gray0 = (int)(0.299*src_px[c][2] + 0.587*src_px[c][1] + 0.114*src_px[c][0]);
int gray1 = (int)(0.299*inv_px[c][2] + 0.587*inv_px[c][1] + 0.114*inv_px[c][0]);
int diff = gray0 - gray1;
// found signal
if( abs(diff) > threshold ){
diff_tot += abs(diff);
diff_n++;
// (first pattern brighter) : white / 1
if( diff > 0 ){
bit_px[c] = 255;
// rgb_px[c] = src_px[c];
rgb_px[c][0] += src_px[c][0];
rgb_px[c][1] += src_px[c][1];
rgb_px[c][2] += src_px[c][2];
rgb_px[c][3] += 1;
}
// (second pattern brighter) : black / 0
if( diff < 0 ){
bit_px[c] = 0;
// rgb_px[c] = inv_px[c];
rgb_px[c][0] += inv_px[c][0];
rgb_px[c][1] += inv_px[c][1];
rgb_px[c][2] += inv_px[c][2];
rgb_px[c][3] += 1;
}
} else {
// set to "invalid" sentinel value 128
bit_px[c] = 128;
}
}
}
}
// monochrome image input
else if(ch_src == 1)
{
for(int r=0; r<src.rows; r++)
{
const uchar* src_px = src.ptr<uchar>(r);
const uchar* inv_px = inv.ptr<uchar>(r);
Vec4f* rgb_px = out_rgb.ptr<Vec4f>(r);
uchar* bit_px = out_bit.ptr<uchar>(r);
for(int c=0; c<src.cols; c++)
{
int gray0 = src_px[c];
int gray1 = inv_px[c];
int diff = gray0 - gray1;
// found signal
if( abs(diff) > threshold ){
diff_tot += abs(diff);
diff_n++;
// (first pattern brighter) : white / 1
if( diff > 0 ){
bit_px[c] = 255;
rgb_px[c][0] += src_px[c];
rgb_px[c][1] += src_px[c];
rgb_px[c][2] += src_px[c];
rgb_px[c][3] += 1;
}
// (second pattern brighter) : black / 0
if( diff < 0 ){
bit_px[c] = 0;
rgb_px[c][0] += inv_px[c];
rgb_px[c][1] += inv_px[c];
rgb_px[c][2] += inv_px[c];
rgb_px[c][3] += 1;
}
} else {
// set to "invalid" sentinel value 128
bit_px[c] = 128;
}
}
}
}
if(diff_n>0)
{
return diff_tot/diff_n;
}
else
{
return -1;
}
}
// input: 1x 8UC1, one bitfield from the output codeword
// output 1x 16UC1, edited code map updated with new bitfield
void mergeNewBit( Mat &bit, Mat &map, int bitfield, int minbit )
{
//printf("MERGING NEW BITFIELD : [bitfield %d] [minbit %d]\n",bitfield,minbit);
//
ushort b = 1<<bitfield;
// copy in new bits as needed
for(int r=0; r<bit.rows; r++)
{
uchar* bit_px = bit.ptr<uchar>(r);
ushort* map_px = map.ptr<ushort>(r);
if(bitfield < minbit)
{
}
else
{
for(int c=0; c<bit.cols; c++)
{
if(map_px[c] != UINT16_MAX)
{
switch( bit_px[c] )
{
case 0:
map_px[c] &= (~b);
break;
case 255:
map_px[c] |= (b);
break;
case 128:
// if(bitfield > minbit)
map_px[c] = UINT16_MAX; // mark as invalid
break;
}
}
}
}
}
}
// pattern and inverse
// correspondence map, rgb average map, and difference stats
void parseImagePair( Mat &pattern,
Mat &pattern_inv,
Mat &out_map,
Mat &out_rgb,
Mat &out_diff,
int bitfield,
int threshold )
{
double* diff_p = out_diff.ptr<double>(0);
// per pixel row
for(int r = 0; r < pattern.rows; r++){
const Vec3b* img_px = pattern.ptr<Vec3b>(r);
const Vec3b* inv_px = pattern_inv.ptr<Vec3b>(r);
ushort* map_px = out_map.ptr<ushort>(r);
// float* rgb_px = out_rgb.ptr<float>(r);
Vec4f* rgb_px = out_rgb.ptr<Vec4f>(r);
// per pixel column
for(int c = 0; c < pattern.cols; c++){
if( map_px[c] == UINT16_MAX){
continue;
}
int gray0 = (0.299*img_px[c][2] + 0.587*img_px[c][1] + 0.114*img_px[c][0]);
int gray1 = (0.299*inv_px[c][2] + 0.587*inv_px[c][1] + 0.114*inv_px[c][0]);
int diff = gray0 - gray1;
map_px[c] = map_px[c] << 1;
assert( (map_px[c] & 0x01) == 0 );
// found signal
if( abs(diff) > threshold ){
diff_p[bitfield*2+0] += abs(diff);
diff_p[bitfield*2+1] += 1;
// white
if( diff > 0 ){
map_px[c] |= 1;
rgb_px[c][0] += img_px[c][0];
rgb_px[c][1] += img_px[c][1];
rgb_px[c][2] += img_px[c][2];
rgb_px[c][3] += 1;
}
// black
if( diff < 0 ){
map_px[c] |= 0;
rgb_px[c][0] += inv_px[c][0];
rgb_px[c][1] += inv_px[c][1];
rgb_px[c][2] += inv_px[c][2];
rgb_px[c][3] += 1;
}
} else {
// set to "invalid" sentinel value
map_px[c] = UINT16_MAX;
}
}
}
}