uvmap.cpp
Go to the documentation of this file.
1
4 #include "uvmap.h"
5 #include <limits>
6 #include "debug.h"
7
8 namespace librealsense {
9 namespace algo {
10 namespace depth_to_rgb_calibration {
11
12
13  static void transform_point_to_uv( double pixel[2], const p_matrix & pmat, const double point[3] )
14  {
15  auto p = pmat.vals;
16  // pmat * point
17  double x = p[0] * point[0] + p[1] * point[1] + p[2] * point[2] + p[3];
18  double y = p[4] * point[0] + p[5] * point[1] + p[6] * point[2] + p[7];
19  double z = p[8] * point[0] + p[9] * point[1] + p[10] * point[2] + p[11];
20
21  pixel[0] = x / z;
22  pixel[1] = y / z;
23  }
24
25
26  /* Given a point in 3D space, compute the corresponding pixel coordinates in an image with no distortion or forward distortion coefficients produced by the same camera */
27  static void distort_pixel( double pixel[2], const rs2_intrinsics_double * intrin, const double point[2] )
28  {
29  double x = (point[0] - intrin->ppx) / intrin->fx;
30  double y = (point[1] - intrin->ppy) / intrin->fy;
31
32  if( intrin->model == RS2_DISTORTION_BROWN_CONRADY )
33  {
34  double r2 = x * x + y * y;
35  double r2c = 1 + intrin->coeffs[0] * r2 + intrin->coeffs[1] * r2*r2 + intrin->coeffs[4] * r2*r2*r2;
36
37  double xcd = x * r2c;
38  double ycd = y * r2c;
39
40  double dx = xcd + 2 * intrin->coeffs[2] * x*y + intrin->coeffs[3] * (r2 + 2 * x*x);
41  double dy = ycd + 2 * intrin->coeffs[3] * x*y + intrin->coeffs[2] * (r2 + 2 * y*y);
42
43  x = dx;
44  y = dy;
45  }
46
47  pixel[0] = x * (double)intrin->fx + (double)intrin->ppx;
48  pixel[1] = y * (double)intrin->fy + (double)intrin->ppy;
49  }
50
51
53  std::vector< double3 > const & points,
54  const calib & cal,
55  const p_matrix & p_mat
56  )
57  {
58  auto intrinsics = cal.get_intrinsics();
59
60  std::vector< double2 > uv_map( points.size() );
61
62  for( auto i = 0; i < points.size(); ++i )
63  {
64  double2 uv;
65  transform_point_to_uv( &uv.x, p_mat, &points[i].x );
66
67  double2 uvmap;
68  distort_pixel( &uvmap.x, &intrinsics, &uv.x );
69  uv_map[i] = uvmap;
70  }
71
72  return uv_map;
73  }
74
75
76  std::vector< double > biliniar_interp(
77  std::vector< double > const & vals,
78  size_t width,
79  size_t height,
80  uvmap_t const & uv
81  )
82  {
83  std::vector< double > res( uv.size() );
84
85  for( auto i = 0; i < uv.size(); i++ )
86  {
87  auto x = uv[i].x;
88  auto x1 = floor( x );
89  auto x2 = ceil( x );
90  auto y = uv[i].y;
91  auto y1 = floor( y );
92  auto y2 = ceil( y );
93
94  if( x1 < 0 || x1 >= width || x2 < 0 || x2 >= width ||
95  y1 < 0 || y1 >= height || y2 < 0 || y2 >= height )
96  {
97  res[i] = std::numeric_limits<double>::max();
98  continue;
99  }
100
101  // x1 y1 x2 y1
102  // x1 y2 x2 y2
103
104  // top_l top_r
105  // bot_l bot_r
106
107  auto top_l = vals[int( y1*width + x1 )];
108  auto top_r = vals[int( y1*width + x2 )];
109  auto bot_l = vals[int( y2*width + x1 )];
110  auto bot_r = vals[int( y2*width + x2 )];
111
112  double interp_x_top, interp_x_bot;
113  if( x1 == x2 )
114  {
115  interp_x_top = top_l;
116  interp_x_bot = bot_l;
117  }
118  else
119  {
120  interp_x_top = ((double)(x2 - x) / (double)(x2 - x1))*(double)top_l + ((double)(x - x1) / (double)(x2 - x1))*(double)top_r;
121  interp_x_bot = ((double)(x2 - x) / (double)(x2 - x1))*(double)bot_l + ((double)(x - x1) / (double)(x2 - x1))*(double)bot_r;
122  }
123
124  if( y1 == y2 )
125  {
126  res[i] = interp_x_bot;
127  continue;
128  }
129
130  auto interp_y_x = ((double)(y2 - y) / (double)(y2 - y1))*(double)interp_x_top + ((double)(y - y1) / (double)(y2 - y1))*(double)interp_x_bot;
131  res[i] = interp_y_x;
132  }
133
134 #if 0
135  std::ofstream f;
136  f.open( "interp_y_x" );
137  f.precision( 16 );
138  for( auto i = 0; i < res.size(); i++ )
139  {
140  f << res[i] << std::endl;
141  }
142  f.close();
143 #endif
144  return res;
145  }
146
147
148 }
149 }
150 }
GLint y
std::vector< double2 > uvmap_t
Definition: uvmap.h:22
GLfloat GLfloat p
Definition: glext.h:12687
GLdouble GLdouble GLdouble y2
rs2_intrinsics_double get_intrinsics() const
Definition: calibration.cpp:55
static void distort_pixel(double pixel[2], const rs2_intrinsics_double *intrin, const double point[2])
Definition: uvmap.cpp:27
rs2_intrinsics intrin
GLdouble GLdouble z
point
Definition: rmse.py:166
GLdouble f
static void transform_point_to_uv(double pixel[2], const p_matrix &pmat, const double point[3])
Definition: uvmap.cpp:13
GLdouble x
GLdouble GLdouble x2
GLint GLsizei GLsizei height
std::vector< double > biliniar_interp(std::vector< double > const &vals, size_t width, size_t height, uvmap_t const &uv)
Definition: uvmap.cpp:76
dictionary intrinsics
Definition: t265_stereo.py:142
GLuint GLfloat GLfloat GLfloat x1
Definition: glext.h:9721
uvmap_t get_texture_map(std::vector< double3 > const &points, const calib &cal, const p_matrix &p_mat)
Definition: uvmap.cpp:52
int i
GLuint res
Definition: glext.h:8856
GLdouble y1
GLint GLsizei width

librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:13