RTAB-Map 0.23.10
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
CameraModel.h
1/*
2Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the Universite de Sherbrooke nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifndef CAMERAMODEL_H_
29#define CAMERAMODEL_H_
30
31#include <opencv2/opencv.hpp>
32
33#include "rtabmap/core/rtabmap_core_export.h" // DLL export/import defines
34#include "rtabmap/core/Transform.h"
35
36namespace rtabmap {
37
52class RTABMAP_CORE_EXPORT CameraModel
53{
54public:
63 static Transform opticalRotation() {return Transform(0,0,1,0, -1,0,0,0, 0,-1,0,0);}
64
65public:
66
69
86 const std::string & name,
87 const cv::Size & imageSize,
88 const cv::Mat & K,
89 const cv::Mat & D,
90 const cv::Mat & R,
91 const cv::Mat & P,
92 const Transform & localTransform = opticalRotation());
93
106 double fx,
107 double fy,
108 double cx,
109 double cy,
110 const Transform & localTransform = opticalRotation(),
111 double Tx = 0.0f,
112 const cv::Size & imageSize = cv::Size(0,0));
113
118 const std::string & name,
119 double fx,
120 double fy,
121 double cx,
122 double cy,
123 const Transform & localTransform = opticalRotation(),
124 double Tx = 0.0f,
125 const cv::Size & imageSize = cv::Size(0,0));
126
128 virtual ~CameraModel() {}
129
154
159 bool isRectificationMapInitialized() const {return !mapX_.empty() && !mapY_.empty();}
160
164 bool isValidForProjection() const {return fx()>0.0 && fy()>0.0 && cx()>0.0 && cy()>0.0;}
168 bool isValidForReprojection() const {return fx()>0.0 && fy()>0.0 && cx()>0.0 && cy()>0.0 && imageWidth()>0 && imageHeight()>0;}
173 {
174 return imageSize_.width>0 &&
175 imageSize_.height>0 &&
176 !K_.empty() &&
177 !D_.empty() &&
178 !R_.empty() &&
179 !P_.empty();
180 }
181
183 void setName(const std::string & name) {name_=name;}
185 const std::string & name() const {return name_;}
186
188 double fx() const {return P_.empty()?K_.empty()?0.0:K_.at<double>(0,0):P_.at<double>(0,0);}
190 double fy() const {return P_.empty()?K_.empty()?0.0:K_.at<double>(1,1):P_.at<double>(1,1);}
192 double cx() const {return P_.empty()?K_.empty()?0.0:K_.at<double>(0,2):P_.at<double>(0,2);}
194 double cy() const {return P_.empty()?K_.empty()?0.0:K_.at<double>(1,2):P_.at<double>(1,2);}
196 double Tx() const {return P_.empty()?0.0:P_.at<double>(0,3);}
197
199 cv::Mat K_raw() const {return K_;}
201 cv::Mat D_raw() const {return D_;}
203 cv::Mat K() const {return !P_.empty()?P_.colRange(0,3):K_;}
205 cv::Mat D() const {return P_.empty()&&!D_.empty()?D_:cv::Mat::zeros(1,5,CV_64FC1);} // if P exists, return rectified version
207 cv::Mat R() const {return R_;}
209 cv::Mat P() const {return P_;}
210
212 void setLocalTransform(const Transform & transform) {localTransform_ = transform;}
214 const Transform & localTransform() const {return localTransform_;}
215
235 void setImageSize(const cv::Size & size);
236 const cv::Size & imageSize() const {return imageSize_;}
237 int imageWidth() const {return imageSize_.width;}
238 int imageHeight() const {return imageSize_.height;}
239
250 double fovX() const;
251
262 double fovY() const;
263
271 double horizontalFOV() const;
272
280 double verticalFOV() const;
281
283 bool isFisheye() const {return D_.cols == 6;}
284
315 bool load(const std::string & filePath, bool initRectificationMaps = true);
316
329 bool load(const std::string & directory, const std::string & cameraName, bool initRectificationMaps = true);
330
354 bool save(const std::string & directory) const;
355
377 std::vector<unsigned char> serialize() const;
389 unsigned int deserialize(const std::vector<unsigned char>& data);
390
410 unsigned int deserialize(const unsigned char * data, unsigned int dataSize);
411
426 CameraModel scaled(double scale) const;
427
442 CameraModel roi(const cv::Rect & roi) const;
443
463 cv::Mat rectifyImage(const cv::Mat & raw, int interpolation = cv::INTER_LINEAR) const;
464
488 cv::Mat rectifyDepth(const cv::Mat & raw) const;
489
507 void project(float u, float v, float depth, float & x, float & y, float & z) const;
508
525 void reproject(float x, float y, float z, float & u, float & v) const;
526
542 void reproject(float x, float y, float z, int & u, int & v) const;
543
553 bool inFrame(int u, int v) const;
554
555private:
556 std::string name_;
557 cv::Size imageSize_;
558 cv::Mat K_;
559 cv::Mat D_;
560 cv::Mat R_;
561 cv::Mat P_;
562 cv::Mat mapX_;
563 cv::Mat mapY_;
564 Transform localTransform_;
565};
566
590RTABMAP_CORE_EXPORT std::ostream& operator<<(std::ostream& os, const CameraModel& model);
591
592} /* namespace rtabmap */
593#endif /* CAMERAMODEL_H_ */
Represents a pinhole camera model containing intrinsic and extrinsic parameters, used for projection,...
Definition CameraModel.h:53
void project(float u, float v, float depth, float &x, float &y, float &z) const
Projects a 2D pixel and depth value into a 3D point in the camera coordinate frame (/camera_link).
static Transform opticalRotation()
Returns the default optical rotation to convert image coordinates to robot coordinates.
Definition CameraModel.h:63
double horizontalFOV() const
Returns the horizontal field of view in degrees.
cv::Mat rectifyImage(const cv::Mat &raw, int interpolation=cv::INTER_LINEAR) const
Rectifies a raw image using the precomputed rectification maps.
unsigned int deserialize(const unsigned char *data, unsigned int dataSize)
Deserializes a camera model from a raw byte buffer.
cv::Mat rectifyDepth(const cv::Mat &raw) const
Rectifies a raw depth image using the precomputed rectification maps.
bool load(const std::string &filePath, bool initRectificationMaps=true)
Loads the camera model parameters from a YAML calibration file.
CameraModel roi(const cv::Rect &roi) const
Returns a new camera model adjusted for a given region of interest (ROI).
bool isFisheye() const
Checks if the distortion model is fisheye (6 coefficients: k1,k2,0,0,k3,k4).
void setName(const std::string &name)
Sets the camera name, used to set a camera name when saving to a file.
void setImageSize(const cv::Size &size)
Sets the image size of the camera model and updates the principal point if undefined.
bool isValidForRectification() const
Checks if the model has sufficient data for image rectification.
CameraModel(const std::string &name, const cv::Size &imageSize, const cv::Mat &K, const cv::Mat &D, const cv::Mat &R, const cv::Mat &P, const Transform &localTransform=opticalRotation())
Constructor using full camera parameters.
unsigned int deserialize(const std::vector< unsigned char > &data)
Deserializes a camera model from a byte vector.
cv::Mat D() const
Returns the rectified distortion coefficients (1x5 filled with zeros) if the projection matrix P exis...
bool initRectificationMap()
Initializes the rectification maps used to undistort and rectify images.
std::vector< unsigned char > serialize() const
Serializes the camera model to a binary format.
cv::Mat K() const
Returns the rectified camera intrinsic matrix if the projection matrix P exists, otherwise returns th...
void reproject(float x, float y, float z, float &u, float &v) const
Reprojects a 3D point in the camera frame (/camera_link) into 2D image coordinates (floating-point).
double Tx() const
Returns the x translation (usually fx * baseline in case of stereo, otherwise would be 0).
CameraModel(const std::string &name, double fx, double fy, double cx, double cy, const Transform &localTransform=opticalRotation(), double Tx=0.0f, const cv::Size &imageSize=cv::Size(0, 0))
Minimal constructor with name for saving.
bool isValidForProjection() const
Checks if the model is valid for 2D->3D projection.
bool isRectificationMapInitialized() const
Checks if the rectification map has been initialized.
cv::Mat D_raw() const
Returns the raw distortion coefficients (before rectification).
const Transform & localTransform() const
Returns the local transform (base frame to optical frame).
void reproject(float x, float y, float z, int &u, int &v) const
Reprojects a 3D point in the camera frame (/camera_link) into 2D image coordinates (rounded to int).
bool inFrame(int u, int v) const
Checks if a given pixel coordinate lies within the image bounds.
void setLocalTransform(const Transform &transform)
Sets the local transform of the camera (base frame to optical frame).
const std::string & name() const
Returns the camera name.
double cy() const
Returns principal point y.
CameraModel(double fx, double fy, double cx, double cy, const Transform &localTransform=opticalRotation(), double Tx=0.0f, const cv::Size &imageSize=cv::Size(0, 0))
Minimal constructor using intrinsic parameters. This assumes the images are already rectified.
bool save(const std::string &directory) const
Saves the camera model parameters to a YAML calibration file in ROS format.
bool isValidForReprojection() const
Checks if the model is valid for 3D->2D reprojection.
bool load(const std::string &directory, const std::string &cameraName, bool initRectificationMaps=true)
Loads the camera model by constructing a file path from a directory and camera name.
double fovX() const
Returns the horizontal field of view (FoV) in radians.
double fy() const
Returns focal length in y.
cv::Mat P() const
Returns the projection matrix.
double fx() const
Returns focal length in x.
cv::Mat R() const
Returns the rectification matrix.
cv::Mat K_raw() const
Returns the raw intrinsic matrix (before rectification).
CameraModel()
Default constructor.
CameraModel scaled(double scale) const
Returns a new camera model with all intrinsic parameters scaled by a given factor.
double fovY() const
Returns the vertical field of view (FoV) in radians.
double verticalFOV() const
Returns the vertical field of view in degrees.
virtual ~CameraModel()
Destructor.
double cx() const
Returns principal point x.
Represents a 3D rigid body transformation (rotation + translation).
Definition Transform.h:53
RTABMAP_CORE_EXPORT std::ostream & operator<<(std::ostream &os, const CameraModel &model)
Stream operator for printing a camera model to an output stream.