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
39using namespace armarx;
40using namespace pcl;
41using namespace cv;
42
43namespace visionx
44{
45 void
54
55 void
64
65 void
74
75 void
85
86 void
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
103 {
104 device_->startVideo();
105 device_->startDepth();
106 }
107
108 void
110 {
112 ARMARX_INFO << "Stopping KinectV1 Grabber Interface";
113 device_->stopVideo();
114 device_->stopDepth();
115 ARMARX_INFO << "Stopped KinectV1 Grabber Interface";
116 }
117
118 void
120 {
122 ARMARX_VERBOSE << "onExitCapturingPointCloudProvider()";
123
124 delete rgbImages_[0];
125 delete rgbImages_[1];
126
127 delete[] rgbImages_;
128 }
129
130 void
132 {
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);
146 }
147
148 bool
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 }
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
200
201 StereoCalibration
203 {
204 return calibration;
205 }
206
207} // namespace visionx
uint8_t index
constexpr T c
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
virtual void onExitComponent()
Hook for subclass.
virtual void onDisconnectComponent()
Hook for subclass.
virtual void onConnectComponent()=0
Pure virtual hook for the subclass.
virtual void onInitComponent()=0
Pure virtual hook for the subclass.
std::atomic_bool captureEnabled
Indicates that capturing is enabled and running.
void onInitComponent() override
void onDisconnectComponent() override
Hook for subclass.
void setImageFormat(ImageDimension imageDimension, ImageType imageType, BayerPatternType bayerPatternType=visionx::eBayerPatternRg)
Sets the image basic format data.
void provideImages(void **inputBuffers, const IceUtil::Time &imageTimestamp=IceUtil::Time())
send images raw.
void onConnectComponent() override
void setNumberImages(int numberImages)
Sets the number of images on each capture.
void onExitComponent() override
void onInitComponent() override
Pure virtual hook for the subclass.
void onDisconnectComponent() override
Hook for subclass.
void onConnectComponent() override
Pure virtual hook for the subclass.
StereoCalibration getStereoCalibration(const Ice::Current &c=Ice::emptyCurrent) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onExitComponent() override
Hook for subclass.
void providePointCloud(PointCloudPtrT pointCloudPtr)
offer the new point cloud.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition helper.h:35
void depthValueToRGB(unsigned int depthInMM, unsigned char &r, unsigned char &g, unsigned char &b, bool noiseResistant=false)
Definition ImageUtil.h:183
ArmarX headers.
#define ARMARX_TRACE
Definition trace.h:77