-
Notifications
You must be signed in to change notification settings - Fork 10
/
mexopencv.hpp
447 lines (420 loc) · 15.9 KB
/
mexopencv.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* @file mexopencv.hpp
* @brief Global constant definitions
* @author Kota Yamaguchi
* @date 2012
*
* The header file for a MATLAB MEX-function that uses OpenCV library.
* The file includes definition of MxArray class that converts between mxArray
* and a couple of std:: and cv:: data types including cv::Mat.
*/
#ifndef __MEXOPENCV_HPP__
#define __MEXOPENCV_HPP__
#include "MxArray.hpp"
/**************************************************************\
* Global constants *
\**************************************************************/
/** Translates class name used in MATLAB to equivalent OpenCV depth.
* @param classname numeric MATLAB data type, e.g. \c uint8.
* @return equivalent OpenCV data type, e.g. \c CV_8U.
*
* Note: 64-bit integer types are not supported by OpenCV.
* Also, OpenCV only has signed 32-bit integer type.
*/
const ConstMap<std::string,int> ClassNameMap = ConstMap<std::string,int>
("uint8", CV_8U)
("int8", CV_8S)
("uint16", CV_16U)
("int16", CV_16S)
//("uint32", CV_32S)
("int32", CV_32S)
("single", CV_32F)
("double", CV_64F)
("logical", CV_8U);
/** Translates data type definition used in OpenCV to that of MATLAB.
* @param depth OpenCV cv::Mat data depth, e.g. \c CV_8U.
* @return equivalent MATLAB class name, e.g. \c uint8.
*/
const ConstMap<int,std::string> ClassNameInvMap = ConstMap<int,std::string>
(CV_8U, "uint8")
(CV_8S, "int8")
(CV_16U, "uint16")
(CV_16S, "int16")
(CV_32S, "int32")
(CV_32F, "single")
(CV_64F, "double");
/// Border type map for option processing
const ConstMap<std::string,int> BorderType = ConstMap<std::string,int>
("Constant", cv::BORDER_CONSTANT) // iiiiii|abcdefgh|iiiiiii for some i
("Replicate", cv::BORDER_REPLICATE) // aaaaaa|abcdefgh|hhhhhhh
("Reflect", cv::BORDER_REFLECT) // fedcba|abcdefgh|hgfedcb
("Wrap", cv::BORDER_WRAP) // cdefgh|abcdefgh|abcdefg
("Reflect101", cv::BORDER_REFLECT_101) // gfedcb|abcdefgh|gfedcba
("Transparent", cv::BORDER_TRANSPARENT) // uvwxyz|absdefgh|ijklmno
("Default", cv::BORDER_DEFAULT) // same as "Reflect101"
("Isolated", cv::BORDER_ISOLATED); // do not look outside of ROI
/// Interpolation type map for option processing
const ConstMap<std::string,int> InterpType = ConstMap<std::string,int>
("Nearest", cv::INTER_NEAREST) // nearest neighbor interpolation
("Linear", cv::INTER_LINEAR) // bilinear interpolation
("Cubic", cv::INTER_CUBIC) // bicubic interpolation
("Area", cv::INTER_AREA) // area-based (or super) interpolation
("Lanczos4", cv::INTER_LANCZOS4) // Lanczos interpolation over 8x8 neighborhood
("Max", cv::INTER_MAX);
//("WarpInverseMap", cv::WARP_INVERSE_MAP);
/// Thresholding type map for option processing
const ConstMap<std::string,int> ThreshType = ConstMap<std::string,int>
("Binary", cv::THRESH_BINARY)
("BinaryInv", cv::THRESH_BINARY_INV)
("Trunc", cv::THRESH_TRUNC)
("ToZero", cv::THRESH_TOZERO)
("ToZeroInv", cv::THRESH_TOZERO_INV)
("Mask", cv::THRESH_MASK);
//("Otsu", cv::THRESH_OTSU);
/// Distance types for Distance Transform and M-estimators
const ConstMap<std::string,int> DistType = ConstMap<std::string,int>
("User", cv::DIST_USER)
("L1", cv::DIST_L1)
("L2", cv::DIST_L2)
("C", cv::DIST_C)
("L12", cv::DIST_L12)
("Fair", cv::DIST_FAIR)
("Welsch", cv::DIST_WELSCH)
("Huber", cv::DIST_HUBER);
/// Line type for drawing
const ConstMap<std::string,int> LineType = ConstMap<std::string,int>
("4", cv::LINE_4)
("8", cv::LINE_8)
("AA", cv::LINE_AA);
/// Thickness type for drawing
const ConstMap<std::string,int> ThicknessType = ConstMap<std::string,int>
("Filled", cv::FILLED);
/// Font faces for drawing
const ConstMap<std::string,int> FontFace = ConstMap<std::string,int>
("HersheySimplex", cv::FONT_HERSHEY_SIMPLEX)
("HersheyPlain", cv::FONT_HERSHEY_PLAIN)
("HersheyDuplex", cv::FONT_HERSHEY_DUPLEX)
("HersheyComplex", cv::FONT_HERSHEY_COMPLEX)
("HersheyTriplex", cv::FONT_HERSHEY_TRIPLEX)
("HersheyComplexSmall", cv::FONT_HERSHEY_COMPLEX_SMALL)
("HersheyScriptSimplex", cv::FONT_HERSHEY_SCRIPT_SIMPLEX)
("HersheyScriptComplex", cv::FONT_HERSHEY_SCRIPT_COMPLEX);
/// Font styles for drawing
const ConstMap<std::string,int> FontStyle = ConstMap<std::string,int>
("Regular", 0)
("Italic", cv::FONT_ITALIC);
/// Norm type map for option processing
const ConstMap<std::string,int> NormType = ConstMap<std::string,int>
("Inf", cv::NORM_INF)
("L1", cv::NORM_L1)
("L2", cv::NORM_L2)
("L2Sqr", cv::NORM_L2SQR)
("Hamming", cv::NORM_HAMMING)
("Hamming2", cv::NORM_HAMMING2)
("MinMax", cv::NORM_MINMAX);
/// Inverse norm type map for option processing
const ConstMap<int,std::string> NormTypeInv = ConstMap<int,std::string>
(cv::NORM_INF, "Inf")
(cv::NORM_L1, "L1")
(cv::NORM_L2, "L2")
(cv::NORM_L2SQR, "L2Sqr")
(cv::NORM_HAMMING, "Hamming")
(cv::NORM_HAMMING2, "Hamming2")
(cv::NORM_MINMAX, "MinMax");
/**************************************************************\
* Helper Macros & Functions *
\**************************************************************/
/// set or clear a bit in flag depending on bool value
#define UPDATE_FLAG(NUM, TF, BIT) \
do { \
if ((TF)) { (NUM) |= (BIT); } \
else { (NUM) &= ~(BIT); } \
} while(0)
/// Alias for input/ouput arguments number check
inline void nargchk(bool cond)
{
if (!cond) {
mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
}
}
/**************************************************************\
* Conversion Functions: MxArray to vector *
\**************************************************************/
/** Convert an MxArray to std::vector<cv::Point_<T>>
*
* @param arr MxArray object. In one of the following forms:
* - a cell-array of 2D points (2-element vectors) of length \c N,
* e.g: <tt>{[x,y], [x,y], ...}</tt>
* - a numeric matrix of size \c Nx2, \c Nx1x2, or \c 1xNx2 in the form:
* <tt>[x,y; x,y; ...]</tt> or <tt>cat(3, [x,y], [x,y], ...)</tt>
* @return vector of 2D points of size \c N
*
* Example:
* @code
* MxArray cellArray(prhs[0]);
* vector<Point2d> vp = MxArrayToVectorPoint<double>(cellArray);
* @endcode
*/
template <typename T>
std::vector<cv::Point_<T> > MxArrayToVectorPoint(const MxArray& arr)
{
std::vector<cv::Point_<T> > vp;
if (arr.isNumeric()) {
if (arr.numel() == 2)
vp.push_back(arr.toPoint_<T>());
else
arr.toMat(cv::DataType<T>::depth).reshape(2, 0).copyTo(vp);
}
else if (arr.isCell()) {
/*
std::vector<MxArray> va(arr.toVector<MxArray>());
vp.reserve(va.size());
for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
vp.push_back(it->toPoint_<T>());
*/
vp = arr.toVector(
std::const_mem_fun_ref_t<cv::Point_<T>, MxArray>(
&MxArray::toPoint_<T>));
}
else
mexErrMsgIdAndTxt("mexopencv:error",
"Unable to convert MxArray to std::vector<cv::Point_<T>>");
return vp;
}
/** Convert an MxArray to std::vector<cv::Point3_<T>>
*
* @param arr MxArray object. In one of the following forms:
* - a cell-array of 3D points (3-element vectors) of length \c N,
* e.g: <tt>{[x,y,z], [x,y,z], ...}</tt>
* - a numeric matrix of size \c Nx3, \c Nx1x3, or \c 1xNx3 in the form:
* <tt>[x,y,z; x,y,z; ...]</tt> or <tt>cat(3, [x,y,z], [x,y,z], ...)</tt>
* @return vector of 3D points of size \c N
*
* Example:
* @code
* MxArray cellArray(prhs[0]);
* vector<Point3f> vp = MxArrayToVectorPoint3<float>(cellArray);
* @endcode
*/
template <typename T>
std::vector<cv::Point3_<T> > MxArrayToVectorPoint3(const MxArray& arr)
{
std::vector<cv::Point3_<T> > vp;
if (arr.isNumeric()) {
if (arr.numel() == 3)
vp.push_back(arr.toPoint3_<T>());
else
arr.toMat(cv::DataType<T>::depth).reshape(3, 0).copyTo(vp);
}
else if (arr.isCell()) {
/*
std::vector<MxArray> va(arr.toVector<MxArray>());
vp.reserve(va.size());
for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
vp.push_back(it->toPoint3_<T>());
*/
vp = arr.toVector(
std::const_mem_fun_ref_t<cv::Point3_<T>, MxArray>(
&MxArray::toPoint3_<T>));
}
else
mexErrMsgIdAndTxt("mexopencv:error",
"Unable to convert MxArray to std::vector<cv::Point3_<T>>");
return vp;
}
/** Convert an MxArray to std::vector<cv::Rect_<T>>
*
* @param arr MxArray object. In one of the following forms:
* - a cell-array of rectangles (4-element vectors) of length \c N,
* e.g: <tt>{[x,y,w,h], [x,y,w,h], ...}</tt>
* - a numeric matrix of size \c Nx4, \c Nx1x4, or \c 1xNx4 in the form:
* <tt>[x,y,w,h; x,y,w,h; ...]</tt> or
* <tt>cat(3, [x,y,w,h], [x,y,w,h], ...)</tt>
* @return vector of rectangles of size \c N
*
* Example:
* @code
* MxArray cellArray(prhs[0]);
* vector<Rect2f> vr = MxArrayToVectorRect<float>(cellArray);
* @endcode
*/
template <typename T>
std::vector<cv::Rect_<T> > MxArrayToVectorRect(const MxArray& arr)
{
std::vector<cv::Rect_<T> > vr;
if (arr.isNumeric()) {
if (arr.numel() == 4)
vr.push_back(arr.toRect_<T>());
else
arr.toMat(cv::DataType<T>::depth).reshape(4, 0).copyTo(vr);
}
else if (arr.isCell()) {
/*
std::vector<MxArray> va(arr.toVector<MxArray>());
vr.reserve(va.size());
for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
vr.push_back(it->toRect_<T>());
*/
vr = arr.toVector(
std::const_mem_fun_ref_t<cv::Rect_<T>, MxArray>(
&MxArray::toRect_<T>));
}
else
mexErrMsgIdAndTxt("mexopencv:error",
"Unable to convert MxArray to std::vector<cv::Rect_<T>>");
return vr;
}
/** Convert an MxArray to std::vector<cv::Vec<T,cn>>
*
* @param arr MxArray object. In one of the following forms:
* - a cell-array of vecs (\c cn -element vectors) of length \c N,
* e.g: <tt>{[v_1,v_2,...,v_cn], [v_1,v_2,...,v_cn], ...}</tt>
* - a numeric matrix of size \c Nxcn, \c Nx1xcn, or \c 1xNxcn in the form:
* <tt>[v_1,v_2,...,v_cn; v_1,v_2,...,v_cn; ...]</tt> or
* <tt>cat(3, [v_1,v_2,...,v_cn], [v_1,v_2,...,v_cn], ...)</tt>
* @return vector of vecs of size \c N
*
* Example:
* @code
* MxArray cellArray(prhs[0]);
* vector<Vec4i> vv = MxArrayToVectorVec<int,4>(cellArray);
* @endcode
*/
template <typename T, int cn>
std::vector<cv::Vec<T,cn> > MxArrayToVectorVec(const MxArray& arr)
{
std::vector<cv::Vec<T,cn> > vv;
if (arr.isNumeric()) {
if (arr.numel() == cn)
vv.push_back(arr.toVec<T,cn>());
else
arr.toMat(cv::Vec<T,cn>::depth).reshape(cn, 0).copyTo(vv);
}
else if (arr.isCell()) {
/*
std::vector<MxArray> va(arr.toVector<MxArray>());
vv.reserve(va.size());
for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
vv.push_back(it->toVec<T,cn>());
*/
vv = arr.toVector(
std::const_mem_fun_ref_t<cv::Vec<T,cn>, MxArray>(
&MxArray::toVec<T,cn>));
}
else
mexErrMsgIdAndTxt("mexopencv:error",
"Unable to convert MxArray to std::vector<cv::Vec<T,cn>>");
return vv;
}
/** Convert an MxArray to std::vector<cv::Matx<T,m,n>>
*
* @param arr MxArray object. In one of the following forms:
* - a cell-array of mats (\c mxn matrices) of length \c N,
* e.g: <tt>{[mat_11, ..., mat_1n; ....; mat_m1, ..., mat_mn], ...}</tt>
* - a sole numeric matrix (\c N=1) of size \c mxn in the form:
* <tt>[mat_11, ..., mat_1n; ....; mat_m1, ..., mat_mn]</tt>
* @return vector of mats of size \c N
*
* Example:
* @code
* MxArray cellArray(prhs[0]);
* vector<Matx32f> vx = MxArrayToVectorMatx<float,3,2>(cellArray);
* @endcode
*/
template <typename T, int m, int n>
std::vector<cv::Matx<T,m,n> > MxArrayToVectorMatx(const MxArray& arr)
{
std::vector<cv::Matx<T,m,n> > vx;
if (arr.isNumeric()) {
vx.push_back(arr.toMatx<T,m,n>());
}
else if (arr.isCell()) {
/*
std::vector<MxArray> va(arr.toVector<MxArray>());
vx.reserve(va.size());
for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
vx.push_back(it->toMatx<T,m,n>());
*/
vx = arr.toVector(
std::const_mem_fun_ref_t<cv::Matx<T,m,n>, MxArray>(
&MxArray::toMatx<T,m,n>));
}
else
mexErrMsgIdAndTxt("mexopencv:error",
"Unable to convert MxArray to std::vector<cv::Matx<T,m,n>>");
return vx;
}
/**************************************************************\
* Conversion Functions: MxArray to vector of vectors *
\**************************************************************/
/** Convert an MxArray to std::vector<std::vector<cv::Point_<T>>>
*
* @param arr MxArray object. In one of the following forms:
* - a cell-array of cell-arrays of 2D points (2-element vectors),
* e.g: <tt>{{[x,y], [x,y], ..}, {[x,y], [x,y], ..}, ...}</tt>
* - a cell-array of numeric matrices of size \c Mx2, \c Mx1x2, or \c 1xMx2,
* e.g: <tt>{[x,y; x,y; ...], [x,y; x,y; ...], ...}</tt> or
* <tt>{cat(3, [x,y], [x,y], ...), cat(3, [x,y], [x,y], ...), ...}</tt>
* @return vector of vectors of 2D points
*
* Example:
* @code
* MxArray cellArray(prhs[0]);
* vector<vector<Point2d>> vvp = MxArrayToVectorVectorPoint<double>(cellArray);
* @endcode
*/
template <typename T>
std::vector<std::vector<cv::Point_<T> > > MxArrayToVectorVectorPoint(const MxArray& arr)
{
std::vector<MxArray> vva(arr.toVector<MxArray>());
std::vector<std::vector<cv::Point_<T> > > vvp;
vvp.reserve(vva.size());
for (std::vector<MxArray>::const_iterator it = vva.begin(); it != vva.end(); ++it) {
/*
std::vector<MxArray> va(it->toVector<MxArray());
std::vector<cv::Point_<T> > vp;
for (std::vector<MxArray>::const_iterator jt = va.begin(); jt != va.end(); ++jt) {
vp.push_back(jt->toPoint_<T>());
}
vvp.push_back(vp);
*/
vvp.push_back(MxArrayToVectorPoint<T>(*it));
}
return vvp;
}
/** Convert an MxArray to std::vector<std::vector<cv::Point3_<T>>>
*
* @param arr MxArray object. In one of the following forms:
* - a cell-array of cell-arrays of 3D points (3-element vectors),
* e.g: <tt>{{[x,y,z], [x,y,z], ..}, {[x,y,z], [x,y,z], ..}, ...}</tt>
* - a cell-array of numeric matrices of size \c Mx3, \c Mx1x3, or \c 1xMx3,
* e.g: <tt>{[x,y,z; x,y,z; ...], [x,y,z; x,y,z; ...], ...}</tt> or
* <tt>{cat(3, [x,y,z], [x,y,z], ...), cat(3, [x,y,z], [x,y,z], ...), ...}</tt>
* @return vector of vectors of 3D points
*
* Example:
* @code
* MxArray cellArray(prhs[0]);
* vector<vector<Point3d>> vvp = MxArrayToVectorVectorPoint3<double>(cellArray);
* @endcode
*/
template <typename T>
std::vector<std::vector<cv::Point3_<T> > > MxArrayToVectorVectorPoint3(const MxArray& arr)
{
std::vector<MxArray> vva(arr.toVector<MxArray>());
std::vector<std::vector<cv::Point3_<T> > > vvp;
vvp.reserve(vva.size());
for (std::vector<MxArray>::const_iterator it = vva.begin(); it != vva.end(); ++it) {
/*
std::vector<MxArray> va(it->toVector<MxArray());
std::vector<cv::Point3_<T> > vp;
for (std::vector<MxArray>::const_iterator jt = va.begin(); jt != va.end(); ++jt) {
vp.push_back(jt->toPoint3_<T>());
}
vvp.push_back(vp);
*/
vvp.push_back(MxArrayToVectorPoint3<T>(*it));
}
return vvp;
}
#endif