45
#ifndef OPENCV_CORE_EIGEN_HPP
46
#define OPENCV_CORE_EIGEN_HPP
48
#ifndef EIGEN_WORLD_VERSION
49
#error "Wrong usage of OpenCV's Eigen utility header. Include Eigen's headers first. See https://github.com/opencv/opencv/issues/17366"
52
#include "opencv2/core.hpp"
54
#if defined _MSC_VER && _MSC_VER >= 1200
56
#pragma warning( disable: 4714 )
57
#pragma warning( disable: 4127 )
58
#pragma warning( disable: 4244 )
61
#if !defined(OPENCV_DISABLE_EIGEN_TENSOR_SUPPORT)
62
#if EIGEN_WORLD_VERSION == 3 && EIGEN_MAJOR_VERSION >= 3 \
63
&& defined(CV_CXX11) && defined(CV_CXX_STD_ARRAY)
64
#include <unsupported/Eigen/CXX11/Tensor>
65
#define OPENCV_EIGEN_TENSOR_SUPPORT 1
83
#if defined(OPENCV_EIGEN_TENSOR_SUPPORT) || defined(CV_DOXYGEN)
99
template
<
typename
_Tp,
int
_layout>
static
inline
100
void
eigen2cv(
const
Eigen::Tensor<_Tp, 3, _layout> &src, OutputArray dst )
102
if( !(_layout & Eigen::RowMajorBit) )
104
const
std::array<int, 3> shuffle{2, 1, 0};
105
Eigen::Tensor<_Tp, 3, !_layout> row_major_tensor = src.swap_layout().shuffle(shuffle);
106
Mat _src(src.dimension(0), src.dimension(1), CV_MAKETYPE(DataType<_Tp>::type, src.dimension(2)), row_major_tensor.data());
111
Mat _src(src.dimension(0), src.dimension(1), CV_MAKETYPE(DataType<_Tp>::type, src.dimension(2)), (
void
*)src.data());
131
template
<
typename
_Tp,
int
_layout>
static
inline
132
void
cv2eigen(
const
Mat &src, Eigen::Tensor<_Tp, 3, _layout> &dst )
134
if( !(_layout & Eigen::RowMajorBit) )
136
Eigen::Tensor<_Tp, 3, !_layout> row_major_tensor(src.rows, src.cols, src.channels());
137
Mat _dst(src.rows, src.cols, CV_MAKETYPE(DataType<_Tp>::type, src.channels()), row_major_tensor.data());
138
if
(src.type() == _dst.type())
141
src.convertTo(_dst, _dst.type());
142
const
std::array<int, 3> shuffle{2, 1, 0};
143
dst = row_major_tensor.swap_layout().shuffle(shuffle);
147
dst.resize(src.rows, src.cols, src.channels());
148
Mat _dst(src.rows, src.cols, CV_MAKETYPE(DataType<_Tp>::type, src.channels()), dst.data());
149
if
(src.type() == _dst.type())
152
src.convertTo(_dst, _dst.type());
175
template
<
typename
_Tp>
static
inline
176Eigen::TensorMap<Eigen::Tensor<_Tp, 3, Eigen::RowMajor>> cv2eigen_tensormap(InputArray src)
178
Mat mat = src.getMat();
179
CV_CheckTypeEQ(mat.type(), CV_MAKETYPE(traits::Type<_Tp>::value, mat.channels()),
"");
180
return
Eigen::TensorMap<Eigen::Tensor<_Tp, 3, Eigen::RowMajor>>((_Tp *)mat.data, mat.rows, mat.cols, mat.channels());
184
template<
typename
_Tp,
int
_rows,
int
_cols,
int
_options,
int
_maxRows,
int
_maxCols>
static
inline
185
void
eigen2cv(
const
Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src, OutputArray dst )
187
if( !(src.Flags & Eigen::RowMajorBit) )
189
Mat _src(src.cols(), src.rows(), traits::Type<_Tp>::value,
190
(
void*)src.data(), src.outerStride()*
sizeof(_Tp));
195
Mat _src(src.rows(), src.cols(), traits::Type<_Tp>::value,
196
(
void*)src.data(), src.outerStride()*
sizeof(_Tp));
202
template<
typename
_Tp,
int
_rows,
int
_cols,
int
_options,
int
_maxRows,
int
_maxCols>
static
inline
203
void
eigen2cv(
const
Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src,
204
Matx<_Tp, _rows, _cols>& dst )
206
if( !(src.Flags & Eigen::RowMajorBit) )
208
dst = Matx<_Tp, _cols, _rows>(
static_cast<
const
_Tp*
>(src.data())).t();
212
dst = Matx<_Tp, _rows, _cols>(
static_cast<
const
_Tp*
>(src.data()));
216
template<
typename
_Tp,
int
_rows,
int
_cols,
int
_options,
int
_maxRows,
int
_maxCols>
static
inline
217
void
cv2eigen(
const
Mat& src,
218
Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst )
221
if( !(dst.Flags & Eigen::RowMajorBit) )
223
const
Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
224
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
225
if( src.type() == _dst.type() )
227
else
if( src.cols == src.rows )
229
src.convertTo(_dst, _dst.type());
233
Mat(src.t()).convertTo(_dst, _dst.type());
237
const
Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
238
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
239
src.convertTo(_dst, _dst.type());
244
template<
typename
_Tp,
int
_rows,
int
_cols,
int
_options,
int
_maxRows,
int
_maxCols>
static
inline
245
void
cv2eigen(
const
Matx<_Tp, _rows, _cols>& src,
246
Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst )
248
if( !(dst.Flags & Eigen::RowMajorBit) )
250
const
Mat _dst(_cols, _rows, traits::Type<_Tp>::value,
251
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
256
const
Mat _dst(_rows, _cols, traits::Type<_Tp>::value,
257
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
258
Mat(src).copyTo(_dst);
262
template<
typename
_Tp>
static
inline
263
void
cv2eigen(
const
Mat& src,
264
Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
266
dst.resize(src.rows, src.cols);
267
if( !(dst.Flags & Eigen::RowMajorBit) )
269
const
Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
270
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
271
if( src.type() == _dst.type() )
273
else
if( src.cols == src.rows )
275
src.convertTo(_dst, _dst.type());
279
Mat(src.t()).convertTo(_dst, _dst.type());
283
const
Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
284
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
285
src.convertTo(_dst, _dst.type());
290
template<
typename
_Tp,
int
_rows,
int
_cols>
static
inline
291
void
cv2eigen(
const
Matx<_Tp, _rows, _cols>& src,
292
Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
294
dst.resize(_rows, _cols);
295
if( !(dst.Flags & Eigen::RowMajorBit) )
297
const
Mat _dst(_cols, _rows, traits::Type<_Tp>::value,
298
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
303
const
Mat _dst(_rows, _cols, traits::Type<_Tp>::value,
304
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
305
Mat(src).copyTo(_dst);
309
template<
typename
_Tp>
static
inline
310
void
cv2eigen(
const
Mat& src,
311
Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
314
dst.resize(src.rows);
316
if( !(dst.Flags & Eigen::RowMajorBit) )
318
const
Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
319
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
320
if( src.type() == _dst.type() )
323
Mat(src.t()).convertTo(_dst, _dst.type());
327
const
Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
328
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
329
src.convertTo(_dst, _dst.type());
334
template<
typename
_Tp,
int
_rows>
static
inline
335
void
cv2eigen(
const
Matx<_Tp, _rows, 1>& src,
336
Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
340
if( !(dst.Flags & Eigen::RowMajorBit) )
342
const
Mat _dst(1, _rows, traits::Type<_Tp>::value,
343
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
348
const
Mat _dst(_rows, 1, traits::Type<_Tp>::value,
349
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
355
template<
typename
_Tp>
static
inline
356
void
cv2eigen(
const
Mat& src,
357
Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
360
dst.resize(src.cols);
361
if( !(dst.Flags & Eigen::RowMajorBit) )
363
const
Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
364
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
365
if( src.type() == _dst.type() )
368
Mat(src.t()).convertTo(_dst, _dst.type());
372
const
Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
373
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
374
src.convertTo(_dst, _dst.type());
379
template<
typename
_Tp,
int
_cols>
static
inline
380
void
cv2eigen(
const
Matx<_Tp, 1, _cols>& src,
381
Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
384
if( !(dst.Flags & Eigen::RowMajorBit) )
386
const
Mat _dst(_cols, 1, traits::Type<_Tp>::value,
387
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
392
const
Mat _dst(1, _cols, traits::Type<_Tp>::value,
393
dst.data(), (
size_t)(dst.outerStride()*
sizeof(_Tp)));
394
Mat(src).copyTo(_dst);
CV_EXPORTS_W void transpose(InputArray src, OutputArray dst)
Transposes a matrix.
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails
Definition:
base.hpp:342
#define CV_DbgAssert(expr)
Definition:
base.hpp:375
"black box" representation of the file storage associated with a file on disk.
Definition:
aruco.hpp:75