mexopencv  3.4.1
MEX interface for OpenCV library
RotationWarper_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 #include "opencv2/stitching.hpp"
11 #include <typeinfo>
12 using namespace std;
13 using namespace cv;
14 using namespace cv::detail;
15 
16 // Persistent objects
17 namespace {
19 int last_id = 0;
22 }
23 
31 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
32 {
33  // Check the number of arguments
34  nargchk(nrhs>=2 && nlhs<=3);
35 
36  // Argument vector
37  vector<MxArray> rhs(prhs, prhs+nrhs);
38  int id = rhs[0].toInt();
39  string method(rhs[1].toString());
40 
41  // Constructor is called. Create a new object from argument
42  if (method == "new") {
43  nargchk(nrhs>=4 && nlhs<=1);
45  rhs[2].toString(), rhs.begin() + 4, rhs.end(), rhs[3].toFloat());
46  plhs[0] = MxArray(last_id);
47  mexLock();
48  return;
49  }
50 
51  // Big operation switch
52  Ptr<RotationWarper> obj = obj_[id];
53  if (obj.empty())
54  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
55  if (method == "delete") {
56  nargchk(nrhs==2 && nlhs==0);
57  obj_.erase(id);
58  mexUnlock();
59  }
60  else if (method == "typeid") {
61  nargchk(nrhs==2 && nlhs<=1);
62  plhs[0] = MxArray(string(typeid(*obj).name()));
63  }
64  else if (method == "buildMaps") {
65  nargchk(nrhs==5 && nlhs<=3);
66  Size src_size(rhs[2].toSize());
67  Mat K(rhs[3].toMat(CV_32F)),
68  R(rhs[4].toMat(CV_32F)),
69  xmap, ymap;
70  Rect bbox = obj->buildMaps(src_size, K, R, xmap, ymap);
71  plhs[0] = MxArray(xmap);
72  if (nlhs > 1)
73  plhs[1] = MxArray(ymap);
74  if (nlhs > 2)
75  plhs[2] = MxArray(bbox);
76  }
77  else if (method == "warpPoint") {
78  nargchk(nrhs==5 && nlhs<=1);
79  Point2f pt(rhs[2].toPoint2f());
80  Mat K(rhs[3].toMat(CV_32F)), R(rhs[4].toMat(CV_32F));
81  Point2f uv = obj->warpPoint(pt, K, R);
82  plhs[0] = MxArray(uv);
83  }
84  else if (method == "warp") {
85  nargchk(nrhs>=5 && (nrhs%2)==1 && nlhs<=2);
86  int interp_mode = cv::INTER_LINEAR;
87  int border_mode = cv::BORDER_CONSTANT;
88  for (int i=5; i<nrhs; i+=2) {
89  string key(rhs[i].toString());
90  if (key == "InterpMode")
91  interp_mode = (rhs[i+1].isChar()) ?
92  InterpType[rhs[i+1].toString()] : rhs[i+1].toInt();
93  else if (key == "BorderMode")
94  border_mode = (rhs[i+1].isChar()) ?
95  BorderType[rhs[i+1].toString()] : rhs[i+1].toInt();
96  else
97  mexErrMsgIdAndTxt("mexopencv:error",
98  "Unrecognized option %s", key.c_str());
99  }
100  Mat src(rhs[2].toMat()), dst;
101  Mat K(rhs[3].toMat(CV_32F)), R(rhs[4].toMat(CV_32F));
102  Point tl = obj->warp(src, K, R, interp_mode, border_mode, dst);
103  plhs[0] = MxArray(dst);
104  if (nlhs > 1)
105  plhs[1] = MxArray(tl);
106  }
107  else if (method == "warpBackward") {
108  nargchk(nrhs>=6 && (nrhs%2)==0 && nlhs<=1);
109  int interp_mode = cv::INTER_LINEAR;
110  int border_mode = cv::BORDER_CONSTANT;
111  for (int i=6; i<nrhs; i+=2) {
112  string key(rhs[i].toString());
113  if (key == "InterpMode")
114  interp_mode = (rhs[i+1].isChar()) ?
115  InterpType[rhs[i+1].toString()] : rhs[i+1].toInt();
116  else if (key == "BorderMode")
117  border_mode = (rhs[i+1].isChar()) ?
118  BorderType[rhs[i+1].toString()] : rhs[i+1].toInt();
119  else
120  mexErrMsgIdAndTxt("mexopencv:error",
121  "Unrecognized option %s", key.c_str());
122  }
123  Mat src(rhs[2].toMat()), dst;
124  Mat K(rhs[3].toMat(CV_32F)), R(rhs[4].toMat(CV_32F));
125  Size dst_size(rhs[5].toSize());
126  obj->warpBackward(src, K, R, interp_mode, border_mode, dst_size, dst);
127  plhs[0] = MxArray(dst);
128  }
129  else if (method == "warpRoi") {
130  nargchk(nrhs==5 && nlhs<=1);
131  Size src_size(rhs[2].toSize());
132  Mat K(rhs[3].toMat(CV_32F)), R(rhs[4].toMat(CV_32F));
133  Rect bbox = obj->warpRoi(src_size, K, R);
134  plhs[0] = MxArray(bbox);
135  }
136  else if (method == "get") {
137  nargchk(nrhs==3 && nlhs<=1);
138  string prop(rhs[2].toString());
139  if (prop == "Scale")
140  plhs[0] = MxArray(obj->getScale());
141  else
142  mexErrMsgIdAndTxt("mexopencv:error",
143  "Unrecognized property %s", prop.c_str());
144  }
145  else if (method == "set") {
146  nargchk(nrhs==4 && nlhs==0);
147  string prop(rhs[2].toString());
148  if (prop == "Scale")
149  obj->setScale(rhs[3].toFloat());
150  else
151  mexErrMsgIdAndTxt("mexopencv:error",
152  "Unrecognized property %s", prop.c_str());
153  }
154  else
155  mexErrMsgIdAndTxt("mexopencv:error",
156  "Unrecognized operation %s", method.c_str());
157 }
virtual void setScale(float)
virtual void warpBackward(InputArray src, InputArray K, InputArray R, int interp_mode, int border_mode, Size dst_size, OutputArray dst)=0
virtual Point warp(InputArray src, InputArray K, InputArray R, int interp_mode, int border_mode, OutputArray dst)=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Common definitions for the stitching module.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
cv::Ptr< cv::detail::RotationWarper > createRotationWarper(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last, float scale=1.0f)
Create an instance of RotationWarper using options in arguments.
const ConstMap< std::string, int > InterpType
Interpolation type map for option processing.
Definition: mexopencv.hpp:85
STL namespace.
T end(T... args)
map< int, Ptr< RotationWarper > > obj_
Object container.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual Point2f warpPoint(const Point2f &pt, InputArray K, InputArray R)=0
int last_id
Last object id to allocate.
#define CV_32F
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...
const ConstMap< std::string, int > BorderType
Border type map for option processing.
Definition: mexopencv.hpp:66
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
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
virtual float getScale() const
STL class.
bool empty() const
Global constant definitions.
T begin(T... args)
INTER_LINEAR
T c_str(T... args)
virtual Rect warpRoi(Size src_size, InputArray K, InputArray R)=0
BORDER_CONSTANT
virtual Rect buildMaps(Size src_size, InputArray K, InputArray R, OutputArray xmap, OutputArray ymap)=0
cv::Mat toMat() const