MRPT  2.0.2
CUndistortMap.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "vision-precomp.h" // Precompiled headers
11 
13 
14 // Universal include for all versions of OpenCV
15 #include <mrpt/3rdparty/do_opencv_includes.h>
16 
17 using namespace mrpt;
18 using namespace mrpt::vision;
19 using namespace mrpt::img;
20 
21 // Ctor: Leave all vectors empty
23 /** Prepares the mapping from the distortion parameters of a camera.
24  * Must be called before invoking \a undistort().
25  */
27 {
29 #if MRPT_HAS_OPENCV
30  m_camera_params = campar;
31 
32  // Convert to opencv's format:
33  double aux1[3][3], aux2[1][5];
34  for (int i = 0; i < 3; i++)
35  for (int j = 0; j < 3; j++) aux1[i][j] = campar.intrinsicParams(i, j);
36  for (int i = 0; i < 5; i++) aux2[0][i] = campar.dist[i];
37 
38  const cv::Mat inMat(3, 3, CV_64F, aux1);
39  const cv::Mat distM(1, 5, CV_64F, aux2);
40 
41  m_dat_mapx.resize(2 * campar.nrows * campar.ncols);
42  m_dat_mapy.resize(campar.nrows * campar.ncols);
43 
44  cv::Mat mapx(campar.nrows, campar.ncols, CV_16SC2, &m_dat_mapx[0]);
45  cv::Mat mapy(campar.nrows, campar.ncols, CV_16UC1, &m_dat_mapy[0]);
46 
47  cv::initUndistortRectifyMap(
48  inMat, distM, cv::Mat(), inMat, mapx.size(), mapx.type(), mapx, mapy);
49 #else
50  THROW_EXCEPTION("MRPT built without OpenCV >=2.0.0!");
51 #endif
52  MRPT_END
53 }
54 
55 /** Undistort the input image and saves the result in-place- \a
56  * setFromCamParams() must have been set prior to calling this.
57  */
59  const mrpt::img::CImage& in_img, mrpt::img::CImage& out_img) const
60 {
62  if (m_dat_mapx.empty())
64  "Error: setFromCamParams() must be called prior to undistort().");
65 
66 #if MRPT_HAS_OPENCV
67  using namespace cv;
68  Mat mapx(
69  m_camera_params.nrows, m_camera_params.ncols, CV_16SC2,
70  const_cast<int16_t*>(&m_dat_mapx[0]));
71  Mat mapy(
72  m_camera_params.nrows, m_camera_params.ncols, CV_16UC1,
73  const_cast<uint16_t*>(&m_dat_mapy[0]));
74 
75  out_img.resize(
76  in_img.getWidth(), in_img.getHeight(), in_img.getChannelCount());
77 
78  cv::remap(
79  in_img.asCvMat<Mat>(SHALLOW_COPY), out_img.asCvMat<Mat>(SHALLOW_COPY),
80  mapx, mapy, INTER_LINEAR);
81 #endif
82  MRPT_END
83 }
84 
85 /** Undistort the input image and saves the result in-place- \a
86  * setFromCamParams() must have been set prior to calling this.
87  */
89 {
91  if (m_dat_mapx.empty())
93  "Error: setFromCamParams() must be called prior to undistort().");
94 
95 #if MRPT_HAS_OPENCV
96  cv::Mat mapx(
97  m_camera_params.nrows, m_camera_params.ncols, CV_16SC2,
98  const_cast<int16_t*>(&m_dat_mapx[0]));
99  cv::Mat mapy(
100  m_camera_params.nrows, m_camera_params.ncols, CV_16UC1,
101  const_cast<uint16_t*>(&m_dat_mapy[0]));
102 
103  cv::Mat in = in_out_img.asCvMat<cv::Mat>(SHALLOW_COPY);
104  cv::Mat out(in.size(), in.type());
105 
106  cv::remap(in, out, mapx, mapy, cv::INTER_LINEAR);
107 
108  in_out_img = CImage(out, SHALLOW_COPY);
109 #endif
110  MRPT_END
111 }
Shallow copy: the copied object is a reference to the original one.
Definition: img/CImage.h:75
uint32_t nrows
Definition: TCamera.h:40
void setFromCamParams(const mrpt::img::TCamera &params)
Prepares the mapping from the distortion parameters of a camera.
#define MRPT_START
Definition: exceptions.h:241
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
TImageChannels getChannelCount() const
Returns the number of channels, typically 1 (GRAY) or 3 (RGB)
Definition: CImage.cpp:878
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:849
Definition: img/CImage.h:23
void asCvMat(cv::Mat &out_img, copy_type_t copy_type) const
Makes a shallow or deep copy of this image into the provided cv::Mat.
Definition: CImage.cpp:217
mrpt::math::CMatrixDouble33 intrinsicParams
Matrix of intrinsic parameters (containing the focal length and principal point coordinates): ...
Definition: TCamera.h:50
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 ...
mrpt::img::CImage CImage
Definition: utils/CImage.h:5
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:818
void resize(std::size_t width, std::size_t height, TImageChannels nChannels, PixelDepth depth=PixelDepth::D8U)
Changes the size of the image, erasing previous contents (does NOT scale its current content...
Definition: CImage.cpp:247
Classes for computer vision, detectors, features, etc.
Definition: CDifodo.h:17
Parameters for the Brown-Conrady camera lens distortion model.
Definition: TCamera.h:26
std::array< double, 8 > dist
[k1 k2 t1 t2 k3 k4 k5 k6] -> k_i: parameters of radial distortion, t_i: parameters of tangential dist...
Definition: TCamera.h:53
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::vision::TStereoCalibResults out
#define MRPT_END
Definition: exceptions.h:245
CUndistortMap()
Default ctor.
uint32_t ncols
Camera resolution.
Definition: TCamera.h:40
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:148



Page generated by Doxygen 1.8.14 for MRPT 2.0.2 Git: 9b4fd2465 Mon May 4 16:59:08 2020 +0200 at lun may 4 17:26:07 CEST 2020