-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.h
419 lines (375 loc) · 9 KB
/
Image.h
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
416
417
418
419
#ifndef __IMAGE__H__
#define __IMAGE__H__
#include "common/image_util.h"
#include "Gradient.h"
#include <cassert>
#include <vector>
double GetRealWorldXFromDepth(uint16_t depth, int x); //returns real world X in mm, with reference to the center of the image as 0mm
double GetRealWorldYFromDepth(uint16_t depth, int y); //returns real world Y in mm, with reference to the center of the image as 0mm
template <typename T>
class Image {
public:
Image(int w, int h);
~Image();
T get(int x, int y);
T get(int i);
int getX(int i);
int getY(int i);
void set(int x, int y, T t);
void set(int i, T t);
//Update data starting at index=0 -> index=ts.size()-1
void update(const std::vector<T> & ts);
//Returns im, pointer, not really safe, but nice for display
//Sets the image and returns it
//Comparator takes T and valid bit, if add other things like gradient, this would take those too
image_u32_t * getImage(uint32_t(*tToPX)(T, bool, Gradient,int));
//Computes the gradient for the image
void computeGradient(double(*tVal)(T,bool));
void invalidate(int x, int y);
void invalidate(int i);
void validate(int x, int y);
void validate(int i);
void setValid(int x, int y, bool v);
void setValid(int i, bool v);
bool isValid(int x, int y);
bool isValid(int i);
void copyValid(std::vector<bool> & otherValid);
//Search
//*** 012
//*.* 3.4
//*** 567
//Vector of neighbors
//Pass comparator to tell wheter neighbor
//And bit vector corresponding to above diagram
std::vector<T> getNeighbors(int x, int y,
const std::vector<bool> & u_valid,
bool(*is_neighbor)(T, T)
);
std::vector<int> getNeighborIds(int x, int y,
const std::vector<bool> & u_valid,
bool(*is_neighbor)(T, T)
);
std::vector<int> getNeighborIds(int x, int y,
const std::vector<bool> & u_valid,
bool(*is_neighbor)(Gradient, Gradient));
//Get nxn grid of id's centered at i
std::vector<int> getBlockNeighborIds(int i, int n);
std::vector<int> getRColNeighborIds(int i, int n);
size_t size();
bool empty();
int w();
int h();
void printGradient();
void printGradient(int i);
int id(int x, int y);
std::vector<bool> valid;
//Gradient information
std::vector<Gradient> gradient;
std::vector<T> data;
private:
int width;
int height;
//Width*height vector containing our data
//Access to (x,y) -> x + width*y
//Valid bits to keep track of validness
image_u32_t * im;
};
template <typename T>
Image<T>::Image(int w, int h)
:width(w), height(h) {
data.resize(w*h);
valid = std::vector<bool>(w*h, true);
gradient = std::vector<Gradient>(w*h, Gradient());
im = image_u32_create(w, h);
/*
printf("Im w: %d, h: %d",
im->width,
im->height);
*/
}
template <typename T>
Image<T>::~Image() {
//image_u32_destroy(im);
}
template <typename T>
T Image<T>::get(int x, int y) {
return data[id(x,y)];
}
template <typename T>
int Image<T>::getY(int i) {
assert(i >= 0 && i < (int)data.size());
return i/width;
}
template <typename T>
int Image<T>::getX(int i) {
assert(i >= 0 && i < (int)data.size());
return i%width;
}
template <typename T>
T Image<T>::get(int i) {
assert(i >= 0 && i < (int)data.size());
return data[i];
}
template <typename T>
int Image<T>::w() {
return width;
}
template <typename T>
int Image<T>::h() {
return height;
}
template <typename T>
void Image<T>::set(int x, int y, T t) {
data[id(x,y)] = t;
}
template <typename T>
void Image<T>::set(int i, T t) {
assert(i >= 0 && i < (int)data.size());
data[i] = t;
}
template <typename T>
size_t Image<T>::size() {
return width*height;
}
template <typename T>
bool Image<T>::empty() {
return size() == 0;
}
template <typename T>
void Image<T>::update(const std::vector<T> & ts) {
assert(ts.size() == data.size());
data = ts;
valid.assign(size(),true);
//gradient.assign(size(),Gradient());
}
template <typename T>
image_u32_t * Image<T>::getImage(uint32_t(*tToPX)(T, bool, Gradient,
int)) {
for(int x = 0; x < im->width; ++x) {
for(int y = 0; y < im->height; ++y) {
/*
if(gradient[id(x,y)].x() ||
gradient[id(x,y)].y()) {
printf("gradient @ (%d,%d)\n",x,y);
gradient[id(x,y)].print();
}
*/
im->buf[x+y*im->stride] = tToPX(
get(x,y),
valid[id(x,y)],
gradient[id(x,y)],
id(x,y)
);
}
}
//printGradient();
return im;
}
template <typename T>
void Image<T>::computeGradient(double(*tVal)(T,bool)) {
//Leave far edges of image with gradient = 0, to avoid bounds
//checking
for(int x = 1; x < width-1; ++x) {
for(int y = 1; y < height-1; ++y) {
//printf("(x+1,y):%d\n", tVal(get(x+1,y)));
//printf("(x+1,y):%f\n",tVal(get(x+1,y)) - tVal(get(x-1,y)));
gradient[id(x,y)].x(
tVal(get(x+1,y),valid[id(x+1,y)]) -
tVal(get(x-1,y),valid[id(x-1,y)])
);
//printf("(x+1,y):%f\n",gradient[id(x,y)].x());
gradient[id(x,y)].y(
tVal(get(x,y+1),valid[id(x,y+1)]) -
tVal(get(x,y-1),valid[id(x,y-1)])
);
//gradient[id(x,y)].print();
}
}
//printGradient();
}
template <typename T>
void Image<T>::invalidate(int i) {
setValid(i,false);
}
template <typename T>
void Image<T>::invalidate(int x, int y) {
setValid(x,y,false);
}
template <typename T>
void Image<T>::validate(int x, int y) {
setValid(x,y,true);
}
template <typename T>
void Image<T>::validate(int i) {
setValid(i,true);
}
template <typename T>
void Image<T>::setValid(int x, int y, bool v) {
valid[id(x,y)] = v;
}
template <typename T>
void Image<T>::setValid(int i, bool v) {
assert(i >= 0 && i < (int)valid.size());
valid[i] = v;
}
template <typename T>
bool Image<T>::isValid(int x, int y) {
return valid[id(x,y)];
}
template <typename T>
bool Image<T>::isValid(int i) {
assert(i >= 0 && i < (int)valid.size());
return valid[i];
}
template <typename T>
void Image<T>::copyValid(std::vector<bool> &otherValid) {
otherValid = valid;
}
template <typename T>
int Image<T>::id(int x, int y) {
assert(x >= 0 && x < width);
assert(y >= 0 && y < height);
return y*width + x;
}
template <typename T>
void Image<T>::printGradient(int i) {
gradient[i].print();
}
template <typename T>
void Image<T>::printGradient() {
for(size_t i = 0; i < size(); ++i) {
gradient[i].print();
}
}
template <typename T>
std::vector<int> Image<T>::getNeighborIds(int x, int y,
const std::vector<bool> & u_valid,
bool(*is_neighbor)(T, T)
) {
assert(u_valid.size() == 8);
std::vector<int> neighbors;
T current = data[id(x,y)];
int index = 0;
for(int j = -1; j <= 1; ++j) {
for(int i = -1; i <= 1; ++i) {
//edge
if(j + y < 0 || j + y >= height) {
index ++;
continue;
}
//edge
if(i + x < 0 || i + x >= width) {
index ++;
continue;
}
//Don't return middle
if(j == 0 && i == 0) {
//Don't increment index
continue;
}
size_t n = id(x+i,y+j);
T t = data[n];
if(valid[n] && u_valid[index]
&& is_neighbor(current,t)) {
assert(n >= 0 && n < size());
neighbors.push_back(n);
}
index ++;
}
}
return neighbors;
}
template <typename T>
std::vector<int> Image<T>::getNeighborIds(int x, int y,
const std::vector<bool> & u_valid,
bool(*is_neighbor)(Gradient, Gradient)
) {
assert(u_valid.size() == 8);
std::vector<int> neighbors;
Gradient current = gradient[id(x,y)];
int index = 0;
for(int j = -1; j <= 1; ++j) {
for(int i = -1; i <= 1; ++i) {
//edge
if(j + y < 0 || j + y >= height) {
index ++;
continue;
}
//edge
if(i + x < 0 || i + x >= width) {
index ++;
continue;
}
//Don't return middle
if(j == 0 && i == 0) {
//Don't increment index
continue;
}
size_t n = id(x+i,y+j);
Gradient t = gradient[n];
if(valid[n] && u_valid[index]
&& is_neighbor(current,t)) {
assert(n >= 0 && n < size());
neighbors.push_back(n);
}
index ++;
}
}
return neighbors;
}
template <typename T>
std::vector<T> Image<T>::getNeighbors(int x, int y,
const std::vector<bool> & u_valid,
bool(*is_neighbor)(T, T)
) {
std::vector<int> neighbors =
getNeighborIds(x,y,u_valid,is_neighbor);
std::vector<T> v_neighbors;
for(size_t i = 0; i < neighbors.size(); ++i) {
v_neighbors.push_back(get(neighbors[i]));
}
return v_neighbors;
}
template <typename T>
std::vector<int> Image<T>::getBlockNeighborIds(int i, int n) {
std::vector<int> neighbors;
int cur_x = getX(i);
int cur_y = getY(i);
for(int y = -n; y <= n; ++y) {
if(cur_y + y < 0 || cur_y + y >= height) {
continue;
}
for(int x = -n; x <= n; ++x) {
//edge
if(cur_x + x < 0 || cur_x + x >= width) {
continue;
}
//Don't return middle
if(cur_y == 0 && cur_x == 0) {
//Don't increment index
continue;
}
neighbors.push_back(id(cur_x + x, cur_y + y));
}
}
return neighbors;
}
template<typename T>
std::vector<int> Image<T>::getRColNeighborIds(int i, int n) {
std::vector<int> neighbors;
int cur_x = getX(i);
int new_x = cur_x + 1;
int cur_y = getY(i);
if( new_x > width) {
return neighbors;
}
for(int j = -n; j <= n; ++j) {
int new_y = cur_y + j;
if(new_y < 0 || new_y >= height) {
continue;
}
neighbors.push_back(id(new_x,new_y));
}
return neighbors;
}
#endif