EstervQrCode 2.0.0
Library for qr code manipulation
Loading...
Searching...
No Matches
eigen.hpp
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
44
45#ifndef OPENCV_CORE_EIGEN_HPP
46#define OPENCV_CORE_EIGEN_HPP
47
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"
50#endif
51
52#include "opencv2/core.hpp"
53
54#if defined _MSC_VER && _MSC_VER >= 1200
55#ifndef NOMINMAX
56#define NOMINMAX // fix https://github.com/opencv/opencv/issues/17548
57#endif
58#pragma warning( disable: 4714 ) //__forceinline is not inlined
59#pragma warning( disable: 4127 ) //conditional expression is constant
60#pragma warning( disable: 4244 ) //conversion from '__int64' to 'int', possible loss of data
61#endif
62
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
67#endif // EIGEN_WORLD_VERSION == 3 && EIGEN_MAJOR_VERSION >= 3
68#endif // !defined(OPENCV_DISABLE_EIGEN_TENSOR_SUPPORT)
69
70namespace cv
71{
72
83
84#if defined(OPENCV_EIGEN_TENSOR_SUPPORT) || defined(CV_DOXYGEN)
100template <typename _Tp, int _layout> static inline
101void eigen2cv( const Eigen::Tensor<_Tp, 3, _layout> &src, OutputArray dst )
102{
103 if( !(_layout & Eigen::RowMajorBit) )
104 {
105 const std::array<int, 3> shuffle{2, 1, 0};
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());
108 _src.copyTo(dst);
109 }
110 else
111 {
112 Mat _src(src.dimension(0), src.dimension(1), CV_MAKETYPE(DataType<_Tp>::type, src.dimension(2)), (void *)src.data());
113 _src.copyTo(dst);
114 }
115}
116
132template <typename _Tp, int _layout> static inline
133void cv2eigen( const Mat &src, Eigen::Tensor<_Tp, 3, _layout> &dst )
134{
135 if( !(_layout & Eigen::RowMajorBit) )
136 {
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())
140 src.copyTo(_dst);
141 else
142 src.convertTo(_dst, _dst.type());
143 const std::array<int, 3> shuffle{2, 1, 0};
144 dst = row_major_tensor.swap_layout().shuffle(shuffle);
145 }
146 else
147 {
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())
151 src.copyTo(_dst);
152 else
153 src.convertTo(_dst, _dst.type());
154 }
155}
156
176template <typename _Tp> static inline
177Eigen::TensorMap<Eigen::Tensor<_Tp, 3, Eigen::RowMajor>> cv2eigen_tensormap(InputArray src)
178{
179 Mat mat = src.getMat();
180 CV_CheckTypeEQ(mat.type(), CV_MAKETYPE(traits::Type<_Tp>::value, mat.channels()), "");
181 return Eigen::TensorMap<Eigen::Tensor<_Tp, 3, Eigen::RowMajor>>((_Tp *)mat.data, mat.rows, mat.cols, mat.channels());
182}
183#endif // OPENCV_EIGEN_TENSOR_SUPPORT
184
185template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
186void eigen2cv( const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src, OutputArray dst )
187{
188 if( !(src.Flags & Eigen::RowMajorBit) )
189 {
190 Mat _src(src.cols(), src.rows(), traits::Type<_Tp>::value,
191 (void*)src.data(), src.outerStride()*sizeof(_Tp));
192 transpose(_src, dst);
193 }
194 else
195 {
196 Mat _src(src.rows(), src.cols(), traits::Type<_Tp>::value,
197 (void*)src.data(), src.outerStride()*sizeof(_Tp));
198 _src.copyTo(dst);
199 }
200}
201
202// Matx case
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,
206{
207 if( !(src.Flags & Eigen::RowMajorBit) )
208 {
209 dst = Matx<_Tp, _cols, _rows>(static_cast<const _Tp*>(src.data())).t();
210 }
211 else
212 {
213 dst = Matx<_Tp, _rows, _cols>(static_cast<const _Tp*>(src.data()));
214 }
215}
216
217template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
218void cv2eigen( const Mat& src,
219 Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst )
220{
221 CV_DbgAssert(src.rows == _rows && src.cols == _cols);
222 if( !(dst.Flags & Eigen::RowMajorBit) )
223 {
224 const Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
225 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
226 if( src.type() == _dst.type() )
227 transpose(src, _dst);
228 else if( src.cols == src.rows )
229 {
230 src.convertTo(_dst, _dst.type());
231 transpose(_dst, _dst);
232 }
233 else
234 Mat(src.t()).convertTo(_dst, _dst.type());
235 }
236 else
237 {
238 const Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
239 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
240 src.convertTo(_dst, _dst.type());
241 }
242}
243
244// Matx case
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 )
248{
249 if( !(dst.Flags & Eigen::RowMajorBit) )
250 {
251 const Mat _dst(_cols, _rows, traits::Type<_Tp>::value,
252 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
253 transpose(src, _dst);
254 }
255 else
256 {
257 const Mat _dst(_rows, _cols, traits::Type<_Tp>::value,
258 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
259 Mat(src).copyTo(_dst);
260 }
261}
262
263template<typename _Tp> static inline
264void cv2eigen( const Mat& src,
265 Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
266{
267 dst.resize(src.rows, src.cols);
268 if( !(dst.Flags & Eigen::RowMajorBit) )
269 {
270 const Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
271 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
272 if( src.type() == _dst.type() )
273 transpose(src, _dst);
274 else if( src.cols == src.rows )
275 {
276 src.convertTo(_dst, _dst.type());
277 transpose(_dst, _dst);
278 }
279 else
280 Mat(src.t()).convertTo(_dst, _dst.type());
281 }
282 else
283 {
284 const Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
285 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
286 src.convertTo(_dst, _dst.type());
287 }
288}
289
290// Matx case
291template<typename _Tp, int _rows, int _cols> static inline
293 Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
294{
295 dst.resize(_rows, _cols);
296 if( !(dst.Flags & Eigen::RowMajorBit) )
297 {
298 const Mat _dst(_cols, _rows, traits::Type<_Tp>::value,
299 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
300 transpose(src, _dst);
301 }
302 else
303 {
304 const Mat _dst(_rows, _cols, traits::Type<_Tp>::value,
305 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
306 Mat(src).copyTo(_dst);
307 }
308}
309
310template<typename _Tp> static inline
311void cv2eigen( const Mat& src,
312 Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
313{
314 CV_Assert(src.cols == 1);
315 dst.resize(src.rows);
316
317 if( !(dst.Flags & Eigen::RowMajorBit) )
318 {
319 const Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
320 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
321 if( src.type() == _dst.type() )
322 transpose(src, _dst);
323 else
324 Mat(src.t()).convertTo(_dst, _dst.type());
325 }
326 else
327 {
328 const Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
329 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
330 src.convertTo(_dst, _dst.type());
331 }
332}
333
334// Matx case
335template<typename _Tp, int _rows> static inline
337 Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
338{
339 dst.resize(_rows);
340
341 if( !(dst.Flags & Eigen::RowMajorBit) )
342 {
343 const Mat _dst(1, _rows, traits::Type<_Tp>::value,
344 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
345 transpose(src, _dst);
346 }
347 else
348 {
349 const Mat _dst(_rows, 1, traits::Type<_Tp>::value,
350 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
351 src.copyTo(_dst);
352 }
353}
354
355
356template<typename _Tp> static inline
357void cv2eigen( const Mat& src,
358 Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
359{
360 CV_Assert(src.rows == 1);
361 dst.resize(src.cols);
362 if( !(dst.Flags & Eigen::RowMajorBit) )
363 {
364 const Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
365 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
366 if( src.type() == _dst.type() )
367 transpose(src, _dst);
368 else
369 Mat(src.t()).convertTo(_dst, _dst.type());
370 }
371 else
372 {
373 const Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
374 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
375 src.convertTo(_dst, _dst.type());
376 }
377}
378
379//Matx
380template<typename _Tp, int _cols> static inline
382 Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
383{
384 dst.resize(_cols);
385 if( !(dst.Flags & Eigen::RowMajorBit) )
386 {
387 const Mat _dst(_cols, 1, traits::Type<_Tp>::value,
388 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
389 transpose(src, _dst);
390 }
391 else
392 {
393 const Mat _dst(1, _cols, traits::Type<_Tp>::value,
394 dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
395 Mat(src).copyTo(_dst);
396 }
397}
398
400
401} // cv
402
403#endif
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
T shuffle(T... args)
int rows
Definition types_c.h:485
union CvMat::@78 data
int type
Definition types_c.h:456
int cols
Definition types_c.h:486
Definition traits.hpp:386
@ value
Definition traits.hpp:386