mexopencv  0.1
mex interface for opencv library
TwoPassStabilizer_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::videostab;
13 
14 // Persistent objects
15 namespace {
17 int last_id = 0;
19 map<int,Ptr<TwoPassStabilizer> > obj_;
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && nlhs<=1);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36  int id = rhs[0].toInt();
37  string method(rhs[1].toString());
38 
39  // Constructor is called. Create a new object from argument
40  if (method == "new") {
41  nargchk(nrhs==2 && nlhs<=1);
42  obj_[++last_id] = makePtr<TwoPassStabilizer>();
43  plhs[0] = MxArray(last_id);
44  return;
45  }
46  // static methods
47  else if (method == "RansacParamsDefault2dMotion") {
48  nargchk(nrhs==3 && nlhs<=1);
49  MotionModel model = MotionModelMap[rhs[2].toString()];
50  RansacParams ransacParams = RansacParams::default2dMotion(model);
51  plhs[0] = toStruct(ransacParams);
52  return;
53  }
54 
55  // Big operation switch
56  Ptr<TwoPassStabilizer> obj = obj_[id];
57  if (method == "delete") {
58  nargchk(nrhs==2 && nlhs==0);
59  obj_.erase(id);
60  }
61  else if (method == "reset") {
62  nargchk(nrhs==2 && nlhs==0);
63  obj->reset();
64  }
65  else if (method == "nextFrame") {
66  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
67  bool flip = true;
68  for (int i=2; i<nrhs; i+=2) {
69  string key(rhs[i].toString());
70  if (key == "FlipChannels")
71  flip = rhs[i+1].toBool();
72  else
73  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
74  }
75  Mat frame = obj->nextFrame();
76  if (flip && (frame.channels() == 3 || frame.channels() == 4)) {
77  // OpenCV's default is BGR/BGRA while MATLAB's is RGB/RGBA
78  cvtColor(frame, frame, (frame.channels()==3 ?
79  cv::COLOR_BGR2RGB : cv::COLOR_BGRA2RGBA));
80  }
81  plhs[0] = MxArray(frame);
82  }
83  else if (method == "setLog") {
84  nargchk(nrhs==3 && nlhs==0);
85  Ptr<ILog> p = createILog(rhs[2].toString());
86  obj->setLog(p);
87  }
88  else if (method == "setFrameSource") {
89  nargchk(nrhs>=3 && nlhs==0);
90  Ptr<IFrameSource> p = createIFrameSource(
91  rhs[2].toString(), rhs.begin() + 3, rhs.end());
92  obj->setFrameSource(p);
93  }
94  else if (method == "setDeblurer") {
95  nargchk(nrhs>=3 && nlhs==0);
96  Ptr<DeblurerBase> p = createDeblurerBase(
97  rhs[2].toString(), rhs.begin() + 3, rhs.end());
98  obj->setDeblurer(p);
99  }
100  else if (method == "setMotionEstimator") {
101  nargchk(nrhs>=3 && nlhs==0);
102  Ptr<ImageMotionEstimatorBase> p = createImageMotionEstimator(
103  rhs[2].toString(), rhs.begin() + 3, rhs.end());
104  obj->setMotionEstimator(p);
105  }
106  else if (method == "setInpainter") {
107  nargchk(nrhs>=3 && nlhs==0);
108  Ptr<InpainterBase> p = createInpainterBase(
109  rhs[2].toString(), rhs.begin() + 3, rhs.end());
110  obj->setInpainter(p);
111  }
112  else if (method == "setMotionStabilizer") {
113  nargchk(nrhs>=3 && nlhs==0);
114  Ptr<IMotionStabilizer> p = createIMotionStabilizer(
115  rhs[2].toString(), rhs.begin() + 3, rhs.end());
116  obj->setMotionStabilizer(p);
117  }
118  else if (method == "setWobbleSuppressor") {
119  nargchk(nrhs>=3 && nlhs==0);
120  Ptr<WobbleSuppressorBase> p = createWobbleSuppressorBase(
121  rhs[2].toString(), rhs.begin() + 3, rhs.end());
122  obj->setWobbleSuppressor(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);
141  Ptr<ImageMotionEstimatorBase> p = obj->motionEstimator();
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 == "getMotionStabilizer") {
150  nargchk(nrhs==2 && nlhs<=1);
151  Ptr<IMotionStabilizer> p = obj->motionStabilizer();
152  plhs[0] = toStruct(p);
153  }
154  else if (method == "getWobbleSuppressor") {
155  nargchk(nrhs==2 && nlhs<=1);
156  Ptr<WobbleSuppressorBase> p = obj->wobbleSuppressor();
157  plhs[0] = toStruct(p);
158  }
159  else if (method == "get") {
160  nargchk(nrhs==3 && nlhs<=1);
161  string prop(rhs[2].toString());
162  if (prop == "BorderMode")
163  plhs[0] = MxArray(BorderTypeInv[obj->borderMode()]);
164  else if (prop == "CorrectionForInclusion")
165  plhs[0] = MxArray(obj->doCorrectionForInclusion());
166  else if (prop == "Radius")
167  plhs[0] = MxArray(obj->radius());
168  else if (prop == "TrimRatio")
169  plhs[0] = MxArray(obj->trimRatio());
170  else if (prop == "EstimateTrimRatio")
171  plhs[0] = MxArray(obj->mustEstimateTrimaRatio());
172  else
173  mexErrMsgIdAndTxt("mexopencv:error",
174  "Unrecognized property %s", prop.c_str());
175  }
176  else if (method == "set") {
177  nargchk(nrhs==4 && nlhs==0);
178  string prop(rhs[2].toString());
179  if (prop == "BorderMode")
180  obj->setBorderMode(BorderType[rhs[3].toString()]);
181  else if (prop == "CorrectionForInclusion")
182  obj->setCorrectionForInclusion(rhs[3].toBool());
183  else if (prop == "Radius")
184  obj->setRadius(rhs[3].toInt());
185  else if (prop == "TrimRatio")
186  obj->setTrimRatio(rhs[3].toFloat());
187  else if (prop == "EstimateTrimRatio")
188  obj->setEstimateTrimRatio(rhs[3].toBool());
189  else
190  mexErrMsgIdAndTxt("mexopencv:error",
191  "Unrecognized property %s", prop.c_str());
192  }
193  else
194  mexErrMsgIdAndTxt("mexopencv:error",
195  "Unrecognized operation %s", method.c_str());
196 }
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.
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.
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.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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:62
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.
const ConstMap< std::string, int > BorderType
Border type map for option processing.
Definition: mexopencv.hpp:52
const ConstMap< std::string, cv::videostab::MotionModel > MotionModelMap
motion model types for option processing
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/ouput arguments number check.
Definition: mexopencv.hpp:166
Global constant definitions.
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.
cv::Ptr< cv::videostab::ILog > createILog(const std::string &type)
Create an instance of ILog of the specified type.