ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkOpenCVBasicTypeBridge.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkOpenCVBasicTypeBridge_h
19 #define itkOpenCVBasicTypeBridge_h
20 
21 #include "itkPoint.h"
22 #include "itkSize.h"
23 #include "itkIndex.h"
24 #include "itkMatrix.h"
25 
26 #include "opencv2/core/version.hpp"
27 #if !defined(CV_VERSION_EPOCH)
28 // OpenCV 3.x
29 #include "opencv2/core.hpp"
30 #else
31 // OpenCV 2.4.x
32 #include "cv.h"
33 #endif
34 
35 namespace itk
36 {
37 template< typename TITKData, typename TOpenCVData >
39 {};
40 
41 template< typename TPoint >
42 struct OpenCVBasicTypeBridge< TPoint, cv::Point_< typename TPoint::CoordRepType > >
43 {
44  using ITKDataType = TPoint;
45  using CoordinateType = typename TPoint::CoordRepType;
46  using OpenCVDataType = cv::Point_< CoordinateType >;
47 
49  {
50  ITKDataType oP;
51  oP[0] = iP.x;
52  oP[1] = iP.y;
53 
54  return oP;
55  }
56 
58  {
59  return OpenCVDataType( iP[0], iP[1] );
60  }
61 
62 #ifdef ITK_USE_CONCEPT_CHECKING
63  // Begin concept checking
64  itkConceptMacro( SameDimensionCheck,
66 #endif
67 
68 };
69 
70 template< typename TPoint >
71 struct OpenCVBasicTypeBridge< TPoint, cv::Point3_< typename TPoint::CoordRepType > >
72 {
73  using ITKDataType = TPoint;
74  using CoordinateType = typename TPoint::CoordRepType;
75  using OpenCVDataType = cv::Point3_< CoordinateType >;
76 
78  {
79  ITKDataType oP;
80  oP[0] = iP.x;
81  oP[1] = iP.y;
82  oP[2] = iP.z;
83 
84  return oP;
85  }
86 
88  {
89  return OpenCVDataType( iP[0], iP[1], iP[2] );
90  }
91 
92 #ifdef ITK_USE_CONCEPT_CHECKING
93  // Begin concept checking
94  itkConceptMacro( SameDimensionCheck,
96 #endif
97 };
98 
99 template<>
100 struct OpenCVBasicTypeBridge< itk::Index< 2 >, cv::Point >
101 {
103  using OpenCVDataType = cv::Point;
104 
106  {
107  ITKDataType oP;
108  oP[0] = static_cast< itk::IndexValueType >( iP.x );
109  oP[1] = static_cast< itk::IndexValueType >( iP.y );
110 
111  return oP;
112  }
113 
115  {
116  return OpenCVDataType( static_cast< int >( iP[0] ), static_cast< int >( iP[1] ) );
117  }
118 };
119 
120 template<>
121 struct OpenCVBasicTypeBridge< itk::Index< 3 >, cv::Point3i >
122 {
124  using OpenCVDataType = cv::Point3i;
125 
127  {
128  ITKDataType oP;
129  oP[0] = static_cast< itk::IndexValueType >( iP.x );
130  oP[1] = static_cast< itk::IndexValueType >( iP.y );
131  oP[2] = static_cast< itk::IndexValueType >( iP.z );
132 
133  return oP;
134  }
135 
137  {
138  return OpenCVDataType( static_cast< int >( iP[0] ), static_cast< int >( iP[1] ), static_cast< int >( iP[2] ) );
139  }
140 };
141 
142 template<>
143 struct OpenCVBasicTypeBridge< itk::Size< 2 >, cv::Size >
144 {
146  using OpenCVDataType = cv::Size;
147 
149  {
150  ITKDataType oP;
151  oP[0] = static_cast< itk::SizeValueType >( iP.width );
152  oP[1] = static_cast< itk::SizeValueType >( iP.height );
153 
154  return oP;
155  }
156 
158  {
159  return OpenCVDataType( static_cast< int >( iP[0] ), static_cast< int >( iP[1] ) );
160  }
161 };
162 
163 template< typename T, unsigned int NRows, unsigned int NColumns >
164 struct OpenCVBasicTypeBridge< itk::Matrix< T, NRows, NColumns >, cv::Matx< T, NRows, NColumns > >
165 {
167  using OpenCVDataType = cv::Matx< T, NRows, NColumns >;
168 
170  {
171  return ITKDataType( typename ITKDataType::InternalMatrixType().copy_in( iP.val ) );
172  }
173 
175  {
176  OpenCVDataType oM;
177  iP.GetVnlMatrix().copy_out( oM.val );
178  return oM;
179  }
180 
181 };
182 
183 template< typename TVector >
184 struct OpenCVBasicTypeBridge< TVector, cv::Vec< typename TVector::ValueType, TVector::Dimension > >
185 {
186  using ITKDataType = TVector;
187  using ValueType = typename TVector::ValueType;
188  using OpenCVDataType = cv::Vec< ValueType, TVector::Dimension >;
189 
191  {
192  return ITKDataType( iP.val );
193  }
194 
196  {
197  OpenCVDataType oM;
198  std::copy( iP.Begin(), iP.End(), oM.val );
199  return oM;
200  }
201 };
202 
203 }
204 #endif
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:49
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:66
unsigned long SizeValueType
Definition: itkIntTypes.h:83
static ITKDataType FromOpenCVToITK(const OpenCVDataType &iP)
static OpenCVDataType FromITKToOpenCV(const ITKDataType &iP)
static OpenCVDataType FromITKToOpenCV(const ITKDataType &iP)
static ITKDataType FromOpenCVToITK(const OpenCVDataType &iP)
static ITKDataType FromOpenCVToITK(const OpenCVDataType &iP)
signed long IndexValueType
Definition: itkIntTypes.h:90
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:68
static OpenCVDataType FromITKToOpenCV(const ITKDataType &iP)
InternalMatrixType & GetVnlMatrix()
Definition: itkMatrix.h:168
#define itkConceptMacro(name, concept)
vnl_matrix_fixed< T, NRows, NColumns > InternalMatrixType
Definition: itkMatrix.h:64