-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfreenect_grabber.hpp
246 lines (210 loc) · 8.71 KB
/
freenect_grabber.hpp
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
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <libfreenect/libfreenect.hpp>
#include <stdio.h>
#include <iostream>
#ifdef HAVE_OMP
#include "omp.h"
#endif // HAVE_OMP
class Mutex {
public:
Mutex() {
pthread_mutex_init( &m_mutex, NULL );
}
void lock() {
pthread_mutex_lock( &m_mutex );
}
void unlock() {
pthread_mutex_unlock( &m_mutex );
}
private:
pthread_mutex_t m_mutex;
};
class MyFreenectDevice : public Freenect::FreenectDevice {
public:
std::vector<uint16_t> m_buffer_depth;
std::vector<uint8_t> m_buffer_video;
std::vector<uint16_t> m_gamma;
Mutex m_rgb_mutex;
Mutex m_depth_mutex;
bool m_new_rgb_frame;
bool m_new_depth_frame;
uint16_t getDepthBufferSize16()
{
return getDepthBufferSize()/2;
}
MyFreenectDevice(freenect_context *_ctx, int _index)
: Freenect::FreenectDevice(_ctx, _index), m_buffer_depth(getDepthBufferSize()),m_buffer_video(getVideoBufferSize()), m_gamma(2048), m_new_rgb_frame(false), m_new_depth_frame(false)
{
for( unsigned int i = 0 ; i < 2048 ; i++) {
float v = i/2048.0;
v = std::pow(v, 3)* 6;
m_gamma[i] = v*6*256;
}
}
// Do not call directly even in child
void VideoCallback(void* _rgb, uint32_t timestamp) {
//std::cout << "RGB callback" << std::endl;
m_rgb_mutex.lock();
uint8_t* rgb = static_cast<uint8_t*>(_rgb);
std::copy(rgb, rgb+getVideoBufferSize(), m_buffer_video.begin());
m_new_rgb_frame = true;
m_rgb_mutex.unlock();
};
// Do not call directly even in child
void DepthCallback(void* _depth, uint32_t timestamp) {
//std::cout << "Depth callback" << std::endl;
/*
m_depth_mutex.lock();
uint16_t* depth = static_cast<uint16_t*>(_depth);
for( unsigned int i = 0 ; i < 640*480 ; i++) {
int pval = m_gamma[depth[i]];
int lb = pval & 0xff;
switch (pval>>8) {
case 0:
m_buffer_depth[3*i+0] = 255;
m_buffer_depth[3*i+1] = 255-lb;
m_buffer_depth[3*i+2] = 255-lb;
break;
case 1:
m_buffer_depth[3*i+0] = 255;
m_buffer_depth[3*i+1] = lb;
m_buffer_depth[3*i+2] = 0;
break;
case 2:
m_buffer_depth[3*i+0] = 255-lb;
m_buffer_depth[3*i+1] = 255;
m_buffer_depth[3*i+2] = 0;
break;
case 3:
m_buffer_depth[3*i+0] = 0;
m_buffer_depth[3*i+1] = 255;
m_buffer_depth[3*i+2] = lb;
break;
case 4:
m_buffer_depth[3*i+0] = 0;
m_buffer_depth[3*i+1] = 255-lb;
m_buffer_depth[3*i+2] = 255;
break;
case 5:
m_buffer_depth[3*i+0] = 0;
m_buffer_depth[3*i+1] = 0;
m_buffer_depth[3*i+2] = 255-lb;
break;
default:
m_buffer_depth[3*i+0] = 0;
m_buffer_depth[3*i+1] = 0;
m_buffer_depth[3*i+2] = 0;
break;
}
}
m_new_depth_frame = true;
m_depth_mutex.unlock();
*/
m_depth_mutex.lock();
uint16_t* depth = static_cast<uint16_t*>(_depth);
// was getVideoBufferSize()
std::copy(depth, depth+getDepthBufferSize(), m_buffer_depth.begin());
m_new_depth_frame = true;
m_depth_mutex.unlock();
}
bool getRGB(std::vector<uint8_t> &buffer) {
m_rgb_mutex.lock();
if(m_new_rgb_frame) {
buffer.swap(m_buffer_video);
m_new_rgb_frame = false;
m_rgb_mutex.unlock();
return true;
} else {
m_rgb_mutex.unlock();
return false;
}
}
bool getDepth(std::vector<uint16_t> &buffer) {
m_depth_mutex.lock();
if(m_new_depth_frame) {
buffer.swap(m_buffer_depth);
m_new_depth_frame = false;
m_depth_mutex.unlock();
return true;
} else {
m_depth_mutex.unlock();
return false;
}
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename PointT>
class freenectGrabber
{
public:
Freenect::Freenect myfreenect;
MyFreenectDevice* device;
std::vector<uint16_t> depth_map;
std::vector<uint8_t> rgb;
freenectGrabber(){
depth_map.resize(640*480*4);
rgb.resize(640*480*4);
freenect_video_format requested_format = FREENECT_VIDEO_RGB;
device = &myfreenect.createDevice<MyFreenectDevice>(0);
device->setDepthFormat(FREENECT_DEPTH_REGISTERED);
device->setVideoFormat(requested_format);
device->startDepth();
device->startVideo();
}
~freenectGrabber(){
device->stopVideo();
device->stopDepth();
}
typename pcl::PointCloud<PointT>::Ptr
get_point_cloud(int distance, bool colored)
{
//get rgb and depth data
while(!device -> getDepth(depth_map)){}
while(!device -> getRGB(rgb)){}
int depth_width = 640;
int depth_height = 480;
//create the empty Pointcloud
boost::shared_ptr<pcl::PointCloud<PointT>> cloud (new pcl::PointCloud<PointT>);
//initialize the PointCloud height and width
//cloud->height = std::max (image_height, depth_height);
//cloud->width = std::max (image_width, depth_width);
//allow infinite values for points coordinates
cloud->is_dense = false;
//set camera parameters for kinect
double focal_x_depth = 585.187492217609;//5.9421434211923247e+02;
double focal_y_depth = 585.308616340665;//5.9104053696870778e+02;
double center_x_depth = 322.714077555293;//3.3930780975300314e+02;
double center_y_depth = 248.626108676666;//2.4273913761751615e+02;
float bad_point = std::numeric_limits<float>::quiet_NaN ();
#pragma omp parallel for
for (unsigned int y = 0; y < depth_height; ++y)
for ( unsigned int x = 0; x < depth_width; ++x){
PointT ptout;
uint16_t dz = depth_map[y*depth_width + x];
if (abs(dz) < distance){
// project
Eigen::Vector3d ptd((x - center_x_depth) * dz / focal_x_depth, (y - center_y_depth) * dz/focal_y_depth, dz);
// assign output xyz
ptout.x = ptd.x()*0.001f;
ptout.y = ptd.y()*0.001f;
ptout.z = ptd.z()*0.001f;
if(colored){
uint8_t r = rgb[(y*depth_width + x)*3];
uint8_t g = rgb[(y*depth_width + x)*3 + 1];
uint8_t b = rgb[(y*depth_width + x)*3 + 2];
ptout.rgba = pcl::PointXYZRGB(r, g, b).rgba; //assign color
//ptout.rgba = pcl::PointXYZRGB(0, 0, 0).rgba;
} else
ptout.rgba = pcl::PointXYZRGB(0, 0, 0).rgba;
#pragma omp critical
cloud->points.push_back(ptout); //assigns point to cloud
}
}
cloud->height = 1;
cloud->width = cloud->points.size();
return (cloud);
}
};