-
Notifications
You must be signed in to change notification settings - Fork 0
/
perspective_camera_model.cpp
59 lines (49 loc) · 1.72 KB
/
perspective_camera_model.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "perspective_camera_model.h"
#include "opencv2/core/eigen.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/calib3d.hpp"
PerspectiveCameraModel::PerspectiveCameraModel(const Eigen::Matrix3d& K,
const Sophus::SE3d& pose_world_camera,
const Vector5d& distortion_coeffs)
: K_{K}
, pose_world_camera_{pose_world_camera}
, distortion_coeffs_{distortion_coeffs}
{
camera_projection_matrix_ = computeCameraProjectionMatrix();
}
Sophus::SE3d PerspectiveCameraModel::getPose() const
{
return pose_world_camera_;
}
Eigen::Matrix3d PerspectiveCameraModel::getCalibrationMatrix() const
{
return K_;
}
PerspectiveCameraModel::Matrix34d PerspectiveCameraModel::getCameraProjectionMatrix() const
{
return camera_projection_matrix_;
}
PerspectiveCameraModel::Matrix34d PerspectiveCameraModel::computeCameraProjectionMatrix()
{
return K_ * pose_world_camera_.inverse().matrix3x4();
}
Eigen::Vector2d PerspectiveCameraModel::projectWorldPoint(Eigen::Vector3d world_point) const
{
return (camera_projection_matrix_ * world_point.homogeneous()).hnormalized();
}
Eigen::Matrix2Xd PerspectiveCameraModel::projectWorldPoints(Eigen::Matrix3Xd world_points) const
{
return (camera_projection_matrix_ * world_points.colwise().homogeneous()).colwise().hnormalized();
}
cv::Mat PerspectiveCameraModel::undistortImage(cv::Mat distorted_image) const
{
// Convert to cv::Mats
cv::Mat K_cv;
cv::eigen2cv(K_, K_cv);
cv::Mat dist_coeffs_cv;
cv::eigen2cv(distortion_coeffs_, dist_coeffs_cv);
// Undistort image.
cv::Mat undistorted_image;
cv::undistort(distorted_image, undistorted_image, K_cv, dist_coeffs_cv);
return undistorted_image;
}