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-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 // Ctor: Leave all vectors empty
21 /** Prepares the mapping from the distortion parameters of a camera.
22  * Must be called before invoking \a undistort().
23  */
25 {
27 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
28  m_camera_params = campar;
29 
30  // Convert to opencv's format:
31  double aux1[3][3], aux2[1][5];
32  for (int i = 0; i < 3; i++)
33  for (int j = 0; j < 3; j++) aux1[i][j] = campar.intrinsicParams(i, j);
34  for (int i = 0; i < 5; i++) aux2[0][i] = campar.dist[i];
35 
36  const cv::Mat inMat(3, 3, CV_64F, aux1);
37  const cv::Mat distM(1, 5, CV_64F, aux2);
38 
39  m_dat_mapx.resize(2 * campar.nrows * campar.ncols);
40  m_dat_mapy.resize(campar.nrows * campar.ncols);
41 
42  CvMat mapx = cvMat(campar.nrows, campar.ncols, CV_16SC2, &m_dat_mapx[0]);
43  CvMat mapy = cvMat(campar.nrows, campar.ncols, CV_16UC1, &m_dat_mapy[0]);
44 
45  cv::Mat _mapx = cv::cvarrToMat(&mapx, false);
46  cv::Mat _mapy = cv::cvarrToMat(&mapy, false);
47 
48  cv::initUndistortRectifyMap(
49  inMat, distM, cv::Mat(), inMat, _mapx.size(), _mapx.type(), _mapx,
50  _mapy);
51 #else
52  THROW_EXCEPTION("MRPT built without OpenCV >=2.0.0!")
53 #endif
54  MRPT_END
55 }
56 
57 /** Undistort the input image and saves the result in-place- \a
58  * setFromCamParams() must have been set prior to calling this.
59  */
61  const mrpt::utils::CImage& in_img, mrpt::utils::CImage& out_img) const
62 {
64  if (m_dat_mapx.empty())
66  "Error: setFromCamParams() must be called prior to undistort().")
67 
68 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
69  CvMat mapx = cvMat(
71  const_cast<int16_t*>(
72  &m_dat_mapx[0])); // Wrappers on the data as a CvMat's.
73  CvMat mapy = cvMat(
75  const_cast<uint16_t*>(&m_dat_mapy[0]));
76 
77  const IplImage* srcImg = in_img.getAs<IplImage>(); // Source Image
78  IplImage* outImg =
79  cvCreateImage(cvGetSize(srcImg), srcImg->depth, srcImg->nChannels);
80  cvRemap(srcImg, outImg, &mapx, &mapy); // cv::remap(src, dst_part,
81  // map1_part, map2_part,
82  // INTER_LINEAR, BORDER_CONSTANT );
83  out_img.setFromIplImage(outImg);
84 #endif
85  MRPT_END
86 }
87 
88 /** Undistort the input image and saves the result in-place- \a
89  * setFromCamParams() must have been set prior to calling this.
90  */
92 {
94  if (m_dat_mapx.empty())
96  "Error: setFromCamParams() must be called prior to undistort().")
97 
98 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
99  CvMat mapx = cvMat(
101  const_cast<int16_t*>(
102  &m_dat_mapx[0])); // Wrappers on the data as a CvMat's.
103  CvMat mapy = cvMat(
105  const_cast<uint16_t*>(&m_dat_mapy[0]));
106 
107  const IplImage* srcImg = in_out_img.getAs<IplImage>(); // Source Image
108  IplImage* outImg =
109  cvCreateImage(cvGetSize(srcImg), srcImg->depth, srcImg->nChannels);
110  cvRemap(srcImg, outImg, &mapx, &mapy); // cv::remap(src, dst_part,
111  // map1_part, map2_part,
112  // INTER_LINEAR, BORDER_CONSTANT );
113  in_out_img.setFromIplImage(outImg);
114 #endif
115  MRPT_END
116 }
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:89
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:118
#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:587
std::vector< uint16_t > m_dat_mapy
Definition: CUndistortMap.h:86
#define MRPT_END
uint32_t ncols
Camera resolution.
Definition: TCamera.h:54
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:368
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:57
#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:60
uint32_t nrows
Definition: TCamera.h:54
std::vector< int16_t > m_dat_mapx
Definition: CUndistortMap.h:85
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:32



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019