31#include <opencv2/opencv.hpp>
33#include "rtabmap/core/rtabmap_core_export.h"
34#include "rtabmap/core/Transform.h"
63 static Transform opticalRotation() {
return Transform(0,0,1,0, -1,0,0,0, 0,-1,0,0);}
86 const std::string & name,
87 const cv::Size & imageSize,
92 const Transform & localTransform = opticalRotation());
110 const Transform & localTransform = opticalRotation(),
112 const cv::Size & imageSize = cv::Size(0,0));
118 const std::string & name,
123 const Transform & localTransform = opticalRotation(),
125 const cv::Size & imageSize = cv::Size(0,0));
168 bool isValidForReprojection()
const {
return fx()>0.0 && fy()>0.0 && cx()>0.0 && cy()>0.0 && imageWidth()>0 && imageHeight()>0;}
174 return imageSize_.width>0 &&
175 imageSize_.height>0 &&
183 void setName(
const std::string & name) {name_=name;}
185 const std::string &
name()
const {
return name_;}
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);}
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);}
207 cv::Mat
R()
const {
return R_;}
209 cv::Mat
P()
const {
return P_;}
236 const cv::Size & imageSize()
const {
return imageSize_;}
237 int imageWidth()
const {
return imageSize_.width;}
238 int imageHeight()
const {
return imageSize_.height;}
315 bool load(
const std::string & filePath,
bool initRectificationMaps =
true);
329 bool load(
const std::string & directory,
const std::string & cameraName,
bool initRectificationMaps =
true);
354 bool save(
const std::string & directory)
const;
410 unsigned int deserialize(
const unsigned char * data,
unsigned int dataSize);
463 cv::Mat
rectifyImage(
const cv::Mat & raw,
int interpolation = cv::INTER_LINEAR)
const;
507 void project(
float u,
float v,
float depth,
float & x,
float & y,
float & z)
const;
525 void reproject(
float x,
float y,
float z,
float & u,
float & v)
const;
542 void reproject(
float x,
float y,
float z,
int & u,
int & v)
const;
Represents a pinhole camera model containing intrinsic and extrinsic parameters, used for projection,...
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.
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.
RTABMAP_CORE_EXPORT std::ostream & operator<<(std::ostream &os, const CameraModel &model)
Stream operator for printing a camera model to an output stream.