OSVR-Core
cvToEigen.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_cvToEigen_h_GUID_3A0994D8_A408_44B1_411A_C86F3E9332AB
26 #define INCLUDED_cvToEigen_h_GUID_3A0994D8_A408_44B1_411A_C86F3E9332AB
27 
28 // Internal Includes
29 // - none
30 
31 // Library/third-party includes
32 #include <opencv2/calib3d/calib3d.hpp>
33 #include <opencv2/core/core.hpp>
34 #include <opencv2/core/eigen.hpp>
36 
37 // Standard includes
38 // - none
39 
40 namespace osvr {
41 namespace vbtracker {
42  inline Eigen::Quaterniond cvRotVecToQuat(cv::Mat const &vec) {
43  // Inspection of the OpenCV source suggests that the rotation vector
44  // manipulated by the Rodrigues function is of the type where the
45  // magnitude is the angle, and the normalized vector is the axis.
46  // However, converting this way resulted in faulty (incredibly small)
47  // rotations, so the roundabout way of using the Rodrigues OpenCV
48  // function then converting the matrix to a quaternion was used instead.
49  cv::Mat rot;
50  cv::Rodrigues(vec, rot);
51  Eigen::Matrix3d rotMat;
52  cv::cv2eigen(rot, rotMat);
53  return Eigen::Quaterniond(rotMat);
54  }
55 
56  inline cv::Mat eiQuatToRotVec(Eigen::Quaterniond const &q) {
57  cv::Mat rotMatrix;
58  Eigen::Matrix3d eiRotMatrix = q.toRotationMatrix();
59  cv::eigen2cv(eiRotMatrix, rotMatrix);
60  cv::Mat rot;
61  cv::Rodrigues(rotMatrix, rot);
62  return rot;
63  }
64 
65  inline Eigen::Vector3f cvToVector(cv::Point3f const &point) {
66  return Eigen::Vector3f(point.x, point.y, point.z);
67  }
68 
69  inline Eigen::Vector3d cvToVector(cv::Point3d const &point) {
70  return Eigen::Vector3d(point.x, point.y, point.z);
71  }
72 
73  inline Eigen::Vector2f cvToVector(cv::Point2f const &point) {
74  return Eigen::Vector2f(point.x, point.y);
75  }
76 
77  inline Eigen::Vector2d cvToVector(cv::Point2d const &point) {
78  return Eigen::Vector2d(point.x, point.y);
79  }
80 
81  inline Eigen::Vector3d cvToVector3d(cv::InputArray vec) {
82  cv::Mat src = vec.getMat();
83  CV_Assert(src.type() == CV_64FC1);
84  Eigen::Vector3d ret;
85  cv::cv2eigen(src, ret);
86  return ret;
87  }
88  inline cv::Point3f vec3dToCVPoint3f(Eigen::Vector3d const &vec) {
89  return cv::Point3f(vec.x(), vec.y(), vec.z());
90  }
91 
92  inline cv::Point2f vecToPoint(Eigen::Vector2f const &vec) {
93  return cv::Point2f(vec.x(), vec.y());
94  }
95 
97  template <typename Scalar, size_t Rows, size_t Cols>
99  map(cv::Matx<Scalar, Rows, Cols> &cvMatx) {
100  return Eigen::Matrix<Scalar, Rows, Cols>::Map(cvMatx.val);
101  }
102 
105  template <typename Scalar, size_t Rows, size_t Cols>
107  map(cv::Matx<Scalar, Rows, Cols> const &cvMatx) {
108  return Eigen::Matrix<Scalar, Rows, Cols>::Map(cvMatx.val);
109  }
110 } // namespace vbtracker
111 } // namespace osvr
112 
113 #endif // INCLUDED_cvToEigen_h_GUID_3A0994D8_A408_44B1_411A_C86F3E9332AB
Matrix3 toRotationMatrix(void) const
Convert the quaternion to a 3x3 rotation matrix.
Definition: Quaternion.h:296
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:104
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Header wrapping include of <Eigen/Core> and <Eigen/Geometry> for warning quieting.
Definition: Quaternion.h:47
Quaternion< double > Quaterniond
double precision quaternion type
Definition: Quaternion.h:211
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127