Main MRPT website > C++ reference for MRPT 1.5.9
CUndistortMap.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "vision-precomp.h" // Precompiled headers
12 
13 // Universal include for all versions of OpenCV
14 #include <mrpt/otherlibs/do_opencv_includes.h>
15 
16 using namespace mrpt;
17 using namespace mrpt::vision;
18 
19 
20 // Ctor: Leave all vectors empty
22 {
23 }
24 
25 
26 /** Prepares the mapping from the distortion parameters of a camera.
27  * Must be called before invoking \a undistort().
28  */
30 {
32 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM>=0x200
33  m_camera_params = campar;
34 
35  // Convert to opencv's format:
36  double aux1[3][3], aux2[1][5];
37  for (int i=0;i<3;i++)
38  for (int j=0;j<3;j++)
39  aux1[i][j] = campar.intrinsicParams(i,j);
40  for (int i=0;i<5;i++)
41  aux2[0][i]=campar.dist[i];
42 
43  const cv::Mat inMat( 3,3, CV_64F, aux1 );
44  const cv::Mat distM( 1, 5, CV_64F, aux2 );
45 
46  m_dat_mapx.resize(2*campar.nrows*campar.ncols);
47  m_dat_mapy.resize(campar.nrows*campar.ncols);
48 
49  CvMat mapx = cvMat(campar.nrows,campar.ncols, CV_16SC2, &m_dat_mapx[0] );
50  CvMat mapy = cvMat(campar.nrows,campar.ncols, CV_16UC1, &m_dat_mapy[0] );
51 
52  cv::Mat _mapx = cv::cvarrToMat(&mapx,false);
53  cv::Mat _mapy = cv::cvarrToMat(&mapy,false);
54 
55  cv::initUndistortRectifyMap( inMat, distM, cv::Mat(), inMat, _mapx.size(), _mapx.type(), _mapx, _mapy );
56 #else
57  THROW_EXCEPTION("MRPT built without OpenCV >=2.0.0!")
58 #endif
59  MRPT_END
60 }
61 
62 /** Undistort the input image and saves the result in-place- \a setFromCamParams() must have been set prior to calling this.
63  */
65 {
67  if (m_dat_mapx.empty())
68  THROW_EXCEPTION("Error: setFromCamParams() must be called prior to undistort().")
69 
70 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM>=0x200
71  CvMat mapx = cvMat(m_camera_params.nrows,m_camera_params.ncols, CV_16SC2, const_cast<int16_t*>(&m_dat_mapx[0]) ); // Wrappers on the data as a CvMat's.
72  CvMat mapy = cvMat(m_camera_params.nrows,m_camera_params.ncols, CV_16UC1, const_cast<uint16_t*>(&m_dat_mapy[0]) );
73 
74  const IplImage *srcImg = in_img.getAs<IplImage>(); // Source Image
75  IplImage *outImg = cvCreateImage( cvGetSize( srcImg ), srcImg->depth, srcImg->nChannels );
76  cvRemap(srcImg, outImg, &mapx, &mapy); //cv::remap(src, dst_part, map1_part, map2_part, INTER_LINEAR, BORDER_CONSTANT );
77  out_img.setFromIplImage(outImg);
78 #endif
79  MRPT_END
80 }
81 
82 /** Undistort the input image and saves the result in-place- \a setFromCamParams() must have been set prior to calling this.
83  */
85 {
87  if (m_dat_mapx.empty())
88  THROW_EXCEPTION("Error: setFromCamParams() must be called prior to undistort().")
89 
90 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM>=0x200
91  CvMat mapx = cvMat(m_camera_params.nrows,m_camera_params.ncols, CV_16SC2, const_cast<int16_t*>(&m_dat_mapx[0]) ); // Wrappers on the data as a CvMat's.
92  CvMat mapy = cvMat(m_camera_params.nrows,m_camera_params.ncols, CV_16UC1, const_cast<uint16_t*>(&m_dat_mapy[0]) );
93 
94  const IplImage *srcImg = in_out_img.getAs<IplImage>(); // Source Image
95  IplImage *outImg = cvCreateImage( cvGetSize( srcImg ), srcImg->depth, srcImg->nChannels );
96  cvRemap(srcImg, outImg, &mapx, &mapy); //cv::remap(src, dst_part, map1_part, map2_part, INTER_LINEAR, BORDER_CONSTANT );
97  in_out_img.setFromIplImage(outImg);
98 #endif
99  MRPT_END
100 }
101 
void undistort(const mrpt::utils::CImage &in_img, mrpt::utils::CImage &out_img) const
Undistort the input image and saves the result in the output one - setFromCamParams() must have been ...
mrpt::utils::TCamera m_camera_params
A copy of the data provided by the user.
Definition: CUndistortMap.h:76
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:101
#define THROW_EXCEPTION(msg)
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&#39;s headers.
Definition: CImage.h:517
std::vector< uint16_t > m_dat_mapy
Definition: CUndistortMap.h:74
#define MRPT_END
uint32_t ncols
Definition: TCamera.h:53
Classes for computer vision, detectors, features, etc.
void setFromIplImage(void *iplImage)
Reads the image from a OpenCV IplImage object (WITHOUT making a copy).
Definition: CImage.cpp:365
void setFromCamParams(const mrpt::utils::TCamera &params)
Prepares the mapping from the distortion parameters of a camera.
mrpt::math::CMatrixDouble33 intrinsicParams
Matrix of intrinsic parameters (containing the focal length and principal point coordinates) ...
Definition: TCamera.h:54
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::math::CArrayDouble< 5 > dist
[k1 k2 t1 t2 k3] -> k_i: parameters of radial distortion, t_i: parameters of tangential distortion (d...
Definition: TCamera.h:55
uint32_t nrows
Camera resolution.
Definition: TCamera.h:53
std::vector< int16_t > m_dat_mapx
Definition: CUndistortMap.h:73
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:31



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020