mexopencv  3.4.1
MEX interface for OpenCV library
TwoPassStabilizer_.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<TwoPassStabilizer>();
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<TwoPassStabilizer> 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 == "setMotionStabilizer") {
119  nargchk(nrhs>=3 && nlhs==0);
121  rhs[2].toString(), rhs.begin() + 3, rhs.end());
122  obj->setMotionStabilizer(p);
123  }
124  else if (method == "setWobbleSuppressor") {
125  nargchk(nrhs>=3 && nlhs==0);
127  rhs[2].toString(), rhs.begin() + 3, rhs.end());
128  obj->setWobbleSuppressor(p);
129  }
130  else if (method == "getLog") {
131  nargchk(nrhs==2 && nlhs<=1);
132  Ptr<ILog> p = obj->log();
133  plhs[0] = toStruct(p);
134  }
135  else if (method == "getFrameSource") {
136  nargchk(nrhs==2 && nlhs<=1);
137  Ptr<IFrameSource> p = obj->frameSource();
138  plhs[0] = toStruct(p);
139  }
140  else if (method == "getDeblurer") {
141  nargchk(nrhs==2 && nlhs<=1);
142  Ptr<DeblurerBase> p = obj->deblurrer();
143  plhs[0] = toStruct(p);
144  }
145  else if (method == "getMotionEstimator") {
146  nargchk(nrhs==2 && nlhs<=1);
148  plhs[0] = toStruct(p);
149  }
150  else if (method == "getInpainter") {
151  nargchk(nrhs==2 && nlhs<=1);
152  Ptr<InpainterBase> p = obj->inpainter();
153  plhs[0] = toStruct(p);
154  }
155  else if (method == "getMotionStabilizer") {
156  nargchk(nrhs==2 && nlhs<=1);
158  plhs[0] = toStruct(p);
159  }
160  else if (method == "getWobbleSuppressor") {
161  nargchk(nrhs==2 && nlhs<=1);
163  plhs[0] = toStruct(p);
164  }
165  else if (method == "get") {
166  nargchk(nrhs==3 && nlhs<=1);
167  string prop(rhs[2].toString());
168  if (prop == "BorderMode")
169  plhs[0] = MxArray(BorderTypeInv[obj->borderMode()]);
170  else if (prop == "CorrectionForInclusion")
171  plhs[0] = MxArray(obj->doCorrectionForInclusion());
172  else if (prop == "Radius")
173  plhs[0] = MxArray(obj->radius());
174  else if (prop == "TrimRatio")
175  plhs[0] = MxArray(obj->trimRatio());
176  else if (prop == "EstimateTrimRatio")
177  plhs[0] = MxArray(obj->mustEstimateTrimaRatio());
178  else
179  mexErrMsgIdAndTxt("mexopencv:error",
180  "Unrecognized property %s", prop.c_str());
181  }
182  else if (method == "set") {
183  nargchk(nrhs==4 && nlhs==0);
184  string prop(rhs[2].toString());
185  if (prop == "BorderMode")
186  obj->setBorderMode(BorderType[rhs[3].toString()]);
187  else if (prop == "CorrectionForInclusion")
188  obj->setCorrectionForInclusion(rhs[3].toBool());
189  else if (prop == "Radius")
190  obj->setRadius(rhs[3].toInt());
191  else if (prop == "TrimRatio")
192  obj->setTrimRatio(rhs[3].toFloat());
193  else if (prop == "EstimateTrimRatio")
194  obj->setEstimateTrimRatio(rhs[3].toBool());
195  else
196  mexErrMsgIdAndTxt("mexopencv:error",
197  "Unrecognized property %s", prop.c_str());
198  }
199  else
200  mexErrMsgIdAndTxt("mexopencv:error",
201  "Unrecognized operation %s", method.c_str());
202 }
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)
void setWobbleSuppressor(Ptr< WobbleSuppressorBase > val)
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< IMotionStabilizer > motionStabilizer() const
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)
Ptr< WobbleSuppressorBase > wobbleSuppressor() const
bool doCorrectionForInclusion() const
Ptr< ILog > log() const
cv::Ptr< cv::videostab::IMotionStabilizer > createIMotionStabilizer(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of IMotionStabilizer using options in arguments.
STL namespace.
Ptr< InpainterBase > inpainter() const
T end(T... args)
void setMotionStabilizer(Ptr< IMotionStabilizer > val)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
MotionModel
STL class.
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
cv::Ptr< cv::videostab::WobbleSuppressorBase > createWobbleSuppressorBase(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of WobbleSuppressorBase using options in arguments.
COLOR_BGRA2RGBA
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)
map< int, Ptr< TwoPassStabilizer > > obj_
Object container.
Ptr< DeblurerBase > deblurrer() const
Ptr< IFrameSource > frameSource() const
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)