mexopencv  0.1
mex interface for opencv library
OnePassStabilizer_.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<OnePassStabilizer> > 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<OnePassStabilizer>();
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<OnePassStabilizer> 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 == "setMotionFilter") {
113  nargchk(nrhs>=3 && nlhs==0);
114  Ptr<MotionFilterBase> p = createMotionFilterBase(
115  rhs[2].toString(), rhs.begin() + 3, rhs.end());
116  obj->setMotionFilter(p);
117  }
118  else if (method == "getLog") {
119  nargchk(nrhs==2 && nlhs<=1);
120  Ptr<ILog> p = obj->log();
121  plhs[0] = toStruct(p);
122  }
123  else if (method == "getFrameSource") {
124  nargchk(nrhs==2 && nlhs<=1);
125  Ptr<IFrameSource> p = obj->frameSource();
126  plhs[0] = toStruct(p);
127  }
128  else if (method == "getDeblurer") {
129  nargchk(nrhs==2 && nlhs<=1);
130  Ptr<DeblurerBase> p = obj->deblurrer();
131  plhs[0] = toStruct(p);
132  }
133  else if (method == "getMotionEstimator") {
134  nargchk(nrhs==2 && nlhs<=1);
135  Ptr<ImageMotionEstimatorBase> p = obj->motionEstimator();
136  plhs[0] = toStruct(p);
137  }
138  else if (method == "getInpainter") {
139  nargchk(nrhs==2 && nlhs<=1);
140  Ptr<InpainterBase> p = obj->inpainter();
141  plhs[0] = toStruct(p);
142  }
143  else if (method == "getMotionFilter") {
144  nargchk(nrhs==2 && nlhs<=1);
145  Ptr<MotionFilterBase> p = obj->motionFilter();
146  plhs[0] = toStruct(p);
147  }
148  else if (method == "get") {
149  nargchk(nrhs==3 && nlhs<=1);
150  string prop(rhs[2].toString());
151  if (prop == "BorderMode")
152  plhs[0] = MxArray(BorderTypeInv[obj->borderMode()]);
153  else if (prop == "CorrectionForInclusion")
154  plhs[0] = MxArray(obj->doCorrectionForInclusion());
155  else if (prop == "Radius")
156  plhs[0] = MxArray(obj->radius());
157  else if (prop == "TrimRatio")
158  plhs[0] = MxArray(obj->trimRatio());
159  else
160  mexErrMsgIdAndTxt("mexopencv:error",
161  "Unrecognized property %s", prop.c_str());
162  }
163  else if (method == "set") {
164  nargchk(nrhs==4 && nlhs==0);
165  string prop(rhs[2].toString());
166  if (prop == "BorderMode")
167  obj->setBorderMode(BorderType[rhs[3].toString()]);
168  else if (prop == "CorrectionForInclusion")
169  obj->setCorrectionForInclusion(rhs[3].toBool());
170  else if (prop == "Radius")
171  obj->setRadius(rhs[3].toInt());
172  else if (prop == "TrimRatio")
173  obj->setTrimRatio(rhs[3].toFloat());
174  else
175  mexErrMsgIdAndTxt("mexopencv:error",
176  "Unrecognized property %s", prop.c_str());
177  }
178  else
179  mexErrMsgIdAndTxt("mexopencv:error",
180  "Unrecognized operation %s", method.c_str());
181 }
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.
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
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::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.
cv::Ptr< cv::videostab::ILog > createILog(const std::string &type)
Create an instance of ILog of the specified type.