Main MRPT website > C++ reference for MRPT 1.9.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-2018, 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 using namespace mrpt::img;
19 
20 // Ctor: Leave all vectors empty
22 /** Prepares the mapping from the distortion parameters of a camera.
23  * Must be called before invoking \a undistort().
24  */
26 {
28 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
29  m_camera_params = campar;
30 
31  // Convert to opencv's format:
32  double aux1[3][3], aux2[1][5];
33  for (int i = 0; i < 3; i++)
34  for (int j = 0; j < 3; j++) aux1[i][j] = campar.intrinsicParams(i, j);
35  for (int i = 0; i < 5; i++) aux2[0][i] = campar.dist[i];
36 
37  const cv::Mat inMat(3, 3, CV_64F, aux1);
38  const cv::Mat distM(1, 5, CV_64F, aux2);
39 
40  m_dat_mapx.resize(2 * campar.nrows * campar.ncols);
41  m_dat_mapy.resize(campar.nrows * campar.ncols);
42 
43  CvMat mapx = cvMat(campar.nrows, campar.ncols, CV_16SC2, &m_dat_mapx[0]);
44  CvMat mapy = cvMat(campar.nrows, campar.ncols, CV_16UC1, &m_dat_mapy[0]);
45 
46  cv::Mat _mapx = cv::cvarrToMat(&mapx, false);
47  cv::Mat _mapy = cv::cvarrToMat(&mapy, false);
48 
49  cv::initUndistortRectifyMap(
50  inMat, distM, cv::Mat(), inMat, _mapx.size(), _mapx.type(), _mapx,
51  _mapy);
52 #else
53  THROW_EXCEPTION("MRPT built without OpenCV >=2.0.0!");
54 #endif
55  MRPT_END
56 }
57 
58 /** Undistort the input image and saves the result in-place- \a
59  * setFromCamParams() must have been set prior to calling this.
60  */
62  const mrpt::img::CImage& in_img, mrpt::img::CImage& out_img) const
63 {
65  if (m_dat_mapx.empty())
67  "Error: setFromCamParams() must be called prior to undistort().")
68 
69 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
70  CvMat mapx = cvMat(
71  m_camera_params.nrows, m_camera_params.ncols, CV_16SC2,
72  const_cast<int16_t*>(
73  &m_dat_mapx[0])); // Wrappers on the data as a CvMat's.
74  CvMat mapy = cvMat(
75  m_camera_params.nrows, m_camera_params.ncols, CV_16UC1,
76  const_cast<uint16_t*>(&m_dat_mapy[0]));
77 
78  const IplImage* srcImg = in_img.getAs<IplImage>(); // Source Image
79  IplImage* outImg =
80  cvCreateImage(cvGetSize(srcImg), srcImg->depth, srcImg->nChannels);
81  cvRemap(srcImg, outImg, &mapx, &mapy); // cv::remap(src, dst_part,
82  // map1_part, map2_part,
83  // INTER_LINEAR, BORDER_CONSTANT );
84  out_img.setFromIplImage(outImg);
85 #endif
86  MRPT_END
87 }
88 
89 /** Undistort the input image and saves the result in-place- \a
90  * setFromCamParams() must have been set prior to calling this.
91  */
93 {
95  if (m_dat_mapx.empty())
97  "Error: setFromCamParams() must be called prior to undistort().")
98 
99 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
100  CvMat mapx = cvMat(
101  m_camera_params.nrows, m_camera_params.ncols, CV_16SC2,
102  const_cast<int16_t*>(
103  &m_dat_mapx[0])); // Wrappers on the data as a CvMat's.
104  CvMat mapy = cvMat(
105  m_camera_params.nrows, m_camera_params.ncols, CV_16UC1,
106  const_cast<uint16_t*>(&m_dat_mapy[0]));
107 
108  const IplImage* srcImg = in_out_img.getAs<IplImage>(); // Source Image
109  IplImage* outImg =
110  cvCreateImage(cvGetSize(srcImg), srcImg->depth, srcImg->nChannels);
111  cvRemap(srcImg, outImg, &mapx, &mapy); // cv::remap(src, dst_part,
112  // map1_part, map2_part,
113  // INTER_LINEAR, BORDER_CONSTANT );
114  in_out_img.setFromIplImage(outImg);
115 #endif
116  MRPT_END
117 }
uint32_t nrows
Definition: TCamera.h:41
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: img/CImage.h:599
void setFromCamParams(const mrpt::img::TCamera &params)
Prepares the mapping from the distortion parameters of a camera.
#define MRPT_START
Definition: exceptions.h:262
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
mrpt::math::CMatrixDouble33 intrinsicParams
Matrix of intrinsic parameters (containing the focal length and principal point coordinates) ...
Definition: TCamera.h:44
void undistort(const mrpt::img::CImage &in_img, mrpt::img::CImage &out_img) const
Undistort the input image and saves the result in the output one - setFromCamParams() must have been ...
Classes for computer vision, detectors, features, etc.
Definition: CCamModel.h:20
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:29
std::array< double, 5 > dist
[k1 k2 t1 t2 k3] -> k_i: parameters of radial distortion, t_i: parameters of tangential distortion (d...
Definition: TCamera.h:47
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define MRPT_END
Definition: exceptions.h:266
uint32_t ncols
Camera resolution.
Definition: TCamera.h:41
void setFromIplImage(void *iplImage)
Reads the image from a OpenCV IplImage object (WITHOUT making a copy).
Definition: CImage.cpp:363
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019