mexopencv  3.4.1
MEX interface for OpenCV library
testMxArray1_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
19 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
20 {
21  // Set a cutom error handler to be called by cv::error() and cousins,
22  // this replaces the default behavior which prints error info on stderr.
23  // Note that OpenCV will still throw a C++ exception after running the
24  // handler, but since we terminate with mexErrMsgIdAndTxt it wont matter.
26 
27  // Check the number of arguments
28  nargchk(nrhs>=1);
29 
30  // Argument vector
31  vector<MxArray> rhs(prhs, prhs+nrhs);
32  string method(rhs[0].toString());
33 
34 
35  if (method == "from_scalar_int") {
36  nargchk(nrhs==1 && nlhs<=1);
37  int x = 5;
38  MxArray arr(x);
39  CV_Assert(arr.isDouble() && arr.ndims() == 2); // arr.isInt32()
40  CV_Assert(arr.numel() == 1 && arr.rows() == 1 && arr.cols() == 1);
41  plhs[0] = arr;
42  }
43  else if (method == "from_scalar_double") {
44  nargchk(nrhs==1 && nlhs<=1);
45  double x = 3.14;
46  MxArray arr(x);
47  CV_Assert(arr.isDouble() && arr.ndims() == 2);
48  CV_Assert(arr.numel() == 1 && arr.rows() == 1 && arr.cols() == 1);
49  plhs[0] = arr;
50  }
51  else if (method == "from_scalar_bool") {
52  nargchk(nrhs==1 && nlhs<=1);
53  bool x = true;
54  MxArray arr(x);
55  CV_Assert(arr.isLogicalScalar() && arr.ndims() == 2);
56  CV_Assert(arr.numel() == 1 && arr.rows() == 1 && arr.cols() == 1);
57  plhs[0] = arr;
58  }
59  else if (method == "from_string") {
60  nargchk(nrhs==1 && nlhs<=1);
61  string str = "test";
62  MxArray arr(str);
63  CV_Assert(arr.isChar() && arr.ndims() == 2);
64  CV_Assert(arr.numel() == str.size() && arr.rows() == 1 && arr.cols() == str.size());
65  plhs[0] = arr;
66  }
67  else if (method == "toMat_row_vector") {
68  nargchk(nrhs==2 && nlhs<=1);
69  Mat m(rhs[1].toMat());
70  CV_Assert(m.depth() == CV_64F && m.channels() == 1);
71  CV_Assert(m.total() == 10 && m.dims == 2 && m.rows == 1 && m.cols == 10);
72  const double *data = m.ptr<double>();
73  const size_t len = m.total() * m.channels();
74  for (size_t i=0; i<len; i++)
75  CV_Assert(data[i] == (i+1));
76  plhs[0] = MxArray(true);
77  }
78  else if (method == "toMat_col_vector") {
79  nargchk(nrhs==2 && nlhs<=1);
80  Mat m(rhs[1].toMat());
81  CV_Assert(m.depth() == CV_64F && m.channels() == 1);
82  CV_Assert(m.total() == 10 && m.dims == 2 && m.rows == 10 && m.cols == 1);
83  const double *data = m.ptr<double>();
84  const size_t len = m.total() * m.channels();
85  for (size_t i=0; i<len; i++)
86  CV_Assert(data[i] == (i+1));
87  plhs[0] = MxArray(true);
88  }
89  else if (method == "fromMat_row_vector") {
90  nargchk(nrhs==1 && nlhs<=1);
91  Mat m = (Mat_<double>(1,10) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
92  MxArray arr(m);
93  CV_Assert(arr.isDouble() && arr.ndims() == 2);
94  CV_Assert(arr.numel() == 10 && arr.rows() == 1 && arr.cols() == 10);
95  plhs[0] = arr;
96  }
97  else if (method == "fromMat_col_vector") {
98  nargchk(nrhs==1 && nlhs<=1);
99  Mat m = (Mat_<double>(10,1) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
100  MxArray arr(m);
101  CV_Assert(arr.isDouble() && arr.ndims() == 2);
102  CV_Assert(arr.numel() == 10 && arr.rows() == 10 && arr.cols() == 1);
103  plhs[0] = arr;
104  }
105  else
106  mexErrMsgIdAndTxt("mexopencv:error",
107  "Unrecognized method %s", method.c_str());
108 }
mwSize ndims() const
Number of dimensions.
Definition: MxArray.hpp:550
#define CV_Assert(...)
mwSize numel() const
Number of elements in an array.
Definition: MxArray.hpp:546
size_t total() const
bool isLogicalScalar() const
Determine whether scalar array is of type mxLogical.
Definition: MxArray.hpp:683
STL namespace.
bool isChar() const
Determine whether input is string array.
Definition: MxArray.hpp:614
bool isDouble() const
Determine whether mxArray represents data as double-precision, floating-point numbers.
Definition: MxArray.hpp:637
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
int MexErrorHandler(int status, const char *func_name, const char *err_msg, const char *file_name, int line, void *userdata)
Cutom error callback to be invoked by cv::error(), CV_Assert, etc.
Definition: MxArray.cpp:86
#define CV_64F
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
int depth() const
T size(T... args)
ErrorCallback redirectError(ErrorCallback errCallback, void *userdata=0, void **prevUserdata=0)
STL class.
uchar * ptr(int i0=0)
Global constant definitions.
T c_str(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mwSize rows() const
Number of rows in an array.
Definition: MxArray.hpp:558
mwSize cols() const
Number of columns in an array.
Definition: MxArray.hpp:565
cv::Mat toMat() const
int channels() const