14 #include <mrpt/otherlibs/do_opencv_includes.h>
24 CStereoRectifyMap::CStereoRectifyMap() :
26 m_resize_output(false),
27 m_enable_both_centers_coincide(false),
28 m_resize_output_value(0,0),
72 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM>=0x200
99 CvMat mapx_left = cvMat(nrows_out,ncols_out, CV_16SC2, &
m_dat_mapx_left[0] );
100 CvMat mapy_left = cvMat(nrows_out,ncols_out, CV_16UC1, &
m_dat_mapy_left[0] );
101 CvMat mapx_right = cvMat(nrows_out,ncols_out, CV_16SC2, &
m_dat_mapx_right[0] );
102 CvMat mapy_right = cvMat(nrows_out,ncols_out, CV_16UC1, &
m_dat_mapy_right[0] );
104 cv::Mat _mapx_left = cv::cvarrToMat(&mapx_left,
false);
105 cv::Mat _mapy_left = cv::cvarrToMat(&mapy_left,
false);
106 cv::Mat _mapx_right = cv::cvarrToMat(&mapx_right,
false);
107 cv::Mat _mapy_right = cv::cvarrToMat(&mapy_right,
false);
113 params.rightCameraPose.getInverseHomogeneousMatrix( hMatrix );
116 for(
unsigned int i = 0; i < 3; ++i)
117 for(
unsigned int j = 0; j < 3; ++j)
118 m1[i][j] = hMatrix(i,j);
121 double rcTrans[3] = { hMatrix(0,3), hMatrix(1,3), hMatrix(2,3) };
123 double ipl[3][3], ipr[3][3], dpl[5], dpr[5];
124 for(
unsigned int i = 0; i < 3; ++i )
125 for(
unsigned int j = 0; j < 3; ++j )
127 ipl[i][j] = cam1.intrinsicParams(i,j);
131 for(
unsigned int i = 0; i < 5; ++i )
133 dpl[i] = cam1.dist[i];
134 dpr[i] = cam2.
dist[i];
137 const cv::Mat
R( 3, 3, CV_64F, &m1 );
138 const cv::Mat T( 3, 1, CV_64F, &rcTrans );
140 const cv::Mat K1(3,3,CV_64F,ipl);
141 const cv::Mat K2(3,3,CV_64F,ipr);
142 const cv::Mat D1(1,5,CV_64F,dpl);
143 const cv::Mat D2(1,5,CV_64F,dpr);
145 double _R1[3][3], _R2[3][3], _P1[3][4], _P2[3][4], _Q[4][4];
146 cv::Mat R1(3,3,CV_64F,_R1);
147 cv::Mat R2(3,3,CV_64F,_R2);
148 cv::Mat P1(3,4,CV_64F,_P1);
149 cv::Mat P2(3,4,CV_64F,_P2);
150 cv::Mat Q(4,4,CV_64F,_Q);
152 const cv::Size img_size(ncols,nrows);
183 #if MRPT_OPENCV_VERSION_NUM<0x210
193 #elif MRPT_OPENCV_VERSION_NUM<0x230
221 cv::initUndistortRectifyMap( K1, D1, R1, P1, real_trg_size, CV_16SC2, _mapx_left, _mapy_left );
222 cv::initUndistortRectifyMap( K2, D2, R2, P2, real_trg_size, CV_16SC2, _mapx_right, _mapy_right );
225 for(
unsigned int i = 0; i < 3; ++i )
226 for(
unsigned int j = 0; j < 3; ++j )
248 const Eigen::Map<Eigen::Matrix3d> R1e( R1.ptr<
double>() );
249 const Eigen::Map<Eigen::Matrix3d> R2e( R2.ptr<
double>() );
274 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM>=0x200
286 const IplImage * in_left = in_left_image.
getAs<IplImage>();
287 const IplImage * in_right = in_right_image.
getAs<IplImage>();
289 IplImage * out_left = out_left_image.
getAs<IplImage>();
290 IplImage * out_right = out_right_image.
getAs<IplImage>();
292 this->
rectify_IPL(in_left, in_right, out_left, out_right);
302 const bool use_internal_mem_cache)
const
306 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM>=0x200
315 const IplImage * in_left = left_image.
getAs<IplImage>();
316 const IplImage * in_right = right_image.
getAs<IplImage>();
318 IplImage * out_left_image, *out_right_image;
319 if (use_internal_mem_cache)
329 out_left_image = cvCreateImage( trg_size, in_left->depth, in_left->nChannels );
330 out_right_image = cvCreateImage( trg_size, in_right->depth, in_right->nChannels );
335 out_left_image, out_right_image);
337 if (use_internal_mem_cache)
344 cvCopy(out_left_image, left_image.
getAs<IplImage>());
345 cvCopy(out_right_image, right_image.
getAs<IplImage>());
363 const bool use_internal_mem_cache)
const
387 const void* srcImg_left,
388 const void* srcImg_right,
390 void* outImg_right)
const
393 ASSERT_(srcImg_left!=outImg_left && srcImg_right!=outImg_right)
397 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM>=0x200
409 const cv::Mat mapx1 = cv::cvarrToMat(&mapx_left);
410 const cv::Mat mapy1 = cv::cvarrToMat(&mapy_left);
411 const cv::Mat mapx2 = cv::cvarrToMat(&mapx_right);
412 const cv::Mat mapy2 = cv::cvarrToMat(&mapy_right);
414 const cv::Mat src1 = cv::cvarrToMat(srcImg_left);
415 const cv::Mat src2 = cv::cvarrToMat(srcImg_right);
416 cv::Mat dst1 = cv::cvarrToMat(outImg_left);
417 cv::Mat dst2 = cv::cvarrToMat(outImg_right);
419 cv::remap( src1, dst1, mapx1, mapy1,
static_cast<int>(
m_interpolation_method),cv::BORDER_CONSTANT, cvScalarAll(0) );
420 cv::remap( src2, dst2, mapx2, mapy2,
static_cast<int>(
m_interpolation_method),cv::BORDER_CONSTANT, cvScalarAll(0) );
447 const std::vector<int16_t> &left_x,
const std::vector<uint16_t> &left_y,
448 const std::vector<int16_t> &right_x,
const std::vector<uint16_t> &right_y )
462 std::vector<int16_t> & left_x, std::vector<uint16_t> & left_y,
463 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...
uint32_t nrows
Camera resolution.
mrpt::math::CArrayDouble< 5 > dist
[k1 k2 t1 t2 k3] -> k_i: parameters of radial distortion, t_i: parameters of tangential distortion (d...
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 rightCamera
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
Memory caches for in-place rectification speed-up.
mrpt::utils::TInterpolationMethod m_interpolation_method
mrpt::poses::CPose3DQuat m_rot_left
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
mrpt::poses::CPose3DQuat m_rot_right
The rotation applied to the left/right camera so their virtual image plane is the same after rectific...
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