26 #ifndef VISIONX_KINECTHELPERS_H
27 #define VISIONX_KINECTHELPERS_H
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>
36 template<
typename Po
intT>
40 KinectToPCLHelper(
const libfreenect2::Freenect2Device::IrCameraParams& depth_p,
bool mirror) : mirror_(mirror),
41 qnan_(
std::numeric_limits<
float>::quiet_NaN())
45 float* pm1 = colmap.data();
46 float* pm2 = rowmap.data();
47 for (
int i = 0; i < w; i++)
49 *pm1++ = (i - depth_p.cx + 0.5) / depth_p.fx;
51 for (
int i = 0; i < h; i++)
53 *pm2++ = (i - depth_p.cy + 0.5) / depth_p.fy;
57 typename pcl::PointCloud<PointT>::Ptr
59 typename pcl::PointCloud<PointT>::Ptr cloud)
61 const std::size_t w = undistorted.width;
62 const std::size_t h = undistorted.height;
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);
70 cv::flip(tmp_itD0, tmp_itD0, 1);
71 cv::flip(tmp_itRGB0, tmp_itRGB0, 1);
75 const float* itD0 = (
float*) tmp_itD0.ptr();
76 const char* itRGB0 = (
char*) tmp_itRGB0.ptr();
78 PointT* itP = &cloud->points[0];
81 for (std::size_t y = 0; y < h; ++y)
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);
91 for (std::size_t x = 0; x < w; ++x, ++itP, ++itD, itRGB += 4)
93 const float depth_value = *itD / 1000.0;
96 if (!std::isnan(depth_value) && (
std::abs(depth_value) >= 0.0001))
99 const float rx = colmap(x) * depth_value;
100 const float ry = dy * depth_value;
101 itP->z = depth_value;
121 cloud->is_dense = is_dense;
139 void calculateImages(libfreenect2::Frame& undistorted, libfreenect2::Frame& registered, CByteImage** rgbImages)
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);
146 void calculateImages(cv::Mat& undistorted, cv::Mat& registered, CByteImage** rgbImages)
150 cv::flip(undistorted, undistorted, 1);
151 cv::flip(registered, registered, 1);
153 for (
int j = 0; j < undistorted.rows; j++)
155 int index = 3 * (j * undistorted.cols);
156 for (
int i = 0; i < undistorted.cols; i++)
158 float value = undistorted.at<
float>(j, i);
161 rgbImages[1]->pixels[
index + 2],
false);
163 auto v = registered.at<cv::Vec4b>(j, i);
164 for (
int k = 0; k < 3; k++)
166 rgbImages[0]->pixels[
index + k] =
v[2 - k];
177 #endif //VISIONX_KINECTHELPERS_H