ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkOpenCVBasicTypeBridge.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
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 * https://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
35namespace itk
36{
37template <typename TITKData, typename TOpenCVData>
40
41template <typename TPoint>
42struct OpenCVBasicTypeBridge<TPoint, cv::Point_<typename TPoint::CoordinateType>>
43{
44 using ITKDataType = TPoint;
45 using CoordinateType = typename TPoint::CoordinateType;
46 using OpenCVDataType = cv::Point_<CoordinateType>;
47
48 static ITKDataType
50 {
51 ITKDataType oP;
52 oP[0] = iP.x;
53 oP[1] = iP.y;
54
55 return oP;
56 }
57
58 static OpenCVDataType
60 {
61 return OpenCVDataType(iP[0], iP[1]);
62 }
63
65};
66
67template <typename TPoint>
68struct OpenCVBasicTypeBridge<TPoint, cv::Point3_<typename TPoint::CoordinateType>>
69{
70 using ITKDataType = TPoint;
71 using CoordinateType = typename TPoint::CoordinateType;
72 using OpenCVDataType = cv::Point3_<CoordinateType>;
73
74 static ITKDataType
76 {
77 ITKDataType oP;
78 oP[0] = iP.x;
79 oP[1] = iP.y;
80 oP[2] = iP.z;
81
82 return oP;
83 }
84
85 static OpenCVDataType
87 {
88 return OpenCVDataType(iP[0], iP[1], iP[2]);
89 }
90
92};
93
94template <>
96{
98 using OpenCVDataType = cv::Point;
99
100 static ITKDataType
102 {
103 ITKDataType oP;
104 oP[0] = static_cast<itk::IndexValueType>(iP.x);
105 oP[1] = static_cast<itk::IndexValueType>(iP.y);
106
107 return oP;
108 }
109
110 static OpenCVDataType
112 {
113 return OpenCVDataType(static_cast<int>(iP[0]), static_cast<int>(iP[1]));
114 }
115};
116
117template <>
118struct OpenCVBasicTypeBridge<Index<3>, cv::Point3i>
119{
121 using OpenCVDataType = cv::Point3i;
122
123 static ITKDataType
125 {
126 ITKDataType oP;
127 oP[0] = static_cast<itk::IndexValueType>(iP.x);
128 oP[1] = static_cast<itk::IndexValueType>(iP.y);
129 oP[2] = static_cast<itk::IndexValueType>(iP.z);
130
131 return oP;
132 }
133
134 static OpenCVDataType
136 {
137 return OpenCVDataType(static_cast<int>(iP[0]), static_cast<int>(iP[1]), static_cast<int>(iP[2]));
138 }
139};
140
141template <>
143{
145 using OpenCVDataType = cv::Size;
146
147 static ITKDataType
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
157 static OpenCVDataType
159 {
160 return OpenCVDataType(static_cast<int>(iP[0]), static_cast<int>(iP[1]));
161 }
162};
163
164// cv::Matx uses int template params; itk::Matrix uses unsigned int.
165// Using int for VRows/VColumns (matching OpenCV) with a cast in the ITK
166// pattern allows the compiler to deduce both sides correctly.
167template <typename T, int VRows, int VColumns>
168struct OpenCVBasicTypeBridge<Matrix<T, static_cast<unsigned int>(VRows), static_cast<unsigned int>(VColumns)>,
169 cv::Matx<T, VRows, VColumns>>
170{
171 using ITKDataType = itk::Matrix<T, static_cast<unsigned int>(VRows), static_cast<unsigned int>(VColumns)>;
172 using OpenCVDataType = cv::Matx<T, VRows, VColumns>;
173
174 static ITKDataType
176 {
177 return ITKDataType(typename ITKDataType::InternalMatrixType().copy_in(iP.val));
178 }
179
180 static OpenCVDataType
182 {
184 iP.GetVnlMatrix().copy_out(oM.val);
185 return oM;
186 }
187};
188
189template <typename TVector>
190struct OpenCVBasicTypeBridge<TVector, cv::Vec<typename TVector::ValueType, TVector::Dimension>>
191{
192 using ITKDataType = TVector;
193 using ValueType = typename TVector::ValueType;
194 using OpenCVDataType = cv::Vec<ValueType, TVector::Dimension>;
195
196 static ITKDataType
198 {
199 return ITKDataType(iP.val);
200 }
201
202 static OpenCVDataType
204 {
206 std::copy(iP.Begin(), iP.End(), oM.val);
207 return oM;
208 }
209};
210
211} // namespace itk
212#endif
A templated class holding a M x N size Matrix.
Definition itkMatrix.h:53
InternalMatrixType & GetVnlMatrix()
Definition itkMatrix.h:214
A templated class holding a geometric point in n-Dimensional space.
Definition itkPoint.h:54
#define itkConceptMacro(name, concept)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition itkIntTypes.h:86
long IndexValueType
Definition itkIntTypes.h:93
Represent a n-dimensional index in a n-dimensional image.
Definition itkIndex.h:69
static OpenCVDataType FromITKToOpenCV(const ITKDataType &iP)
static ITKDataType FromOpenCVToITK(const OpenCVDataType &iP)
static OpenCVDataType FromITKToOpenCV(const ITKDataType &iP)
static ITKDataType FromOpenCVToITK(const OpenCVDataType &iP)
static OpenCVDataType FromITKToOpenCV(const ITKDataType &iP)
static ITKDataType FromOpenCVToITK(const OpenCVDataType &iP)
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition itkSize.h:70