Skip to content

Commit 9fef09f

Browse files
author
Josh Bradley
authored
Merge pull request opencv#17320 from jgbradley1:add-eigen-tensor-conversions
* add eigen tensor conversion functions * add eigen tensor conversion tests * add support for column major order * update eigen tensor tests * fix coding style and add conditional compilation * fix conditional compilation checks * remove whitespace * rearrange functions for easier reading * reformat function documentation and add tensormap unit test * cleanup documentation of unit test * remove condition duplication * check Eigen major version, not minor version * restrict to Eigen v3.3.0+ * add documentation note and add type checking to cv2eigen_tensormap()
1 parent a9b0305 commit 9fef09f

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

modules/core/include/opencv2/core/eigen.hpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747

4848
#include "opencv2/core.hpp"
4949

50+
#if EIGEN_WORLD_VERSION == 3 && EIGEN_MAJOR_VERSION >= 3
51+
#include <unsupported/Eigen/CXX11/Tensor>
52+
#define OPENCV_EIGEN_TENSOR_SUPPORT
53+
#endif // EIGEN_WORLD_VERSION == 3 && EIGEN_MAJOR_VERSION >= 3
54+
5055
#if defined _MSC_VER && _MSC_VER >= 1200
5156
#pragma warning( disable: 4714 ) //__forceinline is not inlined
5257
#pragma warning( disable: 4127 ) //conditional expression is constant
@@ -59,6 +64,107 @@ namespace cv
5964
//! @addtogroup core_eigen
6065
//! @{
6166

67+
#ifdef OPENCV_EIGEN_TENSOR_SUPPORT
68+
/** @brief Converts an Eigen::Tensor to a cv::Mat.
69+
70+
The method converts an Eigen::Tensor with shape (H x W x C) to a cv::Mat where:
71+
H = number of rows
72+
W = number of columns
73+
C = number of channels
74+
75+
Usage:
76+
\code
77+
Eigen::Tensor<float, 3, Eigen::RowMajor> a_tensor(...);
78+
// populate tensor with values
79+
Mat a_mat;
80+
eigen2cv(a_tensor, a_mat);
81+
\endcode
82+
*/
83+
template <typename _Tp, int _layout> static inline
84+
void eigen2cv( const Eigen::Tensor<_Tp, 3, _layout> &src, OutputArray dst )
85+
{
86+
if( !(_layout & Eigen::RowMajorBit) )
87+
{
88+
const std::array<int, 3> shuffle{2, 1, 0};
89+
Eigen::Tensor<_Tp, 3, !_layout> row_major_tensor = src.swap_layout().shuffle(shuffle);
90+
Mat _src(src.dimension(0), src.dimension(1), CV_MAKETYPE(DataType<_Tp>::type, src.dimension(2)), row_major_tensor.data());
91+
_src.copyTo(dst);
92+
}
93+
else
94+
{
95+
Mat _src(src.dimension(0), src.dimension(1), CV_MAKETYPE(DataType<_Tp>::type, src.dimension(2)), (void *)src.data());
96+
_src.copyTo(dst);
97+
}
98+
}
99+
100+
/** @brief Converts a cv::Mat to an Eigen::Tensor.
101+
102+
The method converts a cv::Mat to an Eigen Tensor with shape (H x W x C) where:
103+
H = number of rows
104+
W = number of columns
105+
C = number of channels
106+
107+
Usage:
108+
\code
109+
Mat a_mat(...);
110+
// populate Mat with values
111+
Eigen::Tensor<float, 3, Eigen::RowMajor> a_tensor(...);
112+
cv2eigen(a_mat, a_tensor);
113+
\endcode
114+
*/
115+
template <typename _Tp, int _layout> static inline
116+
void cv2eigen( const Mat &src, Eigen::Tensor<_Tp, 3, _layout> &dst )
117+
{
118+
if( !(_layout & Eigen::RowMajorBit) )
119+
{
120+
Eigen::Tensor<_Tp, 3, !_layout> row_major_tensor(src.rows, src.cols, src.channels());
121+
Mat _dst(src.rows, src.cols, CV_MAKETYPE(DataType<_Tp>::type, src.channels()), row_major_tensor.data());
122+
if (src.type() == _dst.type())
123+
src.copyTo(_dst);
124+
else
125+
src.convertTo(_dst, _dst.type());
126+
const std::array<int, 3> shuffle{2, 1, 0};
127+
dst = row_major_tensor.swap_layout().shuffle(shuffle);
128+
}
129+
else
130+
{
131+
dst.resize(src.rows, src.cols, src.channels());
132+
Mat _dst(src.rows, src.cols, CV_MAKETYPE(DataType<_Tp>::type, src.channels()), dst.data());
133+
if (src.type() == _dst.type())
134+
src.copyTo(_dst);
135+
else
136+
src.convertTo(_dst, _dst.type());
137+
}
138+
}
139+
140+
/** @brief Maps cv::Mat data to an Eigen::TensorMap.
141+
142+
The method wraps an existing Mat data array with an Eigen TensorMap of shape (H x W x C) where:
143+
H = number of rows
144+
W = number of columns
145+
C = number of channels
146+
147+
Explicit instantiation of the return type is required.
148+
149+
@note Caller should be aware of the lifetime of the cv::Mat instance and take appropriate safety measures.
150+
The cv::Mat instance will retain ownership of the data and the Eigen::TensorMap will lose access when the cv::Mat data is deallocated.
151+
152+
The example below initializes a cv::Mat and produces an Eigen::TensorMap:
153+
\code
154+
float arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
155+
Mat a_mat(2, 2, CV_32FC3, arr);
156+
Eigen::TensorMap<Eigen::Tensor<float, 3, Eigen::RowMajor>> a_tensormap = cv2eigen_tensormap<float>(a_mat);
157+
\endcode
158+
*/
159+
template <typename _Tp> static inline
160+
Eigen::TensorMap<Eigen::Tensor<_Tp, 3, Eigen::RowMajor>> cv2eigen_tensormap(const cv::InputArray &src)
161+
{
162+
Mat mat = src.getMat();
163+
CV_CheckTypeEQ(mat.type(), CV_MAKETYPE(traits::Type<_Tp>::value, mat.channels()), "");
164+
return Eigen::TensorMap<Eigen::Tensor<_Tp, 3, Eigen::RowMajor>>((_Tp *)mat.data, mat.rows, mat.cols, mat.channels());
165+
}
166+
#endif // OPENCV_EIGEN_TENSOR_SUPPORT
167+
62168
template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
63169
void eigen2cv( const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src, OutputArray dst )
64170
{

modules/core/test/test_mat.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,6 +2084,86 @@ TEST(Core_Eigen, eigen2cv_check_Mat_type)
20842084
}
20852085
#endif // HAVE_EIGEN
20862086

2087+
#ifdef OPENCV_EIGEN_TENSOR_SUPPORT
2088+
TEST(Core_Eigen, cv2eigen_check_tensor_conversion)
2089+
{
2090+
Mat A(2, 3, CV_32FC3);
2091+
float value = 0;
2092+
for(int row=0; row<A.rows; row++)
2093+
for(int col=0; col<A.cols; col++)
2094+
for(int ch=0; ch<A.channels(); ch++)
2095+
A.at<Vec3f>(row,col)[ch] = value++;
2096+
2097+
Eigen::Tensor<float, 3, Eigen::RowMajor> row_tensor;
2098+
cv2eigen(A, row_tensor);
2099+
2100+
float* mat_ptr = (float*)A.data;
2101+
float* tensor_ptr = row_tensor.data();
2102+
for (int i=0; i< row_tensor.size(); i++)
2103+
ASSERT_FLOAT_EQ(mat_ptr[i], tensor_ptr[i]);
2104+
2105+
Eigen::Tensor<float, 3, Eigen::ColMajor> col_tensor;
2106+
cv2eigen(A, col_tensor);
2107+
value = 0;
2108+
for(int row=0; row<A.rows; row++)
2109+
for(int col=0; col<A.cols; col++)
2110+
for(int ch=0; ch<A.channels(); ch++)
2111+
ASSERT_FLOAT_EQ(value++, col_tensor(row,col,ch));
2112+
}
2113+
#endif // OPENCV_EIGEN_TENSOR_SUPPORT
2114+
2115+
#ifdef OPENCV_EIGEN_TENSOR_SUPPORT
2116+
TEST(Core_Eigen, eigen2cv_check_tensor_conversion)
2117+
{
2118+
Eigen::Tensor<float, 3, Eigen::RowMajor> row_tensor(2,3,3);
2119+
Eigen::Tensor<float, 3, Eigen::ColMajor> col_tensor(2,3,3);
2120+
float value = 0;
2121+
for(int row=0; row<row_tensor.dimension(0); row++)
2122+
for(int col=0; col<row_tensor.dimension(1); col++)
2123+
for(int ch=0; ch<row_tensor.dimension(2); ch++)
2124+
{
2125+
row_tensor(row,col,ch) = value;
2126+
col_tensor(row,col,ch) = value;
2127+
value++;
2128+
}
2129+
2130+
Mat A;
2131+
eigen2cv(row_tensor, A);
2132+
2133+
float* tensor_ptr = row_tensor.data();
2134+
float* mat_ptr = (float*)A.data;
2135+
for (int i=0; i< row_tensor.size(); i++)
2136+
ASSERT_FLOAT_EQ(tensor_ptr[i], mat_ptr[i]);
2137+
2138+
Mat B;
2139+
eigen2cv(col_tensor, B);
2140+
2141+
value = 0;
2142+
for(int row=0; row<B.rows; row++)
2143+
for(int col=0; col<B.cols; col++)
2144+
for(int ch=0; ch<B.channels(); ch++)
2145+
ASSERT_FLOAT_EQ(value++, B.at<Vec3f>(row,col)[ch]);
2146+
}
2147+
#endif // OPENCV_EIGEN_TENSOR_SUPPORT
2148+
2149+
#ifdef OPENCV_EIGEN_TENSOR_SUPPORT
2150+
TEST(Core_Eigen, cv2eigen_tensormap_check_tensormap_access)
2151+
{
2152+
float arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
2153+
Mat a_mat(2, 2, CV_32FC3, arr);
2154+
Eigen::TensorMap<Eigen::Tensor<float, 3, Eigen::RowMajor>> a_tensor = cv2eigen_tensormap<float>(a_mat);
2155+
2156+
for(int i=0; i<a_mat.rows; i++) {
2157+
for (int j=0; j<a_mat.cols; j++) {
2158+
for (int ch=0; ch<a_mat.channels(); ch++) {
2159+
ASSERT_FLOAT_EQ(a_mat.at<Vec3f>(i,j)[ch], a_tensor(i,j,ch));
2160+
ASSERT_EQ(&a_mat.at<Vec3f>(i,j)[ch], &a_tensor(i,j,ch));
2161+
}
2162+
}
2163+
}
2164+
}
2165+
#endif // OPENCV_EIGEN_TENSOR_SUPPORT
2166+
20872167
TEST(Mat, regression_12943) // memory usage: ~4.5 Gb
20882168
{
20892169
applyTestTag(CV_TEST_TAG_MEMORY_6GB);

0 commit comments

Comments
 (0)