25 #include "VideoSource_Kinect.hh"
27 #include <Base/Image/ImageIO.hh>
37 Freenect::Freenect *VideoSource_Kinect::kinectManager_ = NULL;
38 int VideoSource_Kinect::deviceCount_ = 0;
39 int VideoSource_Kinect::openDevices_ = 0;
42 VideoSource_Kinect::VideoSource_Kinect() {
50 VideoSource_Kinect::~VideoSource_Kinect() {
52 if (openDevices_ != 0) {
55 if (openDevices_ == 0) {
58 kinectManager_ = NULL;
62 int VideoSource_Kinect::
68 int VideoSource_Kinect::
69 OpenDevice(
int device)
71 if (openDevices_ > 0 && device==0) {
72 BIASWARN(
"Kinect: I guess you try to open the next device (device=0 given)");
73 device = openDevices_;
74 BIASWARN(
"Kinect: Trying to open device " << device <<
" instead");
76 if (kinectManager_ == NULL) {
78 kinectManager_ =
new Freenect::Freenect();
79 deviceCount_ = kinectManager_->deviceCount();
80 }
catch (std::runtime_error e) {
81 BIASERR(
"Kinect Error: " << e.what());
87 }
catch (std::runtime_error e) {
88 BIASERR(
"Kinect Error: " << e.what());
91 if (kinectDevice_ == NULL) {
92 BIASERR(
"Kinect Error: Device can not be opened");
95 CreateDepthLookupTable();
101 int VideoSource_Kinect::CloseDevice() {
102 kinectManager_->deleteDevice(deviceId_);
104 kinectDevice_ = NULL;
109 int VideoSource_Kinect::PreGrab() {
110 cout <<
"pregrab device " << deviceId_ << endl;
111 BIASASSERT(kinectDevice_ != NULL)
113 kinectDevice_->setDepthFormat(FREENECT_DEPTH_11BIT);
114 kinectDevice_->setVideoFormat(FREENECT_VIDEO_BAYER);
115 }
catch (std::runtime_error e) {
116 BIASERR(
"Kinect Error: " << e.what());
120 kinectDevice_->startDepth();
121 }
catch (std::runtime_error e) {
122 BIASERR(
"Kinect Error: " << e.what());
126 kinectDevice_->startVideo();
127 }
catch (std::runtime_error e) {
128 BIASERR(
"Kinect Error: " << e.what());
135 int VideoSource_Kinect::PostGrab() {
136 BIASASSERT(kinectDevice_ != NULL)
138 kinectDevice_->stopVideo();
139 kinectDevice_->stopDepth();
140 }
catch (std::runtime_error e) {
141 BIASERR(
"Kinect Error: " << e.what());
147 int VideoSource_Kinect::
151 BIASWARN(
"Kinect Error: image already inited");
154 Image.
Init(640,480,1);
159 int VideoSource_Kinect::
163 BIASWARN(
"Kinect Error: image already inited");
166 Image.
Init(640,480,1,ImageBase::ST_float);
173 BIASASSERT(kinectDevice_ != NULL)
174 return kinectDevice_->GetLatestColor(image);
177 int VideoSource_Kinect::CreateDepthLookupTable(
double px,
double py,
double fx,
double fy) {
178 BIASASSERT(kinectDevice_ != NULL)
179 kinectDevice_->CreateLookupTable(px,py,fx,fy);
184 BIASASSERT(kinectDevice_ != NULL)
185 return kinectDevice_->GetLatestDepth(image);
188 int VideoSource_Kinect::SetVideoModeColor() {
189 BIASASSERT(kinectDevice_ != NULL)
190 kinectDevice_->SetSwitchVideoModeAfterGrab(
false);
191 return kinectDevice_->SetVideoMode(FREENECT_VIDEO_BAYER);
194 int VideoSource_Kinect::SetVideoModeIR() {
195 BIASASSERT(kinectDevice_ != NULL)
196 kinectDevice_->SetSwitchVideoModeAfterGrab(
false);
197 return kinectDevice_->SetVideoMode(FREENECT_VIDEO_IR_8BIT);
201 BIASASSERT(kinectDevice_ != NULL)
202 kinectDevice_->SetDepthMode(mode);
206 int VideoSource_Kinect::SetSwitchVideoModeAfterGrab() {
207 return kinectDevice_->SetSwitchVideoModeAfterGrab(
true);
210 int VideoSource_Kinect::SetTilt(
double degrees) {
211 BIASASSERT(kinectDevice_ != NULL)
214 BIASWARN(
"Kinect: clipping angle to -30");
218 BIASWARN(
"Kinect: clipping angle to 30");
221 kinectDevice_->setTiltDegrees(degrees);
222 }
catch (std::runtime_error e) {
223 BIASERR(
"Kinect Error: " << e.what());
229 int VideoSource_Kinect::SetLed(
int mode) {
230 BIASASSERT(kinectDevice_ != NULL)
232 kinectDevice_->setLed((freenect_led_options)mode);
233 }
catch (std::runtime_error e) {
234 BIASERR(
"Kinect Error: " << e.what());
240 int VideoSource_Kinect::GetAccelerometer(
double &tilt,
double &x,
double &y,
double &z) {
241 BIASASSERT(kinectDevice_ != NULL)
243 kinectDevice_->updateState();
244 Freenect::FreenectTiltState state = kinectDevice_->getState();
245 tilt = state.getTiltDegs();
246 state.getAccelerometers(&x, &y, &z);
247 }
catch (std::runtime_error e) {
248 BIASERR(
"Kinect Error: " << e.what());
264 int VideoSource_Kinect::ToggleDepthStream() {
265 BIASERR(
"ToggleDepthStream not implemented for libfreenect");
273 VideoSource_Kinect_Callback::
274 VideoSource_Kinect_Callback(freenect_context *_ctx,
int _index)
275 : Freenect::FreenectDevice(_ctx, _index)
299 for (
int i=0; i<2048; i++) {
300 const float k1 = 1.1863f;
301 const float k2 = 2842.5f;
302 const float k3 = 0.1236f;
303 const float depth = k3 * tanf(i/k2 + k1);
331 uint16_t *pDepth = (uint16_t*)depth;
333 u_int16_t *pDepth = (u_int16_t*)depth;
339 for (
unsigned int x=0;x<640*480;x++) {
340 buf[x] =
lookup_[ pDepth[x] ] * lut[x];
345 for (
unsigned int x=0;x<640*480;x++) {
351 for (
unsigned int x=0;x<640*480;x++) {
357 BIASERR(
"unknown mode")
375 if (
format_ == FREENECT_VIDEO_BAYER) {
397 memset(idaDst,0,3*
sizeof(
float));
398 memcpy(idaDst+3,
depthbuffer_[idx].GetImageData(), (480*640-3)*
sizeof(
float));
409 setVideoFormat(format);
411 }
catch (std::runtime_error e) {
412 BIASERR(
"Kinect Error: " << e.what());
447 for(
unsigned y=0;y<480;y++){
448 for(
unsigned x=0;x<640;x++){
449 float alpha = atan(
float((x-px)/fx));
450 float beta = atan(
float((py-y)/ sqrt( pow((x-px)*(fy/fx),2) + pow(fy,2))));
451 ida[y][x] = 1.0f / (cos(beta) * cos(alpha));
Bayer_GRBG, 1 channel RGB image Bayer tile.
virtual int SetVideoMode(freenect_video_format format)
std::vector< Camera< float > > depthbuffer_
pthread_mutex_t * color_mutex
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
void CopyIn_NoInit(void *data)
Take some data and fill it into the Image.
Implementing FreenectDevice Interface, this class is used internally by VideoSource_Kinect - do not u...
virtual void VideoCallback(void *video, uint32_t timestamp)
libfreenect thread calls this when new color data is available
void SetColorModel(EColorModel Model)
virtual void DepthCallback(void *depth, uint32_t timestamp)
libfreenect thread calls this when new depth data is available
freenect_video_format format_
std::vector< Camera< unsigned char > > colorbuffer_
virtual int GetLatestDepth(Camera< float > &image)
Callbacks store image data in double buffer, get latest Note that the depth image is shifted by 3 pix...
virtual void SetDepthMode(int mode)
virtual int GetLatestColor(Camera< unsigned char > &image)
Callbacks store image data in double buffer, get latest.
The image template class for specific storage types.
pthread_cond_t * newColorAvail_
void CreateLookupTable(double px, double py, double fx, double fy)
Image< float > depthLookupTable_
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
pthread_mutex_t * depthavail_mutex
void Init(unsigned int width, unsigned int height, unsigned int nChannels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
Initialize image size and channels.
Checks for VideoSource capabilities.
virtual int SetSwitchVideoModeAfterGrab(bool switchMode)
pthread_mutex_t * coloravail_mutex
This is the base class for images in BIAS.
pthread_mutex_t * depth_mutex
pthread_cond_t * newDepthAvail_
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase