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)
72 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
81 const cv::Size trg_size =
112 cv::Mat _mapx_left = cv::cvarrToMat(&mapx_left,
false);
113 cv::Mat _mapy_left = cv::cvarrToMat(&mapy_left,
false);
114 cv::Mat _mapx_right = cv::cvarrToMat(&mapx_right,
false);
115 cv::Mat _mapy_right = cv::cvarrToMat(&mapy_right,
false);
121 params.rightCameraPose.getInverseHomogeneousMatrix(hMatrix);
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>());
276 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
280 const CvSize trg_size =
283 : cvSize(ncols, nrows);
286 trg_size.width, trg_size.height, in_left_image.
isColor() ? 3 : 1,
289 trg_size.width, trg_size.height, in_left_image.
isColor() ? 3 : 1,
292 const IplImage* in_left = in_left_image.
getAs<IplImage>();
293 const IplImage* in_right = in_right_image.
getAs<IplImage>();
295 IplImage* out_left = out_left_image.
getAs<IplImage>();
296 IplImage* out_right = out_right_image.
getAs<IplImage>();
298 this->
rectify_IPL(in_left, in_right, out_left, out_right);
307 const bool use_internal_mem_cache)
const
311 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
315 const CvSize trg_size =
318 : cvSize(ncols, nrows);
320 const IplImage* in_left = left_image.
getAs<IplImage>();
321 const IplImage* in_right = right_image.
getAs<IplImage>();
323 IplImage *out_left_image, *out_right_image;
324 if (use_internal_mem_cache)
327 trg_size.width, trg_size.height, left_image.
isColor() ? 3 : 1,
330 trg_size.width, trg_size.height, right_image.
isColor() ? 3 : 1,
339 cvCreateImage(trg_size, in_left->depth, in_left->nChannels);
341 cvCreateImage(trg_size, in_right->depth, in_right->nChannels);
344 this->
rectify_IPL(in_left, in_right, out_left_image, out_right_image);
346 if (use_internal_mem_cache)
351 trg_size.width, trg_size.height, left_image.
isColor() ? 3 : 1,
354 trg_size.width, trg_size.height, right_image.
isColor() ? 3 : 1,
357 cvCopy(out_left_image, left_image.
getAs<IplImage>());
358 cvCopy(out_right_image, right_image.
getAs<IplImage>());
379 const bool use_internal_mem_cache)
const
387 use_internal_mem_cache);
407 const void* srcImg_left,
const void* srcImg_right,
void* outImg_left,
408 void* outImg_right)
const
411 ASSERT_(srcImg_left != outImg_left && srcImg_right != outImg_right)
415 "Error: setFromCamParams() must be called prior to rectify().")
417 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
426 const CvMat mapx_left = cvMat(
427 nrows_out, ncols_out, CV_16SC2,
429 const CvMat mapy_left = cvMat(
430 nrows_out, ncols_out, CV_16UC1,
432 const CvMat mapx_right = cvMat(
433 nrows_out, ncols_out, CV_16SC2,
435 const CvMat mapy_right = cvMat(
436 nrows_out, ncols_out, CV_16UC1,
439 const cv::Mat mapx1 = cv::cvarrToMat(&mapx_left);
440 const cv::Mat mapy1 = cv::cvarrToMat(&mapy_left);
441 const cv::Mat mapx2 = cv::cvarrToMat(&mapx_right);
442 const cv::Mat mapy2 = cv::cvarrToMat(&mapy_right);
444 const cv::Mat src1 = cv::cvarrToMat(srcImg_left);
445 const cv::Mat src2 = cv::cvarrToMat(srcImg_right);
446 cv::Mat dst1 = cv::cvarrToMat(outImg_left);
447 cv::Mat dst2 = cv::cvarrToMat(outImg_right);
451 cv::BORDER_CONSTANT, cvScalarAll(0));
454 cv::BORDER_CONSTANT, cvScalarAll(0));
484 const std::vector<int16_t>& left_x,
const std::vector<uint16_t>& left_y,
485 const std::vector<int16_t>& right_x,
const std::vector<uint16_t>& right_y)
499 std::vector<int16_t>& left_x, std::vector<uint16_t>& left_y,
500 std::vector<int16_t>& right_x, std::vector<uint16_t>& right_y)
A numeric matrix of compile-time fixed size.
A quaternion, which can represent a 3D rotation as pair , with a real part "r" and a 3D vector ,...
Observation class for either a pair of left+right or left+disparity images from a stereo camera.
mrpt::poses::CPose3DQuat cameraPose
The pose of the LEFT camera, relative to the robot.
bool hasImageRight
Whether imageRight actually contains data (Default upon construction: true)
mrpt::utils::CImage imageRight
Image from the right camera, only contains a valid image if hasImageRight == true.
void setStereoCameraParams(const mrpt::utils::TStereoCamera &in_params)
Sets leftCamera, rightCamera and rightCameraPose from a TStereoCamera structure.
mrpt::utils::CImage imageLeft
Image from the left camera (this image will be ALWAYS present)
mrpt::poses::CPose3DQuat rightCameraPose
The pose of the right camera, relative to the left one: Note that using the conventional reference co...
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
void setRotationMatrix(const mrpt::math::CMatrixDouble33 &ROT)
Sets the 3x3 rotation matrix.
A class used to store a 3D pose as a translation (x,y,z) and a quaternion (qr,qx,qy,...
mrpt::math::CArrayDouble< 3 > m_coords
The translation vector [x,y,z].
A class for storing images as grayscale or RGB bitmaps.
void setFromIplImage(void *iplImage)
Reads the image from a OpenCV IplImage object (WITHOUT making a copy).
bool isOriginTopLeft() const
Returns true if the coordinates origin is top-left, or false if it is bottom-left
bool isColor() const
Returns true if the image is RGB, false if it is grayscale.
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,...
const T * getAs() const
Returns a pointer to a const T* containing the image - the idea is to call like "img....
Structure to hold the parameters of a pinhole camera model.
double focalLengthMeters
The focal length of the camera, in meters (can be used among 'intrinsicParams' to determine the pixel...
mrpt::math::CArrayDouble< 5 > dist
[k1 k2 t1 t2 k3] -> k_i: parameters of radial distortion, t_i: parameters of tangential distortion (d...
uint32_t ncols
Camera resolution.
mrpt::math::CMatrixDouble33 intrinsicParams
Matrix of intrinsic parameters (containing the focal length and principal point coordinates)
Structure to hold the parameters of a pinhole stereo camera model.
mrpt::poses::CPose3DQuat rightCameraPose
Pose of the right camera with respect to the coordinate origin of the left camera.
TCamera leftCamera
Intrinsic and distortion parameters of the left and right cameras.
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_right
const mrpt::utils::TCamera & getRectifiedRightImageParams() const
Just like getRectifiedImageParams() but for the right camera only.
void rectify(const mrpt::utils::CImage &in_left_image, const mrpt::utils::CImage &in_right_image, mrpt::utils::CImage &out_left_image, mrpt::utils::CImage &out_right_image) const
Rectify the input image pair and save the result in a different output images - setFromCamParams() mu...
bool m_enable_both_centers_coincide
void setAlpha(double alpha)
Sets the alpha parameter which controls the zoom in/out of the rectified images, such that:
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.
std::vector< int16_t > m_dat_mapx_left
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::utils::TStereoCamera ¶ms)
Prepares the mapping from the intrinsic, distortion and relative pose parameters of a stereo camera.
const mrpt::utils::TCamera & getRectifiedLeftImageParams() const
Just like getRectifiedImageParams() but for the left camera only.
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.
mrpt::utils::TImageSize m_resize_output_value
std::vector< uint16_t > m_dat_mapy_left
mrpt::utils::CImage m_cache2
mrpt::utils::TInterpolationMethod m_interpolation_method
mrpt::poses::CPose3DQuat m_rot_left
The rotation applied to the left/right camera so their virtual image plane is the same after rectific...
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 ...
mrpt::utils::TStereoCamera m_camera_params
A copy of the data provided by the user.
mrpt::utils::TStereoCamera m_rectified_image_params
Resulting images params.
bool isSet() const
Returns true if setFromCamParams() has been already called, false otherwise.
void internal_invalidate()
const mrpt::utils::TStereoCamera & getRectifiedImageParams() const
After computing the rectification maps, this method retrieves the calibration parameters of the recti...
std::vector< uint16_t > m_dat_mapy_right
mrpt::utils::CImage m_cache1
Memory caches for in-place rectification speed-up.
mrpt::poses::CPose3DQuat m_rot_right
GLclampf GLclampf GLclampf alpha
GLenum const GLfloat * params
#define THROW_EXCEPTION(msg)
This base provides a set of functions for maths stuff.
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values,...
Classes for computer vision, detectors, features, etc.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
unsigned __int16 uint16_t
unsigned __int32 uint32_t