1 #ifdef LICORNEA_WITH_LIBFREENECT2 9 using namespace libfreenect2;
11 int grabber::freenect2_frame_types_()
const {
13 if(has_(color) || has_(registered_color)) types |= Frame::Color;
14 if(has_(ir)) types |= Frame::Ir;
15 if(has_(depth) || has_(bigdepth) || has_(registered_color)) types |= Frame::Depth;
20 frame_types_(frame_types),
23 pipeline_(new CpuPacketPipeline()),
24 listener_(freenect2_frame_types_()),
26 undistorted_depth_(512, 424, 4),
27 undistorted_ir_(512, 424, 4),
28 registered_color_(512, 424, 4),
29 bigdepth_(1920, 1082, 4)
31 setGlobalLogger(
nullptr);
33 int device_count = context_.enumerateDevices();
34 if(device_count == 0)
throw std::runtime_error(
"Kinect not found");
36 std::string serial = context_.getDefaultDeviceSerialNumber();
37 device_ = context_.openDevice(serial, pipeline_);
38 if(! device_)
throw std::runtime_error(
"could not open device");
40 std::cout <<
"Kinect serial number: " << serial << std::endl;
42 if(has_(color) || has_(registered_color))
43 device_->setColorFrameListener(&listener_);
44 if(has_(registered_color) || has_(depth) || has_(depth) || has_(bigdepth) || has_(ir))
45 device_->setIrAndDepthFrameListener(&listener_);
47 bool ok = device_->start();
48 if(! ok)
throw std::runtime_error(
"could not start device");
50 Freenect2Device::ColorCameraParams color = device_->getColorCameraParams();
51 Freenect2Device::IrCameraParams ir = device_->getIrCameraParams();
52 registration_ = std::make_unique<Registration>(ir, color);
58 if(! released_) listener_.release(frames_);
59 registration_.reset();
66 const int max_wait_ms = 5000;
67 bool ok = listener_.waitForNewFrame(frames_, max_wait_ms);
68 if(! ok)
return false;
71 if(has_(registered_color) || has_(bigdepth)) {
72 Frame* raw_color = frames_[Frame::Color];
73 Frame* raw_depth = frames_[Frame::Depth];
74 registration_->apply(raw_color, raw_depth, &undistorted_depth_, ®istered_color_,
true, &bigdepth_);
75 }
else if(has_(depth)) {
76 Frame* raw_depth = frames_[Frame::Depth];
77 registration_->undistortDepth(raw_depth, &undistorted_depth_);
80 Frame* raw_ir = frames_[Frame::Ir];
81 registration_->undistortDepth(raw_ir, &undistorted_ir_);
89 if(! released_) listener_.release(frames_);
97 Frame* raw_color = frames_[Frame::Color];
98 cv::Mat_<cv::Vec4b> color_orig(1080, 1920, reinterpret_cast<cv::Vec4b*>(raw_color->data));
99 cv::Mat_<cv::Vec3b> color;
100 cv::cvtColor(color_orig, color, CV_BGRA2BGR);
106 assert(has_(registered_color));
107 cv::Mat_<cv::Vec4b> color_orig(424, 512, reinterpret_cast<cv::Vec4b*>(registered_color_.data));
108 cv::Mat_<cv::Vec3b> color;
109 cv::cvtColor(color_orig, color, CV_BGRA2BGR);
116 Frame* raw_ir = frames_[Frame::Ir];
117 cv::Mat_<float> ir_orig(424, 512, reinterpret_cast<float*>(undistorted ? undistorted_ir_.data : raw_ir->data));
118 float alpha = 255.0f / (max_ir - min_ir);
119 float beta = -alpha * min_ir;
121 cv::convertScaleAbs(ir_orig, ir, alpha, beta);
122 ir.setTo(0, (ir_orig < min_ir));
123 ir.setTo(255, (ir_orig > max_ir));
124 ir.setTo(0, (ir_orig == 0));
131 Frame* raw_ir = frames_[Frame::Ir];
132 cv::Mat_<float> ir_orig(424, 512, reinterpret_cast<float*>(undistorted ? undistorted_ir_.data : raw_ir->data));
139 Frame* raw_depth = frames_[Frame::Depth];
140 cv::Mat_<float> depth = cv::Mat_<float>(424, 512,
reinterpret_cast<float*
>(undistorted ? undistorted_depth_.data : raw_depth->data));
146 cv::Mat_<float> depth_orig(1082, 1920, reinterpret_cast<float*>(bigdepth_.data));
147 return depth_orig.rowRange(1, 1081);
152 auto color = device_->getColorCameraParams();
153 auto ir = device_->getIrCameraParams();
cv::Mat_< uchar > get_ir_frame(float min_ir=0, float max_ir=0xffff, bool undistorted=false)
cv::Mat_< cv::Vec3b > get_color_frame()
kinect_internal_parameters from_freenect2(const freenect2_color_params &, const freenect2_ir_params &)
kinect_internal_parameters internal_parameters()
cv::Mat_< ushort > get_original_ir_frame(bool undistorted=false)
cv::Mat_< cv::Vec3b > get_registered_color_frame()
cv::Mat_< float > get_depth_frame(bool undistorted=false)
cv::Mat_< float > get_bigdepth_frame()