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
58#pragma warning( disable: 4714 )
59#pragma warning( disable: 4127 )
60#pragma warning( disable: 4244 )
63#if !defined(OPENCV_DISABLE_EIGEN_TENSOR_SUPPORT)
64#if EIGEN_WORLD_VERSION == 3 && EIGEN_MAJOR_VERSION >= 3
65#include <unsupported/Eigen/CXX11/Tensor>
66#define OPENCV_EIGEN_TENSOR_SUPPORT 1
84#if defined(OPENCV_EIGEN_TENSOR_SUPPORT) || defined(CV_DOXYGEN)
100template <
typename _Tp,
int _layout>
static inline
103 if( !(_layout & Eigen::RowMajorBit) )
106 Eigen::Tensor<_Tp, 3, !_layout> row_major_tensor = src.swap_layout().shuffle(shuffle);
107 Mat _src(src.dimension(0), src.dimension(1),
CV_MAKETYPE(DataType<_Tp>::type, src.dimension(2)), row_major_tensor.data());
112 Mat _src(src.dimension(0), src.dimension(1),
CV_MAKETYPE(DataType<_Tp>::type, src.dimension(2)), (
void *)src.data());
132template <
typename _Tp,
int _layout>
static inline
133void cv2eigen(
const Mat &src, Eigen::Tensor<_Tp, 3, _layout> &
dst )
135 if( !(_layout & Eigen::RowMajorBit) )
137 Eigen::Tensor<_Tp, 3, !_layout> row_major_tensor(src.rows, src.cols, src.channels());
138 Mat _dst(src.rows, src.cols,
CV_MAKETYPE(DataType<_Tp>::type, src.channels()), row_major_tensor.data());
139 if (src.type() == _dst.type())
142 src.convertTo(_dst, _dst.type());
144 dst = row_major_tensor.swap_layout().shuffle(shuffle);
148 dst.resize(src.rows, src.cols, src.channels());
149 Mat _dst(src.rows, src.cols,
CV_MAKETYPE(DataType<_Tp>::type, src.channels()),
dst.data());
150 if (src.type() == _dst.type())
153 src.convertTo(_dst, _dst.type());
176template <
typename _Tp>
static inline
177Eigen::TensorMap<Eigen::Tensor<_Tp, 3, Eigen::RowMajor>> cv2eigen_tensormap(
InputArray src)
179 Mat
mat = src.getMat();
181 return Eigen::TensorMap<Eigen::Tensor<_Tp, 3, Eigen::RowMajor>>((_Tp *)
mat.
data,
mat.
rows,
mat.
cols,
mat.channels());
185template<
typename _Tp,
int _rows,
int _cols,
int _options,
int _maxRows,
int _maxCols>
static inline
188 if( !(src.Flags & Eigen::RowMajorBit) )
191 (
void*)src.data(), src.outerStride()*
sizeof(_Tp));
197 (
void*)src.data(), src.outerStride()*
sizeof(_Tp));
203template<
typename _Tp,
int _rows,
int _cols,
int _options,
int _maxRows,
int _maxCols>
static inline
204void eigen2cv(
const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src,
207 if( !(src.Flags & Eigen::RowMajorBit) )
217template<
typename _Tp,
int _rows,
int _cols,
int _options,
int _maxRows,
int _maxCols>
static inline
219 Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>&
dst )
222 if( !(
dst.Flags & Eigen::RowMajorBit) )
225 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
239 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
245template<
typename _Tp,
int _rows,
int _cols,
int _options,
int _maxRows,
int _maxCols>
static inline
247 Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>&
dst )
249 if( !(
dst.Flags & Eigen::RowMajorBit) )
252 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
258 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
263template<
typename _Tp>
static inline
265 Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>&
dst )
268 if( !(
dst.Flags & Eigen::RowMajorBit) )
271 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
285 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
291template<
typename _Tp,
int _rows,
int _cols>
static inline
293 Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>&
dst )
295 dst.resize(_rows, _cols);
296 if( !(
dst.Flags & Eigen::RowMajorBit) )
299 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
305 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
310template<
typename _Tp>
static inline
312 Eigen::Matrix<_Tp, Eigen::Dynamic, 1>&
dst )
317 if( !(
dst.Flags & Eigen::RowMajorBit) )
320 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
329 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
335template<
typename _Tp,
int _rows>
static inline
337 Eigen::Matrix<_Tp, Eigen::Dynamic, 1>&
dst )
341 if( !(
dst.Flags & Eigen::RowMajorBit) )
344 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
350 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
356template<
typename _Tp>
static inline
358 Eigen::Matrix<_Tp, 1, Eigen::Dynamic>&
dst )
362 if( !(
dst.Flags & Eigen::RowMajorBit) )
365 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
374 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
380template<
typename _Tp,
int _cols>
static inline
382 Eigen::Matrix<_Tp, 1, Eigen::Dynamic>&
dst )
385 if( !(
dst.Flags & Eigen::RowMajorBit) )
388 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
394 dst.data(), (
size_t)(
dst.outerStride()*
sizeof(_Tp)));
n-dimensional dense array class
Definition mat.hpp:812
void copyTo(OutputArray m) const
Copies the matrix to another one.
int cols
Definition mat.hpp:2138
MatExpr t() const
Transposes a matrix.
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition mat.hpp:2138
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
Converts an array to another data type with optional scaling.
int type() const
Returns the type of a matrix element.
Template class for small matrices whose type and size are known at compilation time.
Definition matx.hpp:100
Matx< _Tp, n, m > t() const
transpose the matrix
Definition matx.inl.hpp:504
This type is very similar to InputArray except that it is used for input/output and output function p...
Definition mat.hpp:296
CV_EXPORTS_W void transpose(InputArray src, OutputArray dst)
Transposes a matrix.
const _OutputArray & OutputArray
Definition mat.hpp:444
CV__DEBUG_NS_END typedef const _InputArray & InputArray
Definition mat.hpp:442
CvArr const CvMat * mat
Definition core_c.h:1308
static void cv2eigen(const Mat &src, Eigen::Matrix< _Tp, _rows, _cols, _options, _maxRows, _maxCols > &dst)
Definition eigen.hpp:218
static void eigen2cv(const Eigen::Matrix< _Tp, _rows, _cols, _options, _maxRows, _maxCols > &src, OutputArray dst)
Definition eigen.hpp:186
#define CV_MAKETYPE(depth, cn)
Definition interface.h:85
#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
OutputArray dst
Definition imgproc.hpp:3564
"black box" representation of the file storage associated with a file on disk.
Definition calib3d.hpp:441
int rows
Definition types_c.h:485
int type
Definition types_c.h:456
int cols
Definition types_c.h:486
Definition traits.hpp:386
@ value
Definition traits.hpp:386