AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
depthCameraSupport.cpp
1 #include "header/depthCameraSupport.h"
2 
4  float ppx;
5  float ppy;
6  float fx;
7  float fy;
8  float coeffs[5];
9 };
10 
11 void DCAM::rs_deproject_pixel_to_point(float point[3], const rs_intrinsics &intrin,
12  const float pixel[2], float depth) {
13 
14  float x = (pixel[0] - intrin.ppx) / intrin.fx;
15  float y = (pixel[1] - intrin.ppy) / intrin.fy;
16  float r2 = x * x + y * y;
17  float f = 1 + intrin.coeffs[0] * r2 + intrin.coeffs[1] * r2 * r2 + intrin.coeffs[4] * r2 * r2 * r2;
18  float ux = x * f + 2 * intrin.coeffs[2] * x * y + intrin.coeffs[3] * (r2 + 2 * x * x);
19  float uy = y * f + 2 * intrin.coeffs[3] * x * y + intrin.coeffs[2] * (r2 + 2 * y * y);
20  x = ux;
21  y = uy;
22  point[0] = depth * x;
23  point[1] = depth * y;
24  point[2] = depth;
25 }
26 
27 
28 bool DCAM::makePointCloud(vpImage<uint16_t> &I_depth_raw,
29  pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud){
30 
31  unsigned int height = I_depth_raw.getRows();
32  unsigned int width = I_depth_raw.getCols();
33 
34  // Transform pointcloud
35  pointcloud->width = width;
36  pointcloud->height = height;
37  pointcloud->reserve((size_t)width * height);
38  // Params of depth camera. They are in the scene.xml
39  const float depth_scale = 1.0f;
40  rs_intrinsics depth_intrinsic;
41  depth_intrinsic.ppx = 120.0f;
42  depth_intrinsic.ppy = 160.0f;
43  depth_intrinsic.fx = 257.986f;
44  depth_intrinsic.fy = 257.341f;
45  depth_intrinsic.coeffs[0] = 0.0f;
46  depth_intrinsic.coeffs[1] = 0.0f;
47  depth_intrinsic.coeffs[2] = 0.0f;
48  depth_intrinsic.coeffs[3] = 0.0f;
49  depth_intrinsic.coeffs[4] = 0.0f;
50  for (unsigned int i = 0; i < height; i++) {
51  for (unsigned int j = 0; j < width; j++) {
52  float scaled_depth = I_depth_raw[i][j] * depth_scale;
53  float point[3];
54  float pixel[2] = {(float)j, (float)i};
55  rs_deproject_pixel_to_point(point, depth_intrinsic, pixel, scaled_depth);
56  pointcloud->push_back(pcl::PointXYZ(point[0], point[1], point[2]));
57  }
58  }
59  return true;
60 }
61 
62 
63 bool DCAM::makePointCloud(cv::Mat I_depth_raw,
64  pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud){
65 
66  unsigned int height = I_depth_raw.rows;
67  unsigned int width = I_depth_raw.cols;
68 
69  // Transform pointcloud
70  pointcloud->width = width;
71  pointcloud->height = height;
72  pointcloud->reserve((size_t)width * height);
73  // Params of depth camera. They are in the scene.xml
74  const float depth_scale = 1.0f;
75  rs_intrinsics depth_intrinsic;
76  //depth_intrinsic.ppx = 120.0f;
77  //depth_intrinsic.ppy = 160.0f;
78  //depth_intrinsic.fx = 257.986f;
79  //depth_intrinsic.fy = 257.341f;
80  depth_intrinsic.ppx = 160.0f;
81  depth_intrinsic.ppy = 120.0f;
82  depth_intrinsic.fx = 257.34082279179285f;
83  depth_intrinsic.fy = 257.34083046114705f;
84  depth_intrinsic.coeffs[0] = 0.0f;
85  depth_intrinsic.coeffs[1] = 0.0f;
86  depth_intrinsic.coeffs[2] = 0.0f;
87  depth_intrinsic.coeffs[3] = 0.0f;
88  depth_intrinsic.coeffs[4] = 0.0f;
89  for (unsigned int i = 0; i < height; i++) {
90  for (unsigned int j = 0; j < width; j++) {
91  float scaled_depth = I_depth_raw.at<float>(i,j) * depth_scale;
92  float point[3];
93  float pixel[2] = {(float)j, (float)i};
94  rs_deproject_pixel_to_point(point, depth_intrinsic, pixel, scaled_depth);
95  pointcloud->push_back(pcl::PointXYZ(point[0], point[1], point[2]));
96  }
97  }
98  return true;
99 }