17 #include <mrpt/otherlibs/do_opencv_includes.h> 34 const std::vector<mrpt::math::TPoint3D> &in_points_3D,
37 std::vector<TPixelCoordf> &projectedPoints,
38 bool accept_points_behind )
43 static const std::vector<double> distortion_dummy(4,0);
51 accept_points_behind );
59 const std::vector<mrpt::math::TPoint3D> &in_points_3D,
62 const std::vector<double> & distortionParams,
63 std::vector<mrpt::utils::TPixelCoordf> &projectedPoints,
64 bool accept_points_behind
72 ASSERT_(distortionParams.size()==4 || distortionParams.size()==5);
74 const size_t N = in_points_3D.size();
75 projectedPoints.resize(N);
79 vector<CvPoint3D64f> objPoints(N);
82 for (
size_t i=0;i<N;i++)
84 in_points_3D[i].x,in_points_3D[i].y,in_points_3D[i].z,
85 objPoints[i].x,objPoints[i].y,objPoints[i].z
89 static double rotation_matrix[] = {1,0,0, 0,1,0, 0,0,1 };
90 static double translation_vector[] = {0,0,0 };
97 proj_matrix[0] = intrinsicParams.get_unsafe(0,0);
98 proj_matrix[4] = intrinsicParams.get_unsafe(1,1);
99 proj_matrix[2] = intrinsicParams.get_unsafe(0,2);
100 proj_matrix[5] = intrinsicParams.get_unsafe(1,2);
103 cv::Mat object_points = cv::Mat( N, 1, CV_64FC3, &objPoints[0] );
106 cv::Rodrigues( cv::Mat( 3, 3, CV_64FC1, rotation_matrix ), rotvec);
108 cv::Mat _translation_vector = cv::Mat( 3, 1, CV_64FC1, translation_vector );
109 cv::Mat camera_matrix = cv::Mat( 3, 3, CV_64FC1, &proj_matrix[0] );
110 cv::Mat dist_coeffs = cv::Mat( 5, 1, CV_64FC1, const_cast<double*>(&distortionParams[0]) );
112 vector<cv::Point2d> image_points;
115 object_points, rotvec, _translation_vector,
116 camera_matrix, dist_coeffs, image_points );
118 for (
size_t i=0;i<N;i++)
120 if (accept_points_behind || objPoints[i].
z>0 )
122 projectedPoints[i].x = image_points[i].x;
123 projectedPoints[i].y = image_points[i].y;
127 projectedPoints[i].x = -1;
128 projectedPoints[i].y = -1;
133 THROW_EXCEPTION(
"Function not available: MRPT was compiled without OpenCV")
143 const std::vector<mrpt::utils::TPixelCoordf> &in_dist_pixels,
144 std::vector<mrpt::utils::TPixelCoordf> &out_pixels,
146 const std::vector<double> & Dk )
151 for (
size_t i=0;i<cam.
dist.static_size;i++) cam.
dist[i] = Dk[i];
156 const std::vector<mrpt::utils::TPixelCoordf> &in_dist_pixels,
157 std::vector<mrpt::utils::TPixelCoordf> &out_pixels,
164 const size_t n = in_dist_pixels.size();
165 out_pixels.resize(
n);
167 const double fx = cameraModel.
fx();
168 const double fy = cameraModel.
fy();
169 const double ifx = 1./fx;
170 const double ify = 1./fy;
171 const double cx = cameraModel.
cx();
172 const double cy = cameraModel.
cy();
174 for(
size_t i = 0; i <
n; i++ )
176 double x = in_dist_pixels[i].x;
177 double y = in_dist_pixels[i].y;
179 double x0 =
x = (
x - cx)*ifx;
180 double y0 =
y = (
y - cy)*ify;
183 for(
unsigned int j = 0; j < 5; j++ )
185 double r2 =
x*
x +
y*
y;
186 double icdist = 1./(1 + ((cameraModel.
dist[4]*r2 + cameraModel.
dist[1])*r2 + cameraModel.
dist[0])*r2);
187 double deltaX = 2*cameraModel.
dist[2]*
x*
y + cameraModel.
dist[3]*(r2 + 2*
x*
x);
188 double deltaY = cameraModel.
dist[2]*(r2 + 2*
y*
y) + 2*cameraModel.
dist[3]*
x*
y;
189 x = (x0 - deltaX)*icdist;
190 y = (y0 - deltaY)*icdist;
194 out_pixels[i].x =
x*fx + cx;
195 out_pixels[i].y =
y*fy + cy;
215 const double fx = cameraModel.
fx();
216 const double fy = cameraModel.
fy();
217 const double ifx = 1./fx;
218 const double ify = 1./fy;
219 const double cx = cameraModel.
cx();
220 const double cy = cameraModel.
cy();
225 double x0 =
x = (
x - cx)*ifx;
226 double y0 =
y = (
y - cy)*ify;
229 for(
unsigned int j = 0; j < 5; j++ )
231 double r2 =
x*
x +
y*
y;
232 double icdist = 1./(1 + ((cameraModel.
dist[4]*r2 + cameraModel.
dist[1])*r2 + cameraModel.
dist[0])*r2);
233 double deltaX = 2*cameraModel.
dist[2]*
x*
y + cameraModel.
dist[3]*(r2 + 2*
x*
x);
234 double deltaY = cameraModel.
dist[2]*(r2 + 2*
y*
y) + 2*cameraModel.
dist[3]*
x*
y;
235 x = (x0 - deltaX)*icdist;
236 y = (y0 - deltaY)*icdist;
248 const std::vector<mrpt::math::TPoint3D> &P,
251 std::vector<mrpt::utils::TPixelCoordf> &
pixels,
252 bool accept_points_behind
257 pixels.resize( P.size() );
261 for( itPoints = P.begin(), itPixels =
pixels.begin(); itPoints != P.end(); ++itPoints, ++itPixels, ++k )
268 const double x = nP.
x/nP.
z;
269 const double y = nP.
y/nP.
z;
273 const double r4 =
square(r2);
274 const double r6 = r2*r4;
276 const double B = 2*
x*
y;
277 if( A > 0 && (accept_points_behind || nP.
z > 0) )
299 bool accept_points_behind
304 const double x = P.
x/P.
z;
305 const double y = P.
y/P.
z;
309 const double r4 =
square(r2);
310 const double r6 = r2*r4;
A pair (x,y) of pixel coordinates (subpixel resolution).
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
#define THROW_EXCEPTION(msg)
double cy() const
Get the value of the principal point y-coordinate (in pixels).
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
void VISION_IMPEXP projectPoints_with_distortion(const std::vector< mrpt::math::TPoint3D > &in_points_3D, const mrpt::poses::CPose3D &cameraPose, const mrpt::math::CMatrixDouble33 &intrinsicParams, const std::vector< double > &distortionParams, std::vector< mrpt::utils::TPixelCoordf > &projectedPoints, bool accept_points_behind=false)
Project a set of 3D points into a camera at an arbitrary 6D pose using its calibration matrix and dis...
const Scalar * const_iterator
double z
X,Y,Z coordinates.
T square(const T x)
Inline function for the square of a number.
double fx() const
Get the value of the focal length x-value (in pixels).
double fy() const
Get the value of the focal length y-value (in pixels).
This base provides a set of functions for maths stuff.
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
Functions related to pinhole camera models, point projections, etc.
This namespace contains representation of robot actions and observations.
void inverseComposePoint(const double gx, const double gy, const double gz, double &lx, double &ly, double &lz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=NULL, mrpt::math::CMatrixFixedNumeric< double, 3, 7 > *out_jacobian_df_dpose=NULL) const
Computes the 3D point L such as .
Classes for computer vision, detectors, features, etc.
void VISION_IMPEXP projectPoints_no_distortion(const std::vector< mrpt::math::TPoint3D > &in_points_3D, const mrpt::poses::CPose3D &cameraPose, const mrpt::math::CMatrixDouble33 &intrinsicParams, std::vector< mrpt::utils::TPixelCoordf > &projectedPoints, bool accept_points_behind=false)
Project a set of 3D points into a camera at an arbitrary 6D pose using its calibration matrix (undist...
A class used to store a 3D pose as a translation (x,y,z) and a quaternion (qr,qx,qy,qz).
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
double cx() const
Get the value of the principal point x-coordinate (in pixels).
mrpt::math::CMatrixDouble33 intrinsicParams
Matrix of intrinsic parameters (containing the focal length and principal point coordinates) ...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void inverseComposePoint(const double gx, const double gy, const double gz, double &lx, double &ly, double &lz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=NULL, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dpose=NULL, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dse3=NULL) const
Computes the 3D point L such as .
void VISION_IMPEXP projectPoint_with_distortion(const mrpt::math::TPoint3D &in_point_wrt_cam, const mrpt::utils::TCamera &in_cam_params, mrpt::utils::TPixelCoordf &out_projectedPoints, bool accept_points_behind=false)
Project one 3D point into a camera using its calibration matrix and distortion parameters (radial and...
void VISION_IMPEXP undistort_point(const mrpt::utils::TPixelCoordf &inPt, mrpt::utils::TPixelCoordf &outPt, const mrpt::utils::TCamera &cameraModel)
Undistort one point given by its pixel coordinates and the camera parameters.
mrpt::math::CArrayDouble< 5 > dist
[k1 k2 t1 t2 k3] -> k_i: parameters of radial distortion, t_i: parameters of tangential distortion (d...
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
void VISION_IMPEXP undistort_points(const std::vector< mrpt::utils::TPixelCoordf > &srcDistortedPixels, std::vector< mrpt::utils::TPixelCoordf > &dstUndistortedPixels, const mrpt::math::CMatrixDouble33 &intrinsicParams, const std::vector< double > &distortionParams)
Undistort a list of points given by their pixel coordinates, provided the camera matrix and distortio...
GLenum const GLfloat * params
Structure to hold the parameters of a pinhole camera model.