MRPT  1.9.9
CStereoRectifyMap.h
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 #ifndef mrpt_CStereoRectifyMap_H
10 #define mrpt_CStereoRectifyMap_H
11 
12 #include <mrpt/img/TStereoCamera.h>
13 #include <mrpt/img/CImage.h>
15 #include <mrpt/poses/CPose3DQuat.h>
16 
17 namespace mrpt::vision
18 {
19 /** Use this class to rectify stereo images if the same distortion maps are
20  * reused over and over again.
21  * The rectify maps are cached internally and only computed once for the
22  * camera parameters.
23  * The stereo camera calibration must be supplied in a
24  * mrpt::util::TStereoCamera structure
25  * (which provides method for loading from a plain text config file) or
26  * directly from the
27  * parameters of a mrpt::obs::CObservationStereoImages object.
28  *
29  * Remember that the rectified images have a different set of intrinsic
30  * parameters than the
31  * original images, which can be retrieved with \a getRectifiedImageParams()
32  *
33  * Works with grayscale or color images.
34  *
35  * Refer to the program stereo-calib-gui for a tool that generates the
36  * required stereo camera parameters
37  * from a set of stereo images of a checkerboard.
38  *
39  * Example of usage with mrpt::obs::CObservationStereoImages:
40  *
41  * \code
42  * CStereoRectifyMap rectify_map;
43  * // Set options as desired:
44  * // rectify_map.setAlpha(...);
45  * // rectify_map.enableBothCentersCoincide(...);
46  *
47  * while (true) {
48  * mrpt::obs::CObservationStereoImages::Ptr obs_stereo = ... // Grab stereo
49  * observation from wherever
50  *
51  * // Only once, construct the rectification maps:
52  * if (!rectify_map.isSet())
53  * rectify_map.setFromCamParams(*obs_stereo);
54  *
55  * // Rectify in place:
56  * unmap.rectify(*obs_stereo);
57  * // Rectified images are now in: obs_stereo->imageLeft &
58  * obs_stereo->imageRight
59  * }
60  * \endcode
61  *
62  * Read also the tutorial page online:
63  * http://www.mrpt.org/Rectifying_stereo_images
64  *
65  * \sa CUndistortMap, mrpt::obs::CObservationStereoImages,
66  * mrpt::img::TCamera, the application <a
67  * href="http://www.mrpt.org/Application:camera-calib" >camera-calib</a> for
68  * calibrating a camera.
69  *
70  * \note This class provides a uniform wrap over different OpenCV versions. The
71  * "alpha" parameter is ignored if built against OpenCV 2.0.X
72  *
73  * \ingroup mrpt_vision_grp
74  */
76 {
77  public:
78  /** Default ctor */
80 
81  /** @name Rectify map preparation and setting/getting of parameters
82  @{ */
83  /** Returns true if \a setFromCamParams() has been already called, false
84  * otherwise.
85  * Can be used within loops to determine the first usage of the object and
86  * when it needs to be initialized.
87  */
88  inline bool isSet() const { return !m_dat_mapx_left.empty(); }
89  /** Prepares the mapping from the intrinsic, distortion and relative pose
90  * parameters of a stereo camera.
91  * Must be called before invoking \a rectify().
92  * The \a alpha parameter can be changed with \a setAlpha() before invoking
93  * this method; otherwise, the current rectification maps will be marked as
94  * invalid and should be prepared again.
95  * \sa setAlpha()
96  */
98 
99  /** A wrapper to \a setFromCamParams() which takes the parameters from an
100  * stereo observation object */
102  {
104  stereo_obs.getStereoCameraParams(params);
106  }
107 
108  /** Returns the camera parameters which were used to generate the distortion
109  * map, as passed by the user to \a setFromCamParams */
111  {
112  return m_camera_params;
113  }
114 
115  /** After computing the rectification maps, this method retrieves the
116  * calibration parameters of the rectified images
117  * (which won't have any distortion).
118  * \exception std::exception If the rectification maps have not been
119  * computed.
120  */
122 
123  /** Just like \a getRectifiedImageParams() but for the left camera only */
125  /** Just like \a getRectifiedImageParams() but for the right camera only */
127 
128  /** Sets the \a alpha parameter which controls the zoom in/out of the
129  * rectified images, such that:
130  * - alpha=0 => rectified images are zoom in so that only valid pixels are
131  * visible
132  * - alpha=1 => rectified images will contain large "black areas" but no
133  * pixel from the original image will be lost.
134  * Intermediary values leads to intermediary results.
135  * Its default value (-1) means auto guess by the OpenCV's algorithm.
136  * \note Call this method before building the rectification maps, otherwise
137  * they'll be marked as invalid.
138  */
139  void setAlpha(double alpha);
140 
141  /** Return the \a alpha parameter \sa setAlpha */
142  inline double getAlpha() const { return m_alpha; }
143  /** If enabled, the computed maps will rectify images to a size different
144  * than their original size.
145  * \note Call this method before building the rectification maps, otherwise
146  * they'll be marked as invalid.
147  */
148  void enableResizeOutput(
149  bool enable, unsigned int target_width = 0,
150  unsigned int target_height = 0);
151 
152  /** Returns whether resizing is enabled (default=false) \sa
153  * enableResizeOutput */
154  bool isEnabledResizeOutput() const { return m_resize_output; }
155  /** Only when \a isEnabledResizeOutput() returns true, this gets the target
156  * size \sa enableResizeOutput */
158  {
159  return m_resize_output_value;
160  }
161 
162  /** Change remap interpolation method (default=Lineal). This parameter can
163  * be safely changed at any instant without consequences. */
165  {
167  }
168 
169  /** Get the currently selected interpolation method \sa
170  * setInterpolationMethod */
172  {
173  return m_interpolation_method;
174  }
175 
176  /** If enabled (default=false), the principal points in both output images
177  * will coincide.
178  * \note Call this method before building the rectification maps, otherwise
179  * they'll be marked as invalid.
180  */
181  void enableBothCentersCoincide(bool enable = true);
182 
183  /** \sa enableBothCentersCoincide */
185  {
187  }
188 
189  /** After computing the rectification maps, get the rotation applied to the
190  * left/right camera so their virtual image plane is the same after
191  * rectification */
193  {
194  return m_rot_left;
195  }
196  /** See \a getLeftCameraRot() */
198  {
199  return m_rot_right;
200  }
201  /** Direct input access to rectify maps */
202  void setRectifyMaps(
203  const std::vector<int16_t>& left_x, const std::vector<uint16_t>& left_y,
204  const std::vector<int16_t>& right_x,
205  const std::vector<uint16_t>& right_y);
206 
207  /** Direct input access to rectify maps. This method swaps the vectors so
208  * the inputs are no longer available.*/
209  void setRectifyMapsFast(
210  std::vector<int16_t>& left_x, std::vector<uint16_t>& left_y,
211  std::vector<int16_t>& right_x, std::vector<uint16_t>& right_y);
212 
213  /** @} */
214 
215  /** @name Rectify methods
216  @{ */
217 
218  /** Rectify the input image pair and save the result in a different output
219  * images - \a setFromCamParams() must have been set prior to calling this.
220  * The previous contents of the output images are completely ignored, but
221  * if they are already of the
222  * correct size and type, allocation time will be saved.
223  * Recall that \a getRectifiedImageParams() provides you the new intrinsic
224  * parameters of these images.
225  * \exception std::exception If the rectification maps have not been
226  * computed.
227  * \note The same image CANNOT be at the same time input and output, in
228  * which case an exception will be raised (but see the overloaded version
229  * for in-place rectification)
230  */
231  void rectify(
232  const mrpt::img::CImage& in_left_image,
233  const mrpt::img::CImage& in_right_image,
234  mrpt::img::CImage& out_left_image,
235  mrpt::img::CImage& out_right_image) const;
236 
237  /** Overloaded version for in-place rectification: replace input images with
238  * their rectified versions
239  * If \a use_internal_mem_cache is set to \a true (recommended), will reuse
240  * over and over again the same
241  * auxiliary images (kept internally to this object) needed for in-place
242  * rectification.
243  * The only reason not to enable this cache is when multiple threads can
244  * invoke this method simultaneously.
245  */
246  void rectify(
247  mrpt::img::CImage& left_image, mrpt::img::CImage& right_image,
248  const bool use_internal_mem_cache = true) const;
249 
250  /** Overloaded version for in-place rectification of image pairs stored in a
251  * mrpt::obs::CObservationStereoImages.
252  * Upon return, the new camera intrinsic parameters will be already stored
253  * in the observation object.
254  * If \a use_internal_mem_cache is set to \a true (recommended), will reuse
255  * over and over again the same
256  * auxiliary images (kept internally to this object) needed for in-place
257  * rectification.
258  * The only reason not to enable this cache is when multiple threads can
259  * invoke this method simultaneously.
260  * \note This method uses the left & right camera rotations computed by the
261  * rectification map to update
262  * mrpt::obs::CObservationStereoImages::cameraPose (left camera wrt
263  * the robot frame) and
264  * mrpt::obs::CObservationStereoImages::rightCameraPose (right wrt
265  * left camera).
266  */
267  void rectify(
268  mrpt::obs::CObservationStereoImages& stereo_image_observation,
269  const bool use_internal_mem_cache = true) const;
270 
271  /** Just like rectify() but directly works with OpenCV's "IplImage*", which
272  * must be passed as "void*" to avoid header dependencies
273  * Output images CANNOT coincide with the input images. */
274  void rectify_IPL(
275  const void* in_left_image, const void* in_right_image,
276  void* out_left_image, void* out_right_image) const;
277 
278  /** @} */
279 
280  private:
281  double m_alpha;
286 
287  /** Memory caches for in-place rectification speed-up. */
289 
290  std::vector<int16_t> m_dat_mapx_left, m_dat_mapx_right;
291  std::vector<uint16_t> m_dat_mapy_left, m_dat_mapy_right;
292 
293  /** A copy of the data provided by the user */
295  /** Resulting images params */
297 
298  /** The rotation applied to the left/right camera so their virtual image
299  * plane is the same after rectification. */
301 
302  void internal_invalidate();
303 
304 }; // end class
305 
306 }
307 #endif
308 
309 
mrpt::img::CImage m_cache1
Memory caches for in-place rectification speed-up.
void setFromCamParams(const mrpt::obs::CObservationStereoImages &stereo_obs)
A wrapper to setFromCamParams() which takes the parameters from an stereo observation object...
GLclampf GLclampf GLclampf alpha
Definition: glext.h:3525
mrpt::img::TStereoCamera m_camera_params
A copy of the data provided by the user.
const mrpt::img::TStereoCamera & getRectifiedImageParams() const
After computing the rectification maps, this method retrieves the calibration parameters of the recti...
mrpt::img::TInterpolationMethod getInterpolationMethod() const
Get the currently selected interpolation method.
mrpt::img::TStereoCamera m_rectified_image_params
Resulting images params.
bool isEnabledResizeOutput() const
Returns whether resizing is enabled (default=false)
void setInterpolationMethod(const mrpt::img::TInterpolationMethod interp)
Change remap interpolation method (default=Lineal).
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::img::TStereoCamera &params)
Prepares the mapping from the intrinsic, distortion and relative pose parameters of a stereo camera...
void getStereoCameraParams(mrpt::img::TStereoCamera &out_params) const
Populates a TStereoCamera structure with the parameters in leftCamera, rightCamera and rightCameraPos...
bool isSet() const
Returns true if setFromCamParams() has been already called, false otherwise.
mrpt::img::TInterpolationMethod m_interpolation_method
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&#39;s "IplImage*", which must be passed as "void*" to ...
void rectify(const mrpt::img::CImage &in_left_image, const mrpt::img::CImage &in_right_image, mrpt::img::CImage &out_left_image, mrpt::img::CImage &out_right_image) const
Rectify the input image pair and save the result in a different output images - setFromCamParams() mu...
void setAlpha(double alpha)
Sets the alpha parameter which controls the zoom in/out of the rectified images, such that: ...
const mrpt::img::TCamera & getRectifiedLeftImageParams() const
Just like getRectifiedImageParams() but for the left camera only.
TInterpolationMethod
Interpolation methods for images.
Definition: img/CImage.h:34
Observation class for either a pair of left+right or left+disparity images from a stereo camera...
Use this class to rectify stereo images if the same distortion maps are reused over and over again...
Classes for computer vision, detectors, features, etc.
Definition: CCamModel.h:18
Structure to hold the parameters of a pinhole camera model.
Definition: TCamera.h:27
mrpt::img::TImageSize getResizeOutputSize() const
Only when isEnabledResizeOutput() returns true, this gets the target size.
A class used to store a 3D pose as a translation (x,y,z) and a quaternion (qr,qx,qy,qz).
Definition: CPose3DQuat.h:46
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_left
const mrpt::img::TStereoCamera & getCameraParams() const
Returns the camera parameters which were used to generate the distortion map, as passed by the user t...
mrpt::poses::CPose3DQuat m_rot_left
The rotation applied to the left/right camera so their virtual image plane is the same after rectific...
const mrpt::poses::CPose3DQuat & getLeftCameraRot() const
After computing the rectification maps, get the rotation applied to the left/right camera so their vi...
mrpt::img::TImageSize m_resize_output_value
const mrpt::img::TCamera & getRectifiedRightImageParams() const
Just like getRectifiedImageParams() but for the right camera only.
TPixelCoord TImageSize
A type for image sizes.
Definition: TPixelCoord.h:51
std::vector< int16_t > m_dat_mapx_right
GLuint interp
Definition: glext.h:7133
std::vector< uint16_t > m_dat_mapy_left
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...
double getAlpha() const
Return the alpha parameter.
const mrpt::poses::CPose3DQuat & getRightCameraRot() const
See getLeftCameraRot()
GLenum const GLfloat * params
Definition: glext.h:3534
mrpt::poses::CPose3DQuat m_rot_right
Structure to hold the parameters of a pinhole stereo camera model.
Definition: TStereoCamera.h:23
std::vector< uint16_t > m_dat_mapy_right
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
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.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020