-
Notifications
You must be signed in to change notification settings - Fork 82
/
kinect.cpp
306 lines (241 loc) · 8.82 KB
/
kinect.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
#include "kfusion.h"
#include "helpers.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "perfstats.h"
using namespace std;
using namespace TooN;
#include <libfreenect.h>
#include <libfreenect-registration.h>
#include <pthread.h>
freenect_context *f_ctx;
freenect_device *f_dev;
bool gotDepth;
int depth_index;
freenect_registration registration;
pthread_t freenect_thread;
volatile bool die = false;
uint16_t * buffers[2];
void depth_cb(freenect_device *dev, void *v_depth, uint32_t timestamp)
{
gotDepth = true;
depth_index = (depth_index+1) % 2;
freenect_set_depth_buffer(dev, buffers[depth_index]);
}
void *freenect_threadfunc(void *arg)
{
while(!die){
int res = freenect_process_events(f_ctx);
if (res < 0 && res != -10) {
cout << "\nError "<< res << " received from libusb - aborting.\n";
break;
}
}
freenect_stop_depth(f_dev);
freenect_stop_video(f_dev);
freenect_close_device(f_dev);
freenect_shutdown(f_ctx);
}
int InitKinect( uint16_t * depth_buffer[2], void * rgb_buffer ){
if (freenect_init(&f_ctx, NULL) < 0) {
cout << "freenect_init() failed" << endl;
return 1;
}
freenect_set_log_level(f_ctx, FREENECT_LOG_WARNING);
freenect_select_subdevices(f_ctx, (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA));
int nr_devices = freenect_num_devices (f_ctx);
cout << "Number of devices found: " << nr_devices << endl;
if (nr_devices < 1)
return 1;
if (freenect_open_device(f_ctx, &f_dev, 0) < 0) {
cout << "Could not open device" << endl;
return 1;
}
depth_index = 0;
buffers[0] = depth_buffer[0];
buffers[1] = depth_buffer[1];
freenect_set_depth_callback(f_dev, depth_cb);
freenect_set_depth_mode(f_dev, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_REGISTERED));
freenect_set_depth_buffer(f_dev, buffers[depth_index]);
// freenect_set_video_callback(f_dev, rgb_cb);
freenect_set_video_mode(f_dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB));
freenect_set_video_buffer(f_dev, rgb_buffer);
freenect_start_depth(f_dev);
freenect_start_video(f_dev);
registration = freenect_copy_registration(f_dev);
gotDepth = false;
int res = pthread_create(&freenect_thread, NULL, freenect_threadfunc, NULL);
if(res){
cout << "error starting kinect thread " << res << endl;
return 1;
}
return 0;
}
float GetFocalLength() {
return registration.zero_plane_info.reference_distance / (2 * registration.zero_plane_info.reference_pixel_size );
}
void CloseKinect(){
die = true;
pthread_join(freenect_thread, NULL);
}
KFusion kfusion;
Image<uchar4, HostDevice> lightScene, depth, lightModel, texModel;
Image<uint16_t, HostDevice> depthImage[2];
Image<uchar3, HostDevice> rgbImage;
const float3 light = make_float3(-2.0, -2.0, 0);
const float3 ambient = make_float3(0.1, 0.1, 0.1);
SE3<float> initPose;
int counter = 0;
int integration_rate = 2;
bool reset = true;
Image<float3, Device> pos, normals;
Image<float, Device> dep;
void display(void){
const uint2 imageSize = kfusion.configuration.inputSize;
static bool integrate = true;
glClear( GL_COLOR_BUFFER_BIT );
const double startFrame = Stats.start();
const double startProcessing = Stats.sample("kinect");
kfusion.setKinectDeviceDepth(depthImage[!depth_index].getDeviceImage());
Stats.sample("raw to cooked");
integrate = kfusion.Track();
Stats.sample("track");
if((integrate && ((counter % integration_rate) == 0)) || reset){
kfusion.Integrate();
Stats.sample("integrate");
reset = false;
}
renderLight( lightModel.getDeviceImage(), kfusion.vertex, kfusion.normal, light, ambient);
renderLight( lightScene.getDeviceImage(), kfusion.inputVertex[0], kfusion.inputNormal[0], light, ambient );
renderTrackResult( depth.getDeviceImage(), kfusion.reduction );
static int count = 4;
if(count > 3){
renderInput( pos, normals, dep, kfusion.integration, toMatrix4(SE3<float>::exp(makeVector(1.0, 1.0, -1.0, 0, 0, 0))) * getInverseCameraMatrix(kfusion.configuration.camera), kfusion.configuration.nearPlane, kfusion.configuration.farPlane, kfusion.configuration.stepSize(), 0.75 * kfusion.configuration.mu);
count = 0;
} else
count++;
renderTexture( texModel.getDeviceImage(), pos, normals, rgbImage.getDeviceImage(), getCameraMatrix(kfusion.configuration.camera * 2) * inverse(kfusion.pose), light);
cudaDeviceSynchronize();
Stats.sample("render");
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0,imageSize.y * 0);
glDrawPixels(lightScene);
glRasterPos2i(imageSize.x, imageSize.y * 0);
glDrawPixels(depth);
glRasterPos2i(0,imageSize.y * 1);
glDrawPixels(lightModel);
glRasterPos2i(imageSize.x, imageSize.y);
glDrawPixels(texModel);
const double endProcessing = Stats.sample("draw");
Stats.sample("total", endProcessing - startFrame, PerfStats::TIME);
Stats.sample("total_proc", endProcessing - startProcessing, PerfStats::TIME);
if(printCUDAError())
exit(1);
++counter;
if(counter % 50 == 0){
Stats.print();
Stats.reset();
cout << endl;
}
glutSwapBuffers();
}
void idle(void){
if(gotDepth){
gotDepth = false;
glutPostRedisplay();
}
}
void keys(unsigned char key, int x, int y){
switch(key){
case 'c':
kfusion.Reset();
kfusion.setPose(toMatrix4(initPose));
reset = true;
break;
case 'q':
exit(0);
break;
}
}
void reshape(int width, int height){
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glColor3f(1.0f,1.0f,1.0f);
glRasterPos2f(-1, 1);
glOrtho(-0.375, width-0.375, height-0.375, -0.375, -1 , 1); //offsets to make (0,0) the top left pixel (rather than off the display)
glPixelZoom(1,-1);
}
void exitFunc(void){
CloseKinect();
kfusion.Clear();
cudaDeviceReset();
}
int main(int argc, char ** argv) {
const float size = (argc > 1) ? atof(argv[1]) : 2.f;
KFusionConfig config;
// it is enough now to set the volume resolution once.
// everything else is derived from that.
// config.volumeSize = make_uint3(64);
// config.volumeSize = make_uint3(128);
config.volumeSize = make_uint3(256);
// these are physical dimensions in meters
config.volumeDimensions = make_float3(size);
config.nearPlane = 0.4f;
config.farPlane = 5.0f;
config.mu = 0.1;
config.combinedTrackAndReduce = false;
// change the following parameters for using 640 x 480 input images
config.inputSize = make_uint2(320,240);
// config.iterations is a vector<int>, the length determines
// the number of levels to be used in tracking
// push back more then 3 iteraton numbers to get more levels.
config.iterations[0] = 10;
config.iterations[1] = 5;
config.iterations[2] = 4;
config.dist_threshold = (argc > 2 ) ? atof(argv[2]) : config.dist_threshold;
config.normal_threshold = (argc > 3 ) ? atof(argv[3]) : config.normal_threshold;
initPose = SE3<float>(makeVector(size/2, size/2, 0, 0, 0, 0));
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize(config.inputSize.x * 2, config.inputSize.y * 2);
glutCreateWindow("kfusion");
kfusion.Init(config);
if(printCUDAError()) {
cudaDeviceReset();
exit(1);
}
kfusion.setPose(toMatrix4(initPose));
lightScene.alloc(config.inputSize), depth.alloc(config.inputSize), lightModel.alloc(config.inputSize), texModel.alloc(config.inputSize);
depthImage[0].alloc(make_uint2(640, 480));
depthImage[1].alloc(make_uint2(640, 480));
rgbImage.alloc(make_uint2(640, 480));
memset(depthImage[0].data(), 0, depthImage[0].size.x*depthImage[0].size.y * sizeof(uint16_t));
memset(depthImage[1].data(), 0, depthImage[1].size.x*depthImage[1].size.y * sizeof(uint16_t));
memset(rgbImage.data(), 0, rgbImage.size.x*rgbImage.size.y * sizeof(uchar3));
pos.alloc(config.inputSize), normals.alloc(config.inputSize), dep.alloc(config.inputSize);
uint16_t * buffers[2] = {depthImage[0].data(), depthImage[1].data()};
if(InitKinect(buffers, rgbImage.data())){
cudaDeviceReset();
exit(1);
}
// get focal length from Kinect
const float focal_length = GetFocalLength();
config.camera = make_float4(focal_length/2, focal_length/2, 640/4, 480/4);
atexit(exitFunc);
glutDisplayFunc(display);
glutKeyboardFunc(keys);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMainLoop();
CloseKinect();
return 0;
}