31 #include <Image/ImageProcessor.h>
32 #include <Calibration/Calibration.h>
34 #include <pcl/io/pcd_io.h>
45 void KinectV2PointCloudProvider::onInitComponent()
50 ImageProvider::onInitComponent();
51 CapturingPointCloudProvider::onInitComponent();
54 void KinectV2PointCloudProvider::onConnectComponent()
59 ImageProvider::onConnectComponent();
60 CapturingPointCloudProvider::onConnectComponent();
63 void KinectV2PointCloudProvider::onDisconnectComponent()
67 captureEnabled =
false;
68 ImageProvider::onDisconnectComponent();
69 CapturingPointCloudProvider::onDisconnectComponent();
72 void KinectV2PointCloudProvider::onExitComponent()
77 ImageProvider::onExitComponent();
78 CapturingPointCloudProvider::onExitComponent();
82 void KinectV2PointCloudProvider::onInitCapturingPointCloudProvider()
85 enable_rgb_ = getProperty<bool>(
"EnableRGB").getValue();
86 enable_depth_ = getProperty<bool>(
"EnableDepth").getValue();
87 mirror_ = getProperty<bool>(
"Mirror").getValue();
88 serial_ = getProperty<std::string>(
"Serial").getValue();
89 pipeline_type_ = getProperty<KinectProcessorType>(
"Pipeline").getValue();
102 switch (pipeline_type_)
105 pipeline_ =
new libfreenect2::CpuPacketPipeline();
108 pipeline_ =
new libfreenect2::OpenGLPacketPipeline();
111 pipeline_ =
new libfreenect2::OpenCLPacketPipeline(deviceId_);
114 pipeline_ =
new libfreenect2::OpenCLKdePacketPipeline(deviceId_);
117 pipeline_ =
new libfreenect2::CudaPacketPipeline(deviceId_);
120 pipeline_ =
new libfreenect2::CudaKdePacketPipeline(deviceId_);
123 pipeline_ =
new libfreenect2::CpuPacketPipeline();
128 if (!enable_rgb_ && !enable_depth_)
131 ARMARX_ERROR <<
"Either depth or rgb needs to be enabled";
133 if (freenect2_.enumerateDevices() == 0)
141 serial_ = freenect2_.getDefaultDeviceSerialNumber();
146 dev_ = freenect2_.openDevice(serial_, pipeline_);
151 dev_ = freenect2_.openDevice(serial_);
160 rgbImages_ =
new CByteImage*[2];
162 rgbImages_[0] =
new CByteImage(d_width_, d_height_, CByteImage::eRGB24);
163 rgbImages_[1] =
new CByteImage(d_width_, d_height_, CByteImage::eRGB24);
164 ::ImageProcessor::Zero(rgbImages_[0]);
165 ::ImageProcessor::Zero(rgbImages_[1]);
170 void KinectV2PointCloudProvider::onStartCapture(
float frameRate)
172 if (enable_rgb_ && enable_depth_)
183 if (!dev_->startStreams(enable_rgb_, enable_depth_))
188 libfreenect2::Freenect2Device::IrCameraParams depth_p = dev_->getIrCameraParams();
189 libfreenect2::Freenect2Device::ColorCameraParams rgb_p = dev_->getColorCameraParams();
191 dev_->setColorFrameListener(&listener_);
192 dev_->setIrAndDepthFrameListener(&listener_);
195 initializeCameraCalibration();
200 void KinectV2PointCloudProvider::onStopCapture()
203 ARMARX_INFO <<
"Stopping KinectV2 Grabber Interface";
209 ARMARX_INFO <<
"Stopped KinectV2 Grabber Interface";
213 void KinectV2PointCloudProvider::onExitCapturingPointCloudProvider()
218 delete rgbImages_[0];
219 delete rgbImages_[1];
221 delete [] rgbImages_;
225 void KinectV2PointCloudProvider::onInitImageProvider()
228 Eigen::Vector2i dimensions = Eigen::Vector2i(512, 424);
229 setImageFormat(visionx::ImageDimension(dimensions(0), dimensions(1)), visionx::eRgb);
234 bool KinectV2PointCloudProvider::doCapture()
236 if (!listener_.waitForNewFrame(frames_, 1 * 1000))
242 pcl::PointCloud<PointT>::Ptr outputCloud = pcl::PointCloud<PointT>::Ptr(
new pcl::PointCloud<PointT>());
247 libfreenect2::Frame* depth = frames_[libfreenect2::Frame::Depth];
248 if (enable_rgb_ && enable_depth_)
250 registration_->apply(rgb, depth, &undistorted_, ®istered_,
true);
252 ivtHelper->calculateImages(undistorted_, registered_, rgbImages_);
253 pclHelper->calculatePointCloud(undistorted_, registered_, outputCloud);
255 outputCloud->header.stamp = ts.toMicroSeconds();
256 outputCloud->header.seq = depth->sequence;
257 providePointCloud<PointT>(outputCloud);
259 provideImages(rgbImages_, ts);
260 listener_.release(frames_);
270 libfreenect2::Freenect2Device::IrCameraParams KinectV2PointCloudProvider::getCameraIrParameters()
272 libfreenect2::Freenect2Device::IrCameraParams ir = dev_->getIrCameraParams();
276 libfreenect2::Freenect2Device::ColorCameraParams KinectV2PointCloudProvider::getCameraRGBParameters()
278 libfreenect2::Freenect2Device::ColorCameraParams rgb = dev_->getColorCameraParams();
282 StereoCalibration KinectV2PointCloudProvider::getStereoCalibration(
const Ice::Current&
c)
287 void KinectV2PointCloudProvider::initializeCameraCalibration()
289 visionx::CameraParameters RGBCameraIntrinsics;
290 RGBCameraIntrinsics.focalLength.resize(2, 0.0f);
291 RGBCameraIntrinsics.principalPoint.resize(2, 0.0f);
292 RGBCameraIntrinsics.distortion.resize(4, 0.0f);
297 auto rgb_params = getCameraRGBParameters();
301 RGBCameraIntrinsics.principalPoint[0] = rgb_params.cx;
302 RGBCameraIntrinsics.principalPoint[1] = rgb_params.cy;
303 RGBCameraIntrinsics.focalLength[0] = rgb_params.fx;
304 RGBCameraIntrinsics.focalLength[1] = rgb_params.fy;
305 RGBCameraIntrinsics.width = rgb_width_;
306 RGBCameraIntrinsics.height = rgb_height_;
309 visionx::CameraParameters depthCameraIntrinsics;
310 depthCameraIntrinsics.focalLength.resize(2, 0.0f);
311 depthCameraIntrinsics.principalPoint.resize(2, 0.0f);
312 depthCameraIntrinsics.distortion.resize(4, 0.0f);
316 auto d_params = getCameraIrParameters();
317 depthCameraIntrinsics.principalPoint[0] = d_params.cx;
318 depthCameraIntrinsics.principalPoint[1] = d_params.cy;
319 depthCameraIntrinsics.focalLength[0] = d_params.fx;
320 depthCameraIntrinsics.focalLength[1] = d_params.fy;
321 depthCameraIntrinsics.width = d_width_;
322 depthCameraIntrinsics.height = d_height_;
329 calibration.calibrationLeft.cameraParam = RGBCameraIntrinsics;
330 calibration.calibrationRight.cameraParam = depthCameraIntrinsics;