ivt_calibration.cpp
Go to the documentation of this file.
1 
19 #include <boost/make_shared.hpp>
20 
21 #include <iostream>
22 
23 namespace ivt_bridge {
24 
25 cv::Mat_<double> matx34ToMat_(const cv::Matx<double, 3,4> &matx) {
26  cv::Mat_<double>M(3,4);
27  for (int i = 0; i < M.rows; ++i) {
28  for (int j = 0; j < M.cols; ++j) {
29  M(i,j) = matx(i,j);
30  }
31  }
32  return M;
33 }
34 
35 cv::Mat_<double> matx33ToMat_(const cv::Matx<double, 3,3> &matx) {
36  cv::Mat_<double>M(3,3);
37  for (int i = 0; i < M.rows; ++i) {
38  for (int j = 0; j < M.cols; ++j) {
39  M(i,j) = matx(i,j);
40  }
41  }
42  return M;
43 }
44 
45 cv::Mat_<double> matx35ToMat_(const cv::Matx<double, 3,5> &matx) {
46  cv::Mat_<double>M(3,5);
47  for (int i = 0; i < M.rows; ++i) {
48  for (int j = 0; j < M.cols; ++j) {
49  M(i,j) = matx(i,j);
50  }
51  }
52  return M;
53 }
54 
55 
57  boost::shared_ptr<CCalibration> calibration = boost::make_shared<CCalibration>();
58 
59  Mat3d ivtR; Vec3d ivtt;
60  // Set with + height
61  calibration->SetCameraParameters(0, 0, 0, 0, 0, 0, 0, 0, ivtR, ivtt, cam_model.rawRoi().width, cam_model.rawRoi().height);
62 
63  cv::Mat_<double> projectionMatrix = matx34ToMat_(cam_model.projectionMatrix());
64  cv::Mat_<double> intrinsicMatrix = matx33ToMat_(cam_model.intrinsicMatrix());
65  cv::Mat_<double> R = matx33ToMat_(cam_model.rotationMatrix());
66  cv::Mat_<double> t;
67  const cv::Mat_<double> distortion;
68 
69  if (forRectifiedImages) {
70  cv::decomposeProjectionMatrix(projectionMatrix, intrinsicMatrix, R, t);
71  // IVT calibrates in mm while ROS does it in m. (-1) determined empirical.
72  t = t * -1000;
73  calibration->SetDistortion(0, 0, 0, 0);
74  } else {
75  //intrinsicMatrix = cam_model.intrinsicMatrix();
76  // Rec = P[0,3]^(-1) * H * K (src:Learning OpenCV Book) => H = P[0,3] * Rec * K^(-1)
77  const cv::Mat_<double> H = projectionMatrix.colRange(0,3) * R * intrinsicMatrix.inv();
78  // P^(*) = H * P = H * K * [R|t] (src: Image processing, Analysis, and Machine vision) => [R|t] = K^(-1) * H^(-1) * P^(*)
79  const cv::Mat_<double> Rt = intrinsicMatrix.inv() * H.inv() * projectionMatrix;
80  R = Rt.colRange(0,3);
81  // IVT calibrates in mm while ROS does it in m.
82  t = Rt.col(3) * 1000;
83  cv::Mat_<double> distortion = cam_model.distortionCoeffs();
84  calibration->SetDistortion(distortion(0,0), distortion(0,1), distortion(0,2), distortion(0,3));
85  }
86 
87  calibration->SetIntrinsicBase(intrinsicMatrix(0,2), intrinsicMatrix(1,2), intrinsicMatrix(0,0), intrinsicMatrix(1,1));
88 
89  Math3d::SetMat(ivtR,
90  R(0,0), R(0,1), R(0,2),
91  R(1,0), R(1,1), R(1,2),
92  R(2,0), R(2,1), R(2,2));
93  calibration->SetRotation(ivtR);
94 
95  Math3d::SetVec(ivtt, t(0,0), t(1,0), t(2,0));
96  calibration->SetTranslation(ivtt);
97 
98  return calibration;
99 
100 }
101 
102 bool IvtCalibration::fromCameraInfo(const sensor_msgs::CameraInfo& msg){
103  return cam_model.fromCameraInfo(msg);
104 }
105 
106 bool IvtCalibration::fromCameraInfo(const sensor_msgs::CameraInfoConstPtr& msg){
107  return cam_model.fromCameraInfo(msg);
108 }
109 
111  return calibrationImpl(cam_model, forRectifiedImages);
112 }
113 
114 bool IvtStereoCalibration::fromCameraInfo(const sensor_msgs::CameraInfo& left, const sensor_msgs::CameraInfo& right){
115  return cam_model.fromCameraInfo(left, right);
116 }
117 
118 bool IvtStereoCalibration::fromCameraInfo(const sensor_msgs::CameraInfoConstPtr& left, const sensor_msgs::CameraInfoConstPtr& right){
119  return cam_model.fromCameraInfo(left, right);
120 }
121 
123  boost::shared_ptr<CStereoCalibration> stereoCalibration = boost::make_shared<CStereoCalibration>();
124  stereoCalibration->SetSingleCalibrations(*calibrationImpl(cam_model.left(), forRectifiedImages), *calibrationImpl(cam_model.right(), forRectifiedImages), true);
125 
126  Mat3d Hl, Hr;
127  cv::Mat_<double> projectionMatrixLeft = matx34ToMat_(cam_model.left().projectionMatrix());
128  cv::Mat_<double> projectionMatrixRight = matx34ToMat_(cam_model.right().projectionMatrix());
129  cv::Mat_<double> intrinsicMatrixLeft = matx33ToMat_(cam_model.left().intrinsicMatrix());
130  cv::Mat_<double> intrinsicMatrixRight = matx33ToMat_(cam_model.right().intrinsicMatrix());
131  cv::Mat_<double> RLeft = matx33ToMat_(cam_model.left().rotationMatrix());
132  cv::Mat_<double> RRight = matx33ToMat_(cam_model.right().rotationMatrix());
133 
134  if (forRectifiedImages) {
135  // use identity
136  Math3d::SetMat(Hl, 1,0,0,0,1,0,0,0,1);
137  Math3d::SetMat(Hr, 1,0,0,0,1,0,0,0,1);
138  } else {
139  // ivt saves H inverted
140  // Rec = P[0,3]^(-1) * H * K (src:Learning OpenCV Book) => H = P[0,3] * Rec * K^(-1)
141  const cv::Mat_<double> rectificationMatrixLeft = (projectionMatrixLeft.colRange(0,3) * RLeft * intrinsicMatrixLeft.inv()).inv();
142  Math3d::SetMat(Hl,
143  rectificationMatrixLeft(0,0), rectificationMatrixLeft(0,1), rectificationMatrixLeft(0,2),
144  rectificationMatrixLeft(1,0), rectificationMatrixLeft(1,1), rectificationMatrixLeft(1,2),
145  rectificationMatrixLeft(2,0), rectificationMatrixLeft(2,1), rectificationMatrixLeft(2,2));
146 
147  const cv::Mat_<double> rectificationMatrixRight = (projectionMatrixRight.colRange(0,3) * RRight * intrinsicMatrixRight.inv()).inv();
148  Math3d::SetMat(Hr,
149  rectificationMatrixRight(0,0), rectificationMatrixRight(0,1), rectificationMatrixRight(0,2),
150  rectificationMatrixRight(1,0), rectificationMatrixRight(1,1), rectificationMatrixRight(1,2),
151  rectificationMatrixRight(2,0), rectificationMatrixRight(2,1), rectificationMatrixRight(2,2));
152  }
153 
154  stereoCalibration->rectificationHomographyLeft = Hl;
155  stereoCalibration->rectificationHomographyRight = Hr;
156 
157  return stereoCalibration;
158 }
159 
160 
161 } //namespace ivt_bridge
const cv::Matx33d & intrinsicMatrix() const
image_geometry::PinholeCameraModel cam_model
boost::shared_ptr< CCalibration > calibrationImpl(const image_geometry::PinholeCameraModel &cam_model, bool forRectifiedImages)
cv::Mat_< double > matx33ToMat_(const cv::Matx< double, 3, 3 > &matx)
boost::shared_ptr< CCalibration > getCalibration(bool forRectifiedImages=false) const
Get the IVT Calibration.
cv::Mat_< double > matx35ToMat_(const cv::Matx< double, 3, 5 > &matx)
const cv::Matx33d & rotationMatrix() const
bool fromCameraInfo(const sensor_msgs::CameraInfo &left, const sensor_msgs::CameraInfo &right)
Set the camera parameters from the sensor_msgs/CameraInfo messages.
bool fromCameraInfo(const sensor_msgs::CameraInfo &msg)
boost::shared_ptr< CStereoCalibration > getStereoCalibration(bool forRectifiedImages=false) const
Get the IVT StereoCalibration.
bool fromCameraInfo(const sensor_msgs::CameraInfo &msg)
Set the camera parameters from the sensor_msgs/CameraInfo messages.
const cv::Matx34d & projectionMatrix() const
const cv::Mat_< double > & distortionCoeffs() const
cv::Mat_< double > matx34ToMat_(const cv::Matx< double, 3, 4 > &matx)


asr_ivt_bridge
Author(s): Hutmacher Robin, Kleinert Daniel, Meißner Pascal
autogenerated on Mon Jun 10 2019 12:39:24