eigen.hpp
Go to the documentation of this file.
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 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 
43 #ifndef __OPENCV_CORE_EIGEN_HPP__
44 #define __OPENCV_CORE_EIGEN_HPP__
45 
46 #ifdef __cplusplus
47 
48 #include "opencv2/core/core_c.h"
49 #include "opencv2/core/core.hpp"
50 
51 #if defined _MSC_VER && _MSC_VER >= 1200
52 #pragma warning( disable: 4714 ) //__forceinline is not inlined
53 #pragma warning( disable: 4127 ) //conditional expression is constant
54 #pragma warning( disable: 4244 ) //conversion from '__int64' to 'int', possible loss of data
55 #endif
56 
57 namespace cv
58 {
59 
60 template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols>
61 void eigen2cv( const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src, Mat& dst )
62 {
63  if( !(src.Flags & Eigen::RowMajorBit) )
64  {
65  Mat _src(src.cols(), src.rows(), DataType<_Tp>::type,
66  (void*)src.data(), src.stride()*sizeof(_Tp));
67  transpose(_src, dst);
68  }
69  else
70  {
71  Mat _src(src.rows(), src.cols(), DataType<_Tp>::type,
72  (void*)src.data(), src.stride()*sizeof(_Tp));
73  _src.copyTo(dst);
74  }
75 }
76 
77 template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols>
78 void cv2eigen( const Mat& src,
79  Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst )
80 {
81  CV_DbgAssert(src.rows == _rows && src.cols == _cols);
82  if( !(dst.Flags & Eigen::RowMajorBit) )
83  {
84  Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
85  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
86  if( src.type() == _dst.type() )
87  transpose(src, _dst);
88  else if( src.cols == src.rows )
89  {
90  src.convertTo(_dst, _dst.type());
91  transpose(_dst, _dst);
92  }
93  else
94  Mat(src.t()).convertTo(_dst, _dst.type());
95  CV_DbgAssert(_dst.data == (uchar*)dst.data());
96  }
97  else
98  {
99  Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
100  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
101  src.convertTo(_dst, _dst.type());
102  CV_DbgAssert(_dst.data == (uchar*)dst.data());
103  }
104 }
105 
106 // Matx case
107 template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols>
109  Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst )
110 {
111  if( !(dst.Flags & Eigen::RowMajorBit) )
112  {
113  Mat _dst(_cols, _rows, DataType<_Tp>::type,
114  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
115  transpose(src, _dst);
116  CV_DbgAssert(_dst.data == (uchar*)dst.data());
117  }
118  else
119  {
120  Mat _dst(_rows, _cols, DataType<_Tp>::type,
121  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
122  Mat(src).copyTo(_dst);
123  CV_DbgAssert(_dst.data == (uchar*)dst.data());
124  }
125 }
126 
127 template<typename _Tp>
128 void cv2eigen( const Mat& src,
129  Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
130 {
131  dst.resize(src.rows, src.cols);
132  if( !(dst.Flags & Eigen::RowMajorBit) )
133  {
134  Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
135  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
136  if( src.type() == _dst.type() )
137  transpose(src, _dst);
138  else if( src.cols == src.rows )
139  {
140  src.convertTo(_dst, _dst.type());
141  transpose(_dst, _dst);
142  }
143  else
144  Mat(src.t()).convertTo(_dst, _dst.type());
145  CV_DbgAssert(_dst.data == (uchar*)dst.data());
146  }
147  else
148  {
149  Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
150  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
151  src.convertTo(_dst, _dst.type());
152  CV_DbgAssert(_dst.data == (uchar*)dst.data());
153  }
154 }
155 
156 // Matx case
157 template<typename _Tp, int _rows, int _cols>
159  Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
160 {
161  dst.resize(_rows, _cols);
162  if( !(dst.Flags & Eigen::RowMajorBit) )
163  {
164  Mat _dst(_cols, _rows, DataType<_Tp>::type,
165  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
166  transpose(src, _dst);
167  CV_DbgAssert(_dst.data == (uchar*)dst.data());
168  }
169  else
170  {
171  Mat _dst(_rows, _cols, DataType<_Tp>::type,
172  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
173  Mat(src).copyTo(_dst);
174  CV_DbgAssert(_dst.data == (uchar*)dst.data());
175  }
176 }
177 
178 template<typename _Tp>
179 void cv2eigen( const Mat& src,
180  Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
181 {
182  CV_Assert(src.cols == 1);
183  dst.resize(src.rows);
184 
185  if( !(dst.Flags & Eigen::RowMajorBit) )
186  {
187  Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
188  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
189  if( src.type() == _dst.type() )
190  transpose(src, _dst);
191  else
192  Mat(src.t()).convertTo(_dst, _dst.type());
193  CV_DbgAssert(_dst.data == (uchar*)dst.data());
194  }
195  else
196  {
197  Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
198  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
199  src.convertTo(_dst, _dst.type());
200  CV_DbgAssert(_dst.data == (uchar*)dst.data());
201  }
202 }
203 
204 // Matx case
205 template<typename _Tp, int _rows>
207  Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
208 {
209  dst.resize(_rows);
210 
211  if( !(dst.Flags & Eigen::RowMajorBit) )
212  {
213  Mat _dst(1, _rows, DataType<_Tp>::type,
214  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
215  transpose(src, _dst);
216  CV_DbgAssert(_dst.data == (uchar*)dst.data());
217  }
218  else
219  {
220  Mat _dst(_rows, 1, DataType<_Tp>::type,
221  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
222  src.copyTo(_dst);
223  CV_DbgAssert(_dst.data == (uchar*)dst.data());
224  }
225 }
226 
227 
228 template<typename _Tp>
229 void cv2eigen( const Mat& src,
230  Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
231 {
232  CV_Assert(src.rows == 1);
233  dst.resize(src.cols);
234  if( !(dst.Flags & Eigen::RowMajorBit) )
235  {
236  Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
237  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
238  if( src.type() == _dst.type() )
239  transpose(src, _dst);
240  else
241  Mat(src.t()).convertTo(_dst, _dst.type());
242  CV_DbgAssert(_dst.data == (uchar*)dst.data());
243  }
244  else
245  {
246  Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
247  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
248  src.convertTo(_dst, _dst.type());
249  CV_DbgAssert(_dst.data == (uchar*)dst.data());
250  }
251 }
252 
253 //Matx
254 template<typename _Tp, int _cols>
256  Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
257 {
258  dst.resize(_cols);
259  if( !(dst.Flags & Eigen::RowMajorBit) )
260  {
261  Mat _dst(_cols, 1, DataType<_Tp>::type,
262  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
263  transpose(src, _dst);
264  CV_DbgAssert(_dst.data == (uchar*)dst.data());
265  }
266  else
267  {
268  Mat _dst(1, _cols, DataType<_Tp>::type,
269  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
270  Mat(src).copyTo(_dst);
271  CV_DbgAssert(_dst.data == (uchar*)dst.data());
272  }
273 }
274 
275 
276 }
277 
278 #endif
279 
280 #endif
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions ...
Definition: core.hpp:1962
void cv2eigen(const Mat &src, Eigen::Matrix< _Tp, _rows, _cols, _options, _maxRows, _maxCols > &dst)
Definition: eigen.hpp:78
void copyTo(OutputArray m) const
copies the matrix content to "m".
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
converts matrix to another datatype with optional scalng. See cvConvertScale.
GLuint src
Definition: core_c.h:1650
Definition: core.hpp:85
int cols
Definition: core.hpp:1962
int type() const
returns element type, similar to CV_MAT_TYPE(cvmat->type)
Definition: mat.hpp:399
CV_EXPORTS_W void transpose(InputArray src, OutputArray dst)
transposes the matrix
Informative template class for OpenCV "scalars".
Definition: core.hpp:1006
MatExpr t() const
matrix transposition by means of matrix expressions
The Core Functionality.
The n-dimensional matrix class.
Definition: core.hpp:1688
unsigned char uchar
Definition: types_c.h:170
void eigen2cv(const Eigen::Matrix< _Tp, _rows, _cols, _options, _maxRows, _maxCols > &src, Mat &dst)
Definition: eigen.hpp:61
GLuint dst
Definition: calib3d.hpp:134