14 #include <mrpt/otherlibs/do_opencv_includes.h> 23 CStereoRectifyMap::CStereoRectifyMap()
25 m_resize_output(false),
26 m_enable_both_centers_coincide(false),
27 m_resize_output_value(0, 0),
49 bool enable,
unsigned int target_width,
unsigned int target_height)
71 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200 80 const cv::Size trg_size =
111 cv::Mat _mapx_left = cv::cvarrToMat(&mapx_left,
false);
112 cv::Mat _mapy_left = cv::cvarrToMat(&mapy_left,
false);
113 cv::Mat _mapx_right = cv::cvarrToMat(&mapx_right,
false);
114 cv::Mat _mapy_right = cv::cvarrToMat(&mapy_right,
false);
124 for (
unsigned int i = 0; i < 3; ++i)
125 for (
unsigned int j = 0; j < 3; ++j) m1[i][j] = hMatrix(i, j);
128 double rcTrans[3] = {hMatrix(0, 3), hMatrix(1, 3), hMatrix(2, 3)};
130 double ipl[3][3], ipr[3][3], dpl[5], dpr[5];
131 for (
unsigned int i = 0; i < 3; ++i)
132 for (
unsigned int j = 0; j < 3; ++j)
134 ipl[i][j] = cam1.intrinsicParams(i, j);
138 for (
unsigned int i = 0; i < 5; ++i)
140 dpl[i] = cam1.dist[i];
141 dpr[i] = cam2.
dist[i];
144 const cv::Mat
R(3, 3, CV_64F, &m1);
145 const cv::Mat T(3, 1, CV_64F, &rcTrans);
147 const cv::Mat K1(3, 3, CV_64F, ipl);
148 const cv::Mat K2(3, 3, CV_64F, ipr);
149 const cv::Mat D1(1, 5, CV_64F, dpl);
150 const cv::Mat D2(1, 5, CV_64F, dpr);
152 double _R1[3][3], _R2[3][3], _P1[3][4], _P2[3][4], _Q[4][4];
153 cv::Mat R1(3, 3, CV_64F, _R1);
154 cv::Mat R2(3, 3, CV_64F, _R2);
155 cv::Mat P1(3, 4, CV_64F, _P1);
156 cv::Mat P2(3, 4, CV_64F, _P2);
157 cv::Mat Q(4, 4, CV_64F, _Q);
159 const cv::Size img_size(ncols, nrows);
160 const cv::Size real_trg_size =
196 #if MRPT_OPENCV_VERSION_NUM < 0x210 199 K1, D1, K2, D2, img_size,
R, T, R1, R2, P1, P2, Q,
201 #elif MRPT_OPENCV_VERSION_NUM < 0x230 204 K1, D1, K2, D2, img_size,
R, T, R1, R2, P1, P2, Q,
m_alpha,
211 K1, D1, K2, D2, img_size,
R, T, R1, R2, P1, P2, Q,
218 cv::initUndistortRectifyMap(
219 K1, D1, R1, P1, real_trg_size, CV_16SC2, _mapx_left, _mapy_left);
220 cv::initUndistortRectifyMap(
221 K2, D2, R2, P2, real_trg_size, CV_16SC2, _mapx_right, _mapy_right);
224 for (
unsigned int i = 0; i < 3; ++i)
225 for (
unsigned int j = 0; j < 3; ++j)
245 params.leftCamera.focalLengthMeters;
247 params.rightCamera.focalLengthMeters;
251 const Eigen::Map<Eigen::Matrix3d> R1e(R1.ptr<
double>());
252 const Eigen::Map<Eigen::Matrix3d> R2e(R2.ptr<
double>());
275 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200 279 const CvSize trg_size =
282 : cvSize(ncols, nrows);
285 trg_size.width, trg_size.height, in_left_image.
isColor() ? 3 : 1,
288 trg_size.width, trg_size.height, in_left_image.
isColor() ? 3 : 1,
291 const IplImage* in_left = in_left_image.
getAs<IplImage>();
292 const IplImage* in_right = in_right_image.
getAs<IplImage>();
294 IplImage* out_left = out_left_image.
getAs<IplImage>();
295 IplImage* out_right = out_right_image.
getAs<IplImage>();
297 this->
rectify_IPL(in_left, in_right, out_left, out_right);
306 const bool use_internal_mem_cache)
const 310 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200 314 const CvSize trg_size =
317 : cvSize(ncols, nrows);
319 const IplImage* in_left = left_image.
getAs<IplImage>();
320 const IplImage* in_right = right_image.
getAs<IplImage>();
322 IplImage *out_left_image, *out_right_image;
323 if (use_internal_mem_cache)
326 trg_size.width, trg_size.height, left_image.
isColor() ? 3 : 1,
329 trg_size.width, trg_size.height, right_image.
isColor() ? 3 : 1,
338 cvCreateImage(trg_size, in_left->depth, in_left->nChannels);
340 cvCreateImage(trg_size, in_right->depth, in_right->nChannels);
343 this->
rectify_IPL(in_left, in_right, out_left_image, out_right_image);
345 if (use_internal_mem_cache)
350 trg_size.width, trg_size.height, left_image.
isColor() ? 3 : 1,
353 trg_size.width, trg_size.height, right_image.
isColor() ? 3 : 1,
356 cvCopy(out_left_image, left_image.
getAs<IplImage>());
357 cvCopy(out_right_image, right_image.
getAs<IplImage>());
378 const bool use_internal_mem_cache)
const 386 use_internal_mem_cache);
406 const void* srcImg_left,
const void* srcImg_right,
void* outImg_left,
407 void* outImg_right)
const 410 ASSERT_(srcImg_left != outImg_left && srcImg_right != outImg_right);
414 "Error: setFromCamParams() must be called prior to rectify().")
416 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200 425 const CvMat mapx_left = cvMat(
426 nrows_out, ncols_out, CV_16SC2,
428 const CvMat mapy_left = cvMat(
429 nrows_out, ncols_out, CV_16UC1,
431 const CvMat mapx_right = cvMat(
432 nrows_out, ncols_out, CV_16SC2,
434 const CvMat mapy_right = cvMat(
435 nrows_out, ncols_out, CV_16UC1,
438 const cv::Mat mapx1 = cv::cvarrToMat(&mapx_left);
439 const cv::Mat mapy1 = cv::cvarrToMat(&mapy_left);
440 const cv::Mat mapx2 = cv::cvarrToMat(&mapx_right);
441 const cv::Mat mapy2 = cv::cvarrToMat(&mapy_right);
443 const cv::Mat src1 = cv::cvarrToMat(srcImg_left);
444 const cv::Mat src2 = cv::cvarrToMat(srcImg_right);
445 cv::Mat dst1 = cv::cvarrToMat(outImg_left);
446 cv::Mat dst2 = cv::cvarrToMat(outImg_right);
450 cv::BORDER_CONSTANT, cvScalarAll(0));
453 cv::BORDER_CONSTANT, cvScalarAll(0));
482 const std::vector<int16_t>& left_x,
const std::vector<uint16_t>& left_y,
483 const std::vector<int16_t>& right_x,
const std::vector<uint16_t>& right_y)
497 std::vector<int16_t>& left_x, std::vector<uint16_t>& left_y,
498 std::vector<int16_t>& right_x, std::vector<uint16_t>& right_y)
bool hasImageRight
Whether imageRight actually contains data (Default upon construction: true)
mrpt::img::CImage m_cache1
Memory caches for in-place rectification speed-up.
GLclampf GLclampf GLclampf alpha
mrpt::img::TStereoCamera m_camera_params
A copy of the data provided by the user.
const T * getAs() const
Returns a pointer to a const T* containing the image - the idea is to call like "img.getAs<IplImage>()" so we can avoid here including OpenCV's headers.
const mrpt::img::TStereoCamera & getRectifiedImageParams() const
After computing the rectification maps, this method retrieves the calibration parameters of the recti...
void internal_invalidate()
mrpt::img::CImage imageLeft
Image from the left camera (this image will be ALWAYS present)
bool m_enable_both_centers_coincide
void resize(unsigned int width, unsigned int height, TImageChannels nChannels, bool originTopLeft)
Changes the size of the image, erasing previous contents (does NOT scale its current content...
#define THROW_EXCEPTION(msg)
mrpt::img::TStereoCamera m_rectified_image_params
Resulting images params.
void setRectifyMaps(const std::vector< int16_t > &left_x, const std::vector< uint16_t > &left_y, const std::vector< int16_t > &right_x, const std::vector< uint16_t > &right_y)
Direct input access to rectify maps.
void setFromCamParams(const mrpt::img::TStereoCamera ¶ms)
Prepares the mapping from the intrinsic, distortion and relative pose parameters of a stereo camera...
void setRotationMatrix(const mrpt::math::CMatrixDouble33 &ROT)
Sets the 3x3 rotation matrix.
bool isSet() const
Returns true if setFromCamParams() has been already called, false otherwise.
mrpt::poses::CPose3DQuat cameraPose
The pose of the LEFT camera, relative to the robot.
mrpt::math::CMatrixDouble33 intrinsicParams
Matrix of intrinsic parameters (containing the focal length and principal point coordinates) ...
#define ASSERT_(f)
Defines an assertion mechanism.
double focalLengthMeters
The focal length of the camera, in meters (can be used among 'intrinsicParams' to determine the pixel...
A numeric matrix of compile-time fixed size.
This base provides a set of functions for maths stuff.
mrpt::img::TInterpolationMethod m_interpolation_method
void rectify_IPL(const void *in_left_image, const void *in_right_image, void *out_left_image, void *out_right_image) const
Just like rectify() but directly works with OpenCV's "IplImage*", which must be passed as "void*" to ...
void rectify(const mrpt::img::CImage &in_left_image, const mrpt::img::CImage &in_right_image, mrpt::img::CImage &out_left_image, mrpt::img::CImage &out_right_image) const
Rectify the input image pair and save the result in a different output images - setFromCamParams() mu...
void setAlpha(double alpha)
Sets the alpha parameter which controls the zoom in/out of the rectified images, such that: ...
const mrpt::img::TCamera & getRectifiedLeftImageParams() const
Just like getRectifiedImageParams() but for the left camera only.
Observation class for either a pair of left+right or left+disparity images from a stereo camera...
Classes for computer vision, detectors, features, etc.
Structure to hold the parameters of a pinhole camera model.
A class used to store a 3D pose as a translation (x,y,z) and a quaternion (qr,qx,qy,qz).
void enableBothCentersCoincide(bool enable=true)
If enabled (default=false), the principal points in both output images will coincide.
std::vector< int16_t > m_dat_mapx_left
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
void setStereoCameraParams(const mrpt::img::TStereoCamera &in_params)
Sets leftCamera, rightCamera and rightCameraPose from a TStereoCamera structure.
mrpt::math::CArrayDouble< 3 > m_coords
The translation vector [x,y,z].
std::array< double, 5 > dist
[k1 k2 t1 t2 k3] -> k_i: parameters of radial distortion, t_i: parameters of tangential distortion (d...
mrpt::poses::CPose3DQuat m_rot_left
The rotation applied to the left/right camera so their virtual image plane is the same after rectific...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
bool isColor() const
Returns true if the image is RGB, false if it is grayscale.
bool isOriginTopLeft() const
Returns true if the coordinates origin is top-left, or false if it is bottom-left.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
mrpt::img::CImage m_cache2
mrpt::img::TImageSize m_resize_output_value
mrpt::poses::CPose3DQuat rightCameraPose
The pose of the right camera, relative to the left one: Note that using the conventional reference co...
const mrpt::img::TCamera & getRectifiedRightImageParams() const
Just like getRectifiedImageParams() but for the right camera only.
TCamera leftCamera
Intrinsic and distortion parameters of the left and right cameras.
void getInverseHomogeneousMatrix(MATRIX44 &out_HM) const
Returns the corresponding 4x4 inverse homogeneous transformation matrix for this point or pose...
mrpt::math::TPose3DQuat rightCameraPose
Pose of the right camera with respect to the coordinate origin of the left camera.
std::vector< int16_t > m_dat_mapx_right
A quaternion, which can represent a 3D rotation as pair , with a real part "r" and a 3D vector ...
std::vector< uint16_t > m_dat_mapy_left
void enableResizeOutput(bool enable, unsigned int target_width=0, unsigned int target_height=0)
If enabled, the computed maps will rectify images to a size different than their original size...
unsigned __int32 uint32_t
GLenum const GLfloat * params
uint32_t ncols
Camera resolution.
mrpt::poses::CPose3DQuat m_rot_right
void setFromIplImage(void *iplImage)
Reads the image from a OpenCV IplImage object (WITHOUT making a copy).
Structure to hold the parameters of a pinhole stereo camera model.
mrpt::img::CImage imageRight
Image from the right camera, only contains a valid image if hasImageRight == true.
std::vector< uint16_t > m_dat_mapy_right
A class for storing images as grayscale or RGB bitmaps.
void setRectifyMapsFast(std::vector< int16_t > &left_x, std::vector< uint16_t > &left_y, std::vector< int16_t > &right_x, std::vector< uint16_t > &right_y)
Direct input access to rectify maps.