eigen.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #ifndef OPENCV_CORE_EIGEN_HPP
  44. #define OPENCV_CORE_EIGEN_HPP
  45. #ifndef EIGEN_WORLD_VERSION
  46. #error "Wrong usage of OpenCV's Eigen utility header. Include Eigen's headers first. See https://github.com/opencv/opencv/issues/17366"
  47. #endif
  48. #include "opencv2/core.hpp"
  49. #if defined _MSC_VER && _MSC_VER >= 1200
  50. #ifndef NOMINMAX
  51. #define NOMINMAX // fix https://github.com/opencv/opencv/issues/17548
  52. #endif
  53. #pragma warning( disable: 4714 ) //__forceinline is not inlined
  54. #pragma warning( disable: 4127 ) //conditional expression is constant
  55. #pragma warning( disable: 4244 ) //conversion from '__int64' to 'int', possible loss of data
  56. #endif
  57. #if !defined(OPENCV_DISABLE_EIGEN_TENSOR_SUPPORT)
  58. #if EIGEN_WORLD_VERSION == 3 && EIGEN_MAJOR_VERSION >= 3
  59. #include <unsupported/Eigen/CXX11/Tensor>
  60. #define OPENCV_EIGEN_TENSOR_SUPPORT 1
  61. #endif // EIGEN_WORLD_VERSION == 3 && EIGEN_MAJOR_VERSION >= 3
  62. #endif // !defined(OPENCV_DISABLE_EIGEN_TENSOR_SUPPORT)
  63. namespace cv
  64. {
  65. /** @addtogroup core_eigen
  66. These functions are provided for OpenCV-Eigen interoperability. They convert `Mat`
  67. objects to corresponding `Eigen::Matrix` objects and vice-versa. Consult the [Eigen
  68. documentation](https://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html) for
  69. information about the `Matrix` template type.
  70. @note Using these functions requires the `Eigen/Dense` or similar header to be
  71. included before this header.
  72. */
  73. //! @{
  74. #if defined(OPENCV_EIGEN_TENSOR_SUPPORT) || defined(CV_DOXYGEN)
  75. /** @brief Converts an Eigen::Tensor to a cv::Mat.
  76. The method converts an Eigen::Tensor with shape (H x W x C) to a cv::Mat where:
  77. H = number of rows
  78. W = number of columns
  79. C = number of channels
  80. Usage:
  81. \code
  82. Eigen::Tensor<float, 3, Eigen::RowMajor> a_tensor(...);
  83. // populate tensor with values
  84. Mat a_mat;
  85. eigen2cv(a_tensor, a_mat);
  86. \endcode
  87. */
  88. template <typename _Tp, int _layout> static inline
  89. void eigen2cv( const Eigen::Tensor<_Tp, 3, _layout> &src, OutputArray dst )
  90. {
  91. if( !(_layout & Eigen::RowMajorBit) )
  92. {
  93. const std::array<int, 3> shuffle{2, 1, 0};
  94. Eigen::Tensor<_Tp, 3, !_layout> row_major_tensor = src.swap_layout().shuffle(shuffle);
  95. Mat _src(src.dimension(0), src.dimension(1), CV_MAKETYPE(DataType<_Tp>::type, src.dimension(2)), row_major_tensor.data());
  96. _src.copyTo(dst);
  97. }
  98. else
  99. {
  100. Mat _src(src.dimension(0), src.dimension(1), CV_MAKETYPE(DataType<_Tp>::type, src.dimension(2)), (void *)src.data());
  101. _src.copyTo(dst);
  102. }
  103. }
  104. /** @brief Converts a cv::Mat to an Eigen::Tensor.
  105. The method converts a cv::Mat to an Eigen Tensor with shape (H x W x C) where:
  106. H = number of rows
  107. W = number of columns
  108. C = number of channels
  109. Usage:
  110. \code
  111. Mat a_mat(...);
  112. // populate Mat with values
  113. Eigen::Tensor<float, 3, Eigen::RowMajor> a_tensor(...);
  114. cv2eigen(a_mat, a_tensor);
  115. \endcode
  116. */
  117. template <typename _Tp, int _layout> static inline
  118. void cv2eigen( const Mat &src, Eigen::Tensor<_Tp, 3, _layout> &dst )
  119. {
  120. if( !(_layout & Eigen::RowMajorBit) )
  121. {
  122. Eigen::Tensor<_Tp, 3, !_layout> row_major_tensor(src.rows, src.cols, src.channels());
  123. Mat _dst(src.rows, src.cols, CV_MAKETYPE(DataType<_Tp>::type, src.channels()), row_major_tensor.data());
  124. if (src.type() == _dst.type())
  125. src.copyTo(_dst);
  126. else
  127. src.convertTo(_dst, _dst.type());
  128. const std::array<int, 3> shuffle{2, 1, 0};
  129. dst = row_major_tensor.swap_layout().shuffle(shuffle);
  130. }
  131. else
  132. {
  133. dst.resize(src.rows, src.cols, src.channels());
  134. Mat _dst(src.rows, src.cols, CV_MAKETYPE(DataType<_Tp>::type, src.channels()), dst.data());
  135. if (src.type() == _dst.type())
  136. src.copyTo(_dst);
  137. else
  138. src.convertTo(_dst, _dst.type());
  139. }
  140. }
  141. /** @brief Maps cv::Mat data to an Eigen::TensorMap.
  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. Explicit instantiation of the return type is required.
  147. @note Caller should be aware of the lifetime of the cv::Mat instance and take appropriate safety measures.
  148. The cv::Mat instance will retain ownership of the data and the Eigen::TensorMap will lose access when the cv::Mat data is deallocated.
  149. The example below initializes a cv::Mat and produces an Eigen::TensorMap:
  150. \code
  151. float arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  152. Mat a_mat(2, 2, CV_32FC3, arr);
  153. Eigen::TensorMap<Eigen::Tensor<float, 3, Eigen::RowMajor>> a_tensormap = cv2eigen_tensormap<float>(a_mat);
  154. \endcode
  155. */
  156. template <typename _Tp> static inline
  157. Eigen::TensorMap<Eigen::Tensor<_Tp, 3, Eigen::RowMajor>> cv2eigen_tensormap(InputArray src)
  158. {
  159. Mat mat = src.getMat();
  160. CV_CheckTypeEQ(mat.type(), CV_MAKETYPE(traits::Type<_Tp>::value, mat.channels()), "");
  161. return Eigen::TensorMap<Eigen::Tensor<_Tp, 3, Eigen::RowMajor>>((_Tp *)mat.data, mat.rows, mat.cols, mat.channels());
  162. }
  163. #endif // OPENCV_EIGEN_TENSOR_SUPPORT
  164. template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
  165. void eigen2cv( const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src, OutputArray dst )
  166. {
  167. if( !(src.Flags & Eigen::RowMajorBit) )
  168. {
  169. Mat _src(src.cols(), src.rows(), traits::Type<_Tp>::value,
  170. (void*)src.data(), src.outerStride()*sizeof(_Tp));
  171. transpose(_src, dst);
  172. }
  173. else
  174. {
  175. Mat _src(src.rows(), src.cols(), traits::Type<_Tp>::value,
  176. (void*)src.data(), src.outerStride()*sizeof(_Tp));
  177. _src.copyTo(dst);
  178. }
  179. }
  180. // Matx case
  181. template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
  182. void eigen2cv( const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src,
  183. Matx<_Tp, _rows, _cols>& dst )
  184. {
  185. if( !(src.Flags & Eigen::RowMajorBit) )
  186. {
  187. dst = Matx<_Tp, _cols, _rows>(static_cast<const _Tp*>(src.data())).t();
  188. }
  189. else
  190. {
  191. dst = Matx<_Tp, _rows, _cols>(static_cast<const _Tp*>(src.data()));
  192. }
  193. }
  194. template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
  195. void cv2eigen( const Mat& src,
  196. Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst )
  197. {
  198. CV_DbgAssert(src.rows == _rows && src.cols == _cols);
  199. if( !(dst.Flags & Eigen::RowMajorBit) )
  200. {
  201. const Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
  202. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  203. if( src.type() == _dst.type() )
  204. transpose(src, _dst);
  205. else if( src.cols == src.rows )
  206. {
  207. src.convertTo(_dst, _dst.type());
  208. transpose(_dst, _dst);
  209. }
  210. else
  211. Mat(src.t()).convertTo(_dst, _dst.type());
  212. }
  213. else
  214. {
  215. const Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
  216. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  217. src.convertTo(_dst, _dst.type());
  218. }
  219. }
  220. // Matx case
  221. template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
  222. void cv2eigen( const Matx<_Tp, _rows, _cols>& src,
  223. Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst )
  224. {
  225. if( !(dst.Flags & Eigen::RowMajorBit) )
  226. {
  227. const Mat _dst(_cols, _rows, traits::Type<_Tp>::value,
  228. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  229. transpose(src, _dst);
  230. }
  231. else
  232. {
  233. const Mat _dst(_rows, _cols, traits::Type<_Tp>::value,
  234. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  235. Mat(src).copyTo(_dst);
  236. }
  237. }
  238. template<typename _Tp> static inline
  239. void cv2eigen( const Mat& src,
  240. Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
  241. {
  242. dst.resize(src.rows, src.cols);
  243. if( !(dst.Flags & Eigen::RowMajorBit) )
  244. {
  245. const Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
  246. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  247. if( src.type() == _dst.type() )
  248. transpose(src, _dst);
  249. else if( src.cols == src.rows )
  250. {
  251. src.convertTo(_dst, _dst.type());
  252. transpose(_dst, _dst);
  253. }
  254. else
  255. Mat(src.t()).convertTo(_dst, _dst.type());
  256. }
  257. else
  258. {
  259. const Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
  260. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  261. src.convertTo(_dst, _dst.type());
  262. }
  263. }
  264. // Matx case
  265. template<typename _Tp, int _rows, int _cols> static inline
  266. void cv2eigen( const Matx<_Tp, _rows, _cols>& src,
  267. Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
  268. {
  269. dst.resize(_rows, _cols);
  270. if( !(dst.Flags & Eigen::RowMajorBit) )
  271. {
  272. const Mat _dst(_cols, _rows, traits::Type<_Tp>::value,
  273. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  274. transpose(src, _dst);
  275. }
  276. else
  277. {
  278. const Mat _dst(_rows, _cols, traits::Type<_Tp>::value,
  279. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  280. Mat(src).copyTo(_dst);
  281. }
  282. }
  283. template<typename _Tp> static inline
  284. void cv2eigen( const Mat& src,
  285. Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
  286. {
  287. CV_Assert(src.cols == 1);
  288. dst.resize(src.rows);
  289. if( !(dst.Flags & Eigen::RowMajorBit) )
  290. {
  291. const Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
  292. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  293. if( src.type() == _dst.type() )
  294. transpose(src, _dst);
  295. else
  296. Mat(src.t()).convertTo(_dst, _dst.type());
  297. }
  298. else
  299. {
  300. const Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
  301. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  302. src.convertTo(_dst, _dst.type());
  303. }
  304. }
  305. // Matx case
  306. template<typename _Tp, int _rows> static inline
  307. void cv2eigen( const Matx<_Tp, _rows, 1>& src,
  308. Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
  309. {
  310. dst.resize(_rows);
  311. if( !(dst.Flags & Eigen::RowMajorBit) )
  312. {
  313. const Mat _dst(1, _rows, traits::Type<_Tp>::value,
  314. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  315. transpose(src, _dst);
  316. }
  317. else
  318. {
  319. const Mat _dst(_rows, 1, traits::Type<_Tp>::value,
  320. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  321. src.copyTo(_dst);
  322. }
  323. }
  324. template<typename _Tp> static inline
  325. void cv2eigen( const Mat& src,
  326. Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
  327. {
  328. CV_Assert(src.rows == 1);
  329. dst.resize(src.cols);
  330. if( !(dst.Flags & Eigen::RowMajorBit) )
  331. {
  332. const Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
  333. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  334. if( src.type() == _dst.type() )
  335. transpose(src, _dst);
  336. else
  337. Mat(src.t()).convertTo(_dst, _dst.type());
  338. }
  339. else
  340. {
  341. const Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
  342. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  343. src.convertTo(_dst, _dst.type());
  344. }
  345. }
  346. //Matx
  347. template<typename _Tp, int _cols> static inline
  348. void cv2eigen( const Matx<_Tp, 1, _cols>& src,
  349. Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
  350. {
  351. dst.resize(_cols);
  352. if( !(dst.Flags & Eigen::RowMajorBit) )
  353. {
  354. const Mat _dst(_cols, 1, traits::Type<_Tp>::value,
  355. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  356. transpose(src, _dst);
  357. }
  358. else
  359. {
  360. const Mat _dst(1, _cols, traits::Type<_Tp>::value,
  361. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  362. Mat(src).copyTo(_dst);
  363. }
  364. }
  365. //! @}
  366. } // cv
  367. #endif