33 #include <opencv2/imgproc/imgproc.hpp> 38 const std::string & name,
39 const cv::Size & imageSize1,
40 const cv::Mat & K1,
const cv::Mat & D1,
const cv::Mat & R1,
const cv::Mat & P1,
41 const cv::Size & imageSize2,
42 const cv::Mat & K2,
const cv::Mat & D2,
const cv::Mat & R2,
const cv::Mat & P2,
43 const cv::Mat & R,
const cv::Mat & T,
const cv::Mat & E,
const cv::Mat & F,
46 rightSuffix_(
"right"),
47 left_(name+
"_"+leftSuffix_, imageSize1, K1, D1, R1, P1, localTransform),
48 right_(name+
"_"+rightSuffix_, imageSize2, K2, D2, R2, P2, localTransform),
55 UASSERT(
R_.empty() || (
R_.rows == 3 &&
R_.cols == 3 &&
R_.type() == CV_64FC1));
56 UASSERT(
T_.empty() || (
T_.rows == 3 &&
T_.cols == 1 &&
T_.type() == CV_64FC1));
57 UASSERT(
E_.empty() || (
E_.rows == 3 &&
E_.cols == 3 &&
E_.type() == CV_64FC1));
58 UASSERT(
F_.empty() || (
F_.rows == 3 &&
F_.cols == 3 &&
F_.type() == CV_64FC1));
62 const std::string &
name,
71 left_(leftCameraModel),
81 UASSERT(
R_.empty() || (
R_.rows == 3 &&
R_.cols == 3 &&
R_.type() == CV_64FC1));
82 UASSERT(
T_.empty() || (
T_.rows == 3 &&
T_.cols == 1 &&
T_.type() == CV_64FC1));
83 UASSERT(
E_.empty() || (
E_.rows == 3 &&
E_.cols == 3 &&
E_.type() == CV_64FC1));
84 UASSERT(
F_.empty() || (
F_.rows == 3 &&
F_.cols == 3 &&
F_.type() == CV_64FC1));
86 if(!
R_.empty() && !
T_.empty())
90 cv::Mat R1,R2,P1,P2,Q;
96 left_ =
CameraModel(left_.name(), left_.imageSize(), left_.K_raw(), left_.D_raw(), R1, P1, left_.localTransform());
102 const std::string &
name,
108 left_(leftCameraModel),
122 cv::Mat R1,R2,P1,P2,Q;
128 left_ =
CameraModel(left_.name(), left_.imageSize(), left_.K_raw(), left_.D_raw(), R1, P1, left_.localTransform());
140 const cv::Size & imageSize) :
143 left_(fx, fy, cx, cy, localTransform, 0, imageSize),
144 right_(fx, fy, cx, cy, localTransform, baseline*-fx, imageSize)
150 const std::string &
name,
157 const cv::Size & imageSize) :
180 if(leftLoaded && rightLoaded)
182 if(ignoreStereoTransform)
192 std::string filePath = directory+
"/"+cameraName+
"_pose.yaml";
195 UINFO(
"Reading stereo calibration file \"%s\"", filePath.c_str());
196 cv::FileStorage fs(filePath, cv::FileStorage::READ);
200 n = fs[
"camera_name"];
201 if(n.type() != cv::FileNode::NONE)
207 UWARN(
"Missing \"camera_name\" field in \"%s\"", filePath.c_str());
211 n = fs[
"rotation_matrix"];
212 if(n.type() != cv::FileNode::NONE)
214 int rows = (int)n[
"rows"];
215 int cols = (int)n[
"cols"];
216 std::vector<double> data;
218 UASSERT(rows*cols == (
int)data.size());
219 UASSERT(rows == 3 && cols == 3);
220 R_ = cv::Mat(rows, cols, CV_64FC1, data.data()).clone();
224 UWARN(
"Missing \"rotation_matrix\" field in \"%s\"", filePath.c_str());
227 n = fs[
"translation_matrix"];
228 if(n.type() != cv::FileNode::NONE)
230 int rows = (int)n[
"rows"];
231 int cols = (int)n[
"cols"];
232 std::vector<double> data;
234 UASSERT(rows*cols == (
int)data.size());
235 UASSERT(rows == 3 && cols == 1);
236 T_ = cv::Mat(rows, cols, CV_64FC1, data.data()).clone();
240 UWARN(
"Missing \"translation_matrix\" field in \"%s\"", filePath.c_str());
243 n = fs[
"essential_matrix"];
244 if(n.type() != cv::FileNode::NONE)
246 int rows = (int)n[
"rows"];
247 int cols = (int)n[
"cols"];
248 std::vector<double> data;
250 UASSERT(rows*cols == (
int)data.size());
251 UASSERT(rows == 3 && cols == 3);
252 E_ = cv::Mat(rows, cols, CV_64FC1, data.data()).clone();
256 UWARN(
"Missing \"essential_matrix\" field in \"%s\"", filePath.c_str());
259 n = fs[
"fundamental_matrix"];
260 if(n.type() != cv::FileNode::NONE)
262 int rows = (int)n[
"rows"];
263 int cols = (int)n[
"cols"];
264 std::vector<double> data;
266 UASSERT(rows*cols == (
int)data.size());
267 UASSERT(rows == 3 && cols == 3);
268 F_ = cv::Mat(rows, cols, CV_64FC1, data.data()).clone();
272 UWARN(
"Missing \"fundamental_matrix\" field in \"%s\"", filePath.c_str());
281 UWARN(
"Could not load stereo calibration file \"%s\".", filePath.c_str());
290 if(ignoreStereoTransform)
301 std::string filePath = directory+
"/"+
name_+
"_pose.yaml";
302 if(!filePath.empty() && (!
R_.empty() && !
T_.empty()))
304 UINFO(
"Saving stereo calibration to file \"%s\"", filePath.c_str());
305 cv::FileStorage fs(filePath, cv::FileStorage::WRITE);
311 fs <<
"camera_name" <<
name_;
316 fs <<
"rotation_matrix" <<
"{";
317 fs <<
"rows" <<
R_.rows;
318 fs <<
"cols" <<
R_.cols;
319 fs <<
"data" << std::vector<double>((
double*)
R_.data, ((
double*)
R_.data)+(
R_.rows*
R_.cols));
325 fs <<
"translation_matrix" <<
"{";
326 fs <<
"rows" <<
T_.rows;
327 fs <<
"cols" <<
T_.cols;
328 fs <<
"data" << std::vector<double>((
double*)
T_.data, ((
double*)
T_.data)+(
T_.rows*
T_.cols));
334 fs <<
"essential_matrix" <<
"{";
335 fs <<
"rows" <<
E_.rows;
336 fs <<
"cols" <<
E_.cols;
337 fs <<
"data" << std::vector<double>((
double*)
E_.data, ((
double*)
E_.data)+(
E_.rows*
E_.cols));
343 fs <<
"fundamental_matrix" <<
"{";
344 fs <<
"rows" <<
F_.rows;
345 fs <<
"cols" <<
F_.cols;
346 fs <<
"data" << std::vector<double>((
double*)
F_.data, ((
double*)
F_.data)+(
F_.rows*
F_.cols));
356 UERROR(
"Failed saving stereo extrinsics (they are null).");
377 if(disparity == 0.0
f)
408 if(!
R_.empty() && !
T_.empty())
411 R_.at<
double>(0,0),
R_.at<
double>(0,1),
R_.at<
double>(0,2),
T_.at<
double>(0),
412 R_.at<
double>(1,0),
R_.at<
double>(1,1),
R_.at<
double>(1,2),
T_.at<
double>(1),
413 R_.at<
double>(2,0),
R_.at<
double>(2,1),
R_.at<
double>(2,2),
T_.at<
double>(2));
const cv::Size & imageSize() const
const cv::Mat & R() const
const std::string & name() const
Transform stereoTransform() const
bool save(const std::string &directory) const
const cv::Mat & F() const
Some conversion functions.
const std::string & getRightSuffix() const
float computeDepth(float disparity) const
float computeDisparity(float depth) const
#define UASSERT(condition)
bool isValidForProjection() const
const CameraModel & left() const
bool load(const std::string &directory, const std::string &cameraName)
const cv::Mat & T() const
const cv::Mat & E() const
bool saveStereoTransform(const std::string &directory) const
bool isValidForRectification() const
bool save(const std::string &directory, bool ignoreStereoTransform=true) const
CameraModel roi(const cv::Rect &roi) const
void roi(const cv::Rect &roi)
bool load(const std::string &directory, const std::string &cameraName, bool ignoreStereoTransform=true)
const CameraModel & right() const
ULogger class and convenient macros.
void setName(const std::string &name, const std::string &leftSuffix="left", const std::string &rightSuffix="right")
const std::string & getLeftSuffix() const
const Transform & localTransform() const
void setName(const std::string &name)
CameraModel scaled(double scale) const
const Transform & localTransform() const
const std::string & name() const