mexopencv  3.4.1
MEX interface for OpenCV library
OnePassStabilizer_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 #include "opencv2/videostab.hpp"
11 using namespace std;
12 using namespace cv;
13 using namespace cv::videostab;
14 
15 // Persistent objects
16 namespace {
18 int last_id = 0;
21 }
22 
30 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
31 {
32  // Check the number of arguments
33  nargchk(nrhs>=2 && nlhs<=1);
34 
35  // Argument vector
36  vector<MxArray> rhs(prhs, prhs+nrhs);
37  int id = rhs[0].toInt();
38  string method(rhs[1].toString());
39 
40  // Constructor is called. Create a new object from argument
41  if (method == "new") {
42  nargchk(nrhs==2 && nlhs<=1);
43  obj_[++last_id] = makePtr<OnePassStabilizer>();
44  plhs[0] = MxArray(last_id);
45  mexLock();
46  return;
47  }
48  // static methods
49  else if (method == "RansacParamsDefault2dMotion") {
50  nargchk(nrhs==3 && nlhs<=1);
51  MotionModel model = MotionModelMap[rhs[2].toString()];
52  RansacParams ransacParams = RansacParams::default2dMotion(model);
53  plhs[0] = toStruct(ransacParams);
54  return;
55  }
56 
57  // Big operation switch
58  Ptr<OnePassStabilizer> obj = obj_[id];
59  if (obj.empty())
60  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
61  if (method == "delete") {
62  nargchk(nrhs==2 && nlhs==0);
63  obj_.erase(id);
64  mexUnlock();
65  }
66  else if (method == "reset") {
67  nargchk(nrhs==2 && nlhs==0);
68  obj->reset();
69  }
70  else if (method == "nextFrame") {
71  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
72  bool flip = false;
73  for (int i=2; i<nrhs; i+=2) {
74  string key(rhs[i].toString());
75  if (key == "FlipChannels")
76  flip = rhs[i+1].toBool();
77  else
78  mexErrMsgIdAndTxt("mexopencv:error",
79  "Unrecognized option %s", key.c_str());
80  }
81  Mat frame(obj->nextFrame());
82  if (flip && (frame.channels() == 3 || frame.channels() == 4)) {
83  // OpenCV's default is BGR/BGRA while MATLAB's is RGB/RGBA
84  cvtColor(frame, frame, (frame.channels()==3 ?
86  }
87  plhs[0] = MxArray(frame);
88  }
89  else if (method == "setLog") {
90  nargchk(nrhs==3 && nlhs==0);
91  Ptr<ILog> p = createILog(rhs[2].toString());
92  obj->setLog(p);
93  }
94  else if (method == "setFrameSource") {
95  nargchk(nrhs>=3 && nlhs==0);
97  rhs[2].toString(), rhs.begin() + 3, rhs.end());
98  obj->setFrameSource(p);
99  }
100  else if (method == "setDeblurer") {
101  nargchk(nrhs>=3 && nlhs==0);
103  rhs[2].toString(), rhs.begin() + 3, rhs.end());
104  obj->setDeblurer(p);
105  }
106  else if (method == "setMotionEstimator") {
107  nargchk(nrhs>=3 && nlhs==0);
109  rhs[2].toString(), rhs.begin() + 3, rhs.end());
110  obj->setMotionEstimator(p);
111  }
112  else if (method == "setInpainter") {
113  nargchk(nrhs>=3 && nlhs==0);
115  rhs[2].toString(), rhs.begin() + 3, rhs.end());
116  obj->setInpainter(p);
117  }
118  else if (method == "setMotionFilter") {
119  nargchk(nrhs>=3 && nlhs==0);
121  rhs[2].toString(), rhs.begin() + 3, rhs.end());
122  obj->setMotionFilter(p);
123  }
124  else if (method == "getLog") {
125  nargchk(nrhs==2 && nlhs<=1);
126  Ptr<ILog> p = obj->log();
127  plhs[0] = toStruct(p);
128  }
129  else if (method == "getFrameSource") {
130  nargchk(nrhs==2 && nlhs<=1);
131  Ptr<IFrameSource> p = obj->frameSource();
132  plhs[0] = toStruct(p);
133  }
134  else if (method == "getDeblurer") {
135  nargchk(nrhs==2 && nlhs<=1);
136  Ptr<DeblurerBase> p = obj->deblurrer();
137  plhs[0] = toStruct(p);
138  }
139  else if (method == "getMotionEstimator") {
140  nargchk(nrhs==2 && nlhs<=1);
142  plhs[0] = toStruct(p);
143  }
144  else if (method == "getInpainter") {
145  nargchk(nrhs==2 && nlhs<=1);
146  Ptr<InpainterBase> p = obj->inpainter();
147  plhs[0] = toStruct(p);
148  }
149  else if (method == "getMotionFilter") {
150  nargchk(nrhs==2 && nlhs<=1);
152  plhs[0] = toStruct(p);
153  }
154  else if (method == "get") {
155  nargchk(nrhs==3 && nlhs<=1);
156  string prop(rhs[2].toString());
157  if (prop == "BorderMode")
158  plhs[0] = MxArray(BorderTypeInv[obj->borderMode()]);
159  else if (prop == "CorrectionForInclusion")
160  plhs[0] = MxArray(obj->doCorrectionForInclusion());
161  else if (prop == "Radius")
162  plhs[0] = MxArray(obj->radius());
163  else if (prop == "TrimRatio")
164  plhs[0] = MxArray(obj->trimRatio());
165  else
166  mexErrMsgIdAndTxt("mexopencv:error",
167  "Unrecognized property %s", prop.c_str());
168  }
169  else if (method == "set") {
170  nargchk(nrhs==4 && nlhs==0);
171  string prop(rhs[2].toString());
172  if (prop == "BorderMode")
173  obj->setBorderMode(BorderType[rhs[3].toString()]);
174  else if (prop == "CorrectionForInclusion")
175  obj->setCorrectionForInclusion(rhs[3].toBool());
176  else if (prop == "Radius")
177  obj->setRadius(rhs[3].toInt());
178  else if (prop == "TrimRatio")
179  obj->setTrimRatio(rhs[3].toFloat());
180  else
181  mexErrMsgIdAndTxt("mexopencv:error",
182  "Unrecognized property %s", prop.c_str());
183  }
184  else
185  mexErrMsgIdAndTxt("mexopencv:error",
186  "Unrecognized operation %s", method.c_str());
187 }
void setCorrectionForInclusion(bool val)
Common definitions for the videostab module.
cv::Ptr< cv::videostab::DeblurerBase > createDeblurerBase(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of DeblurerBase using options in arguments.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
cv::Ptr< cv::videostab::InpainterBase > createInpainterBase(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of InpainterBase using options in arguments.
Ptr< ImageMotionEstimatorBase > motionEstimator() const
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
COLOR_BGR2RGB
void setLog(Ptr< ILog > ilog)
bool doCorrectionForInclusion() const
Ptr< ILog > log() const
STL namespace.
Ptr< InpainterBase > inpainter() const
map< int, Ptr< OnePassStabilizer > > obj_
Object container.
T end(T... args)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
MotionModel
STL class.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
void setMotionFilter(Ptr< MotionFilterBase > val)
MxArray toStruct(const std::vector< cv::ml::DTrees::Node > &nodes)
Convert tree nodes to struct array.
const ConstMap< int, std::string > BorderTypeInv
Inverse border type map for option processing.
Definition: mexopencv.hpp:76
COLOR_BGRA2RGBA
Ptr< MotionFilterBase > motionFilter() const
const ConstMap< std::string, cv::videostab::MotionModel > MotionModelMap
motion model types for option processing
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.
cv::Ptr< cv::videostab::IFrameSource > createIFrameSource(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of IFrameSource using options in arguments.
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
STL class.
bool empty() const
Global constant definitions.
T begin(T... args)
void setInpainter(Ptr< InpainterBase > val)
T c_str(T... args)
cv::Ptr< cv::videostab::ImageMotionEstimatorBase > createImageMotionEstimator(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of ImageMotionEstimatorBase using options in arguments.
void flip(InputArray src, OutputArray dst, int flipCode)
Ptr< DeblurerBase > deblurrer() const
Ptr< IFrameSource > frameSource() const
cv::Ptr< cv::videostab::MotionFilterBase > createMotionFilterBase(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of MotionFilterBase using options in arguments.
void setDeblurer(Ptr< DeblurerBase > val)
void setFrameSource(Ptr< IFrameSource > val)
cv::Ptr< cv::videostab::ILog > createILog(const std::string &type)
Create an instance of ILog of the specified type.
void setMotionEstimator(Ptr< ImageMotionEstimatorBase > val)