26 #ifndef VISIONX_KINECTHELPERS_H
27 #define VISIONX_KINECTHELPERS_H
29 #include <Eigen/Geometry>
31 #include <pcl/point_cloud.h>
32 #include <pcl/point_types.h>
34 #include <opencv2/opencv.hpp>
38 #include <libfreenect2/libfreenect2.hpp>
40 template <
typename Po
intT>
44 KinectToPCLHelper(
const libfreenect2::Freenect2Device::IrCameraParams& depth_p,
bool mirror) :
45 mirror_(mirror), qnan_(
std::numeric_limits<
float>::quiet_NaN())
49 float* pm1 = colmap.data();
50 float* pm2 = rowmap.data();
51 for (
int i = 0; i < w; i++)
53 *pm1++ = (i - depth_p.cx + 0.5) / depth_p.fx;
55 for (
int i = 0; i < h; i++)
57 *pm2++ = (i - depth_p.cy + 0.5) / depth_p.fy;
61 typename pcl::PointCloud<PointT>::Ptr
63 libfreenect2::Frame& registered,
64 typename pcl::PointCloud<PointT>::Ptr cloud)
66 const std::size_t w = undistorted.width;
67 const std::size_t h = undistorted.height;
69 cv::Mat tmp_itD0(undistorted.height, undistorted.width, CV_32FC1, undistorted.data);
70 cv::Mat tmp_itRGB0(registered.height, registered.width, CV_8UC4, registered.data);
75 cv::flip(tmp_itD0, tmp_itD0, 1);
76 cv::flip(tmp_itRGB0, tmp_itRGB0, 1);
79 const float* itD0 = (
float*)tmp_itD0.ptr();
80 const char* itRGB0 = (
char*)tmp_itRGB0.ptr();
82 PointT* itP = &cloud->points[0];
85 for (std::size_t y = 0; y < h; ++y)
88 const unsigned int offset = y * w;
89 const float* itD = itD0 + offset;
90 const char* itRGB = itRGB0 + offset * 4;
91 const float dy = rowmap(y);
95 for (std::size_t
x = 0;
x < w; ++
x, ++itP, ++itD, itRGB += 4)
97 const float depth_value = *itD / 1000.0;
100 if (!std::isnan(depth_value) && (
std::abs(depth_value) >= 0.0001))
103 const float rx = colmap(
x) * depth_value;
104 const float ry = dy * depth_value;
105 itP->z = depth_value;
125 cloud->is_dense = is_dense;
142 libfreenect2::Frame& registered,
143 CByteImage** rgbImages)
145 cv::Mat tmp_itD0(undistorted.height, undistorted.width, CV_32FC1, undistorted.data);
146 cv::Mat tmp_itRGB0(registered.height, registered.width, CV_8UC4, registered.data);
155 cv::flip(undistorted, undistorted, 1);
156 cv::flip(registered, registered, 1);
158 for (
int j = 0; j < undistorted.rows; j++)
160 int index = 3 * (j * undistorted.cols);
161 for (
int i = 0; i < undistorted.cols; i++)
163 float value = undistorted.at<
float>(j, i);
166 rgbImages[1]->pixels[
index + 0],
167 rgbImages[1]->pixels[
index + 1],
168 rgbImages[1]->pixels[
index + 2],
171 auto v = registered.at<cv::Vec4b>(j, i);
172 for (
int k = 0; k < 3; k++)
174 rgbImages[0]->pixels[
index + k] =
v[2 - k];
185 #endif //VISIONX_KINECTHELPERS_H