KinectV1PointCloudProvider.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX::ArmarXObjects::KinectV1PointCloudProvider
19  * @author Christoph Pohl (christoph dot pohl at kit dot edu)
20  * @date 2019
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 
27 
28 #include <pcl/io/pcd_io.h>
29 
31 
34 
35 #include <Calibration/Calibration.h>
36 #include <Image/ImageProcessor.h>
37 
38 
39 using namespace armarx;
40 using namespace pcl;
41 using namespace cv;
42 
43 namespace visionx
44 {
45  void
46  KinectV1PointCloudProvider::onInitComponent()
47  {
49  ARMARX_VERBOSE << "onInitComponent()";
50 
51  ImageProvider::onInitComponent();
52  CapturingPointCloudProvider::onInitComponent();
53  }
54 
55  void
56  KinectV1PointCloudProvider::onConnectComponent()
57  {
59  ARMARX_VERBOSE << "onConnectComponent()";
60 
61  ImageProvider::onConnectComponent();
62  CapturingPointCloudProvider::onConnectComponent();
63  }
64 
65  void
66  KinectV1PointCloudProvider::onDisconnectComponent()
67  {
69  // hack to prevent deadlock
70  captureEnabled = false;
71  ImageProvider::onDisconnectComponent();
72  CapturingPointCloudProvider::onDisconnectComponent();
73  }
74 
75  void
76  KinectV1PointCloudProvider::onExitComponent()
77  {
79  ARMARX_VERBOSE << "onExitComponent()";
80 
81  ImageProvider::onExitComponent();
82  CapturingPointCloudProvider::onExitComponent();
83  ARMARX_INFO << "Resetting grabber interface";
84  }
85 
86  void
87  KinectV1PointCloudProvider::onInitCapturingPointCloudProvider()
88  {
90 
91 
92  ARMARX_VERBOSE << "onInitCapturingPointCloudProvider()";
93  // from https://jasonchu1313.github.io/2017/10/01/kinect-calibration/
94  cx_ = 343.65;
95  cy_ = 229.81;
96  fx_ = 458.46;
97  fy_ = 458.20;
98  device_ = &freenect_.createDevice<KinectV1Device>(0);
99  }
100 
101  void
102  KinectV1PointCloudProvider::onStartCapture(float frameRate)
103  {
104  device_->startVideo();
105  device_->startDepth();
106  }
107 
108  void
109  KinectV1PointCloudProvider::onStopCapture()
110  {
111  ARMARX_TRACE;
112  ARMARX_INFO << "Stopping KinectV1 Grabber Interface";
113  device_->stopVideo();
114  device_->stopDepth();
115  ARMARX_INFO << "Stopped KinectV1 Grabber Interface";
116  }
117 
118  void
119  KinectV1PointCloudProvider::onExitCapturingPointCloudProvider()
120  {
121  ARMARX_TRACE;
122  ARMARX_VERBOSE << "onExitCapturingPointCloudProvider()";
123 
124  delete rgbImages_[0];
125  delete rgbImages_[1];
126 
127  delete[] rgbImages_;
128  }
129 
130  void
131  KinectV1PointCloudProvider::onInitImageProvider()
132  {
133  ARMARX_TRACE;
134  // init images
135  width_ = 640;
136  height_ = 480;
137  rgbImages_ = new CByteImage*[2];
138  size_ = width_ * height_;
139  rgbImages_[0] = new CByteImage(width_, height_, CByteImage::eRGB24);
140  rgbImages_[1] = new CByteImage(width_, height_, CByteImage::eRGB24);
141  ::ImageProcessor::Zero(rgbImages_[0]);
142  ::ImageProcessor::Zero(rgbImages_[1]);
143  Eigen::Vector2i dimensions = Eigen::Vector2i(width_, height_);
144  setImageFormat(visionx::ImageDimension(dimensions(0), dimensions(1)), visionx::eRgb);
145  setNumberImages(2);
146  }
147 
148  bool
149  KinectV1PointCloudProvider::doCapture()
150  {
151  static std::vector<uint8_t> rgb(size_ * 3);
152  static std::vector<uint16_t> depth(size_);
153  // float f = 595.f;
154  if (!device_->getRGB(rgb) || device_->getDepth(depth))
155  {
156  return false;
157  };
158  pcl::PointCloud<PointT>::Ptr outputCloud =
159  pcl::PointCloud<PointT>::Ptr(new pcl::PointCloud<PointT>());
160  outputCloud->resize(size_);
161  for (int i = 0; i < size_; i++)
162  {
163  // color
164  int index = 3 * i;
165  for (int j = 0; j < 3; j++)
166  {
167  rgbImages_[0]->pixels[index + j] = rgb[index + j];
168  }
169  visionx::tools::depthValueToRGB((int)depth[i],
170  rgbImages_[1]->pixels[index + 0],
171  rgbImages_[1]->pixels[index + 1],
172  rgbImages_[1]->pixels[index + 2],
173  false);
174  // X = (x - cx) * d / fx
175  // Y = (y - cy) * d / fy
176  // Z = d
177  // TODO: precalculate some of this stuff for efficiency
178  // outputCloud->points[i].x = (i % width_ - (width_ - 1) / 2.f) * depth[i] / f;
179  // outputCloud->points[i].y = (i / width_ - (height_ - 1) / 2.f) * depth[i] / f;
180  outputCloud->points[i].x = (i % width_ - cx_ + 0.5) * depth[i] / fx_;
181  outputCloud->points[i].y = (i / width_ - cy_ + 0.5) * depth[i] / fy_;
182  outputCloud->points[i].z = depth[i];
183  outputCloud->points[i].r = rgb[index];
184  outputCloud->points[i].g = rgb[index + 1];
185  outputCloud->points[i].b = rgb[index + 2];
186  }
187  IceUtil::Time ts = IceUtil::Time::now();
188  outputCloud->header.stamp = ts.toMicroSeconds();
189  providePointCloud<PointT>(outputCloud);
190  provideImages(rgbImages_, ts);
191  return true;
192  }
193 
195  KinectV1PointCloudProvider::createPropertyDefinitions()
196  {
197  return PropertyDefinitionsPtr(
198  new KinectV1PointCloudProviderPropertyDefinitions(getConfigIdentifier()));
199  }
200 
201  StereoCalibration
202  KinectV1PointCloudProvider::getStereoCalibration(const Ice::Current& c)
203  {
204  return calibration;
205  }
206 
207 } // namespace visionx
pcl
Definition: pcl_point_operators.cpp:3
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
index
uint8_t index
Definition: EtherCATFrame.h:59
KinectV1PointCloudProvider.h
trace.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
KinectV1Device
Definition: KinectV1Device.hpp:14
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
visionx::tools::depthValueToRGB
void depthValueToRGB(unsigned int depthInMM, unsigned char &r, unsigned char &g, unsigned char &b, bool noiseResistant=false)
Definition: ImageUtil.h:183
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
visionx::KinectV1PointCloudProviderPropertyDefinitions
Definition: KinectV1PointCloudProvider.h:54
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
IceUtil::Handle< class PropertyDefinitionContainer >
ImageUtil.h
cv
Definition: helper.h:34
TypeMapping.h
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27