KinectHelpers.h
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::KinectV2PointCloudProvider
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 
26 #ifndef VISIONX_KINECTHELPERS_H
27 #define VISIONX_KINECTHELPERS_H
28 
29 #include <libfreenect2/libfreenect2.hpp>
30 #include <Eigen/Geometry>
31 #include <pcl/point_cloud.h>
32 #include <opencv2/opencv.hpp>
33 #include <pcl/point_types.h>
35 
36 template<typename PointT>
38 {
39 public:
40  KinectToPCLHelper(const libfreenect2::Freenect2Device::IrCameraParams& depth_p, bool mirror) : mirror_(mirror),
41  qnan_(std::numeric_limits<float>::quiet_NaN())
42  {
43  const int w = 512;
44  const int h = 424;
45  float* pm1 = colmap.data();
46  float* pm2 = rowmap.data();
47  for (int i = 0; i < w; i++)
48  {
49  *pm1++ = (i - depth_p.cx + 0.5) / depth_p.fx;
50  }
51  for (int i = 0; i < h; i++)
52  {
53  *pm2++ = (i - depth_p.cy + 0.5) / depth_p.fy;
54  }
55  }
56 
57  typename pcl::PointCloud<PointT>::Ptr
58  calculatePointCloud(libfreenect2::Frame& undistorted, libfreenect2::Frame& registered,
59  typename pcl::PointCloud<PointT>::Ptr cloud)
60  {
61  const std::size_t w = undistorted.width;
62  const std::size_t h = undistorted.height;
63  cloud->resize(w * h);
64  cv::Mat tmp_itD0(undistorted.height, undistorted.width, CV_32FC1, undistorted.data);
65  cv::Mat tmp_itRGB0(registered.height, registered.width, CV_8UC4, registered.data);
66 
67  if (mirror_)
68  {
69 
70  cv::flip(tmp_itD0, tmp_itD0, 1);
71  cv::flip(tmp_itRGB0, tmp_itRGB0, 1);
72 
73  }
74 
75  const float* itD0 = (float*) tmp_itD0.ptr();
76  const char* itRGB0 = (char*) tmp_itRGB0.ptr();
77 
78  PointT* itP = &cloud->points[0];
79  bool is_dense = true;
80 
81  for (std::size_t y = 0; y < h; ++y)
82  {
83 
84  const unsigned int offset = y * w;
85  const float* itD = itD0 + offset;
86  const char* itRGB = itRGB0 + offset * 4;
87  const float dy = rowmap(y);
89  // bool label = pcl::traits::has_label<PointT>::value;
90  // bool normal = pcl::traits::has_normal<PointT>::value;
91  for (std::size_t x = 0; x < w; ++x, ++itP, ++itD, itRGB += 4)
92  {
93  const float depth_value = *itD / 1000.0;
94  // auto rgb = tmp_itRGB0.at<cv::Vec4b>(y, x);
95 
96  if (!std::isnan(depth_value) && (std::abs(depth_value) >= 0.0001))
97  {
98 
99  const float rx = colmap(x) * depth_value;
100  const float ry = dy * depth_value;
101  itP->z = depth_value;
102  itP->x = rx;
103  itP->y = ry;
104 
105  if (color)
106  {
107  itP->b = itRGB[0];
108  itP->g = itRGB[1];
109  itP->r = itRGB[2];
110  }
111  }
112  else
113  {
114  itP->z = qnan_;
115  itP->x = qnan_;
116  itP->y = qnan_;
117  is_dense = false;
118  }
119  }
120  }
121  cloud->is_dense = is_dense;
122  return cloud;
123  };
124 
125 private:
128  bool mirror_;
129  float qnan_;
130 
131 };
132 
134 {
135  explicit KinectToIVTHelper(bool mirror) : mirror_(mirror)
136  {
137  };
138 
139  void calculateImages(libfreenect2::Frame& undistorted, libfreenect2::Frame& registered, CByteImage** rgbImages)
140  {
141  cv::Mat tmp_itD0(undistorted.height, undistorted.width, CV_32FC1, undistorted.data);
142  cv::Mat tmp_itRGB0(registered.height, registered.width, CV_8UC4, registered.data);
143  calculateImages(tmp_itD0, tmp_itRGB0, rgbImages);
144  }
145 
146  void calculateImages(cv::Mat& undistorted, cv::Mat& registered, CByteImage** rgbImages)
147  {
148  if (mirror_)
149  {
150  cv::flip(undistorted, undistorted, 1);
151  cv::flip(registered, registered, 1);
152  }
153  for (int j = 0; j < undistorted.rows; j++)
154  {
155  int index = 3 * (j * undistorted.cols);
156  for (int i = 0; i < undistorted.cols; i++)
157  {
158  float value = undistorted.at<float>(j, i);
159 
160  visionx::tools::depthValueToRGB((int)value, rgbImages[1]->pixels[index + 0], rgbImages[1]->pixels[index + 1],
161  rgbImages[1]->pixels[index + 2], false);
162 
163  auto v = registered.at<cv::Vec4b>(j, i);
164  for (int k = 0; k < 3; k++)
165  {
166  rgbImages[0]->pixels[index + k] = v[2 - k];
167  }
168  index += 3;
169  }
170  }
171  }
172 
173 private:
174  bool mirror_;
175 };
176 
177 #endif //VISIONX_KINECTHELPERS_H
KinectToIVTHelper::calculateImages
void calculateImages(cv::Mat &undistorted, cv::Mat &registered, CByteImage **rgbImages)
Definition: KinectHelpers.h:146
index
uint8_t index
Definition: EtherCATFrame.h:59
KinectToPCLHelper::calculatePointCloud
pcl::PointCloud< PointT >::Ptr calculatePointCloud(libfreenect2::Frame &undistorted, libfreenect2::Frame &registered, typename pcl::PointCloud< PointT >::Ptr cloud)
Definition: KinectHelpers.h:58
KinectToIVTHelper::calculateImages
void calculateImages(libfreenect2::Frame &undistorted, libfreenect2::Frame &registered, CByteImage **rgbImages)
Definition: KinectHelpers.h:139
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
visionx::tools::depthValueToRGB
void depthValueToRGB(unsigned int depthInMM, unsigned char &r, unsigned char &g, unsigned char &b, bool noiseResistant=false)
Definition: ImageUtil.h:176
KinectToIVTHelper::KinectToIVTHelper
KinectToIVTHelper(bool mirror)
Definition: KinectHelpers.h:135
armarx::PointT
pcl::PointXYZRGBL PointT
Definition: Common.h:28
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
float
#define float
Definition: 16_Level.h:22
std
Definition: Application.h:66
KinectToIVTHelper
Definition: KinectHelpers.h:133
KinectToPCLHelper
Definition: KinectHelpers.h:37
ImageUtil.h
armarx::control::ethercat::reporting::Severity::Error
@ Error
Eigen::Matrix< float, 512, 1 >
KinectToPCLHelper::KinectToPCLHelper
KinectToPCLHelper(const libfreenect2::Freenect2Device::IrCameraParams &depth_p, bool mirror)
Definition: KinectHelpers.h:40