mexopencv  0.1
mex interface for opencv library
StereoSGBM_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 // Persistent objects
13 namespace {
15 int last_id = 0;
17 map<int,Ptr<StereoSGBM> > obj_;
18 
21  ("SGBM", cv::StereoSGBM::MODE_SGBM)
22  ("HH", cv::StereoSGBM::MODE_HH)
23  ("SGBM3Way", cv::StereoSGBM::MODE_SGBM_3WAY);
24 const ConstMap<int, string> InvSGBMModeMap = ConstMap<int, string>
25  (cv::StereoSGBM::MODE_SGBM, "SGBM")
26  (cv::StereoSGBM::MODE_HH, "HH")
27  (cv::StereoSGBM::MODE_SGBM_3WAY, "SGBM3Way");
28 
34 Ptr<StereoSGBM> create_StereoSGBM(
35  vector<MxArray>::const_iterator first,
36  vector<MxArray>::const_iterator last)
37 {
38  ptrdiff_t len = std::distance(first, last);
39  nargchk((len%2)==0);
40  int minDisparity = 0;
41  int numDisparities = 64;
42  int blockSize = 7;
43  int P1 = 0;
44  int P2 = 0;
45  int disp12MaxDiff = 0;
46  int preFilterCap = 0;
47  int uniquenessRatio = 0;
48  int speckleWindowSize = 0;
49  int speckleRange = 0;
50  int mode = cv::StereoSGBM::MODE_SGBM;
51  for (; first != last; first += 2) {
52  string key(first->toString());
53  const MxArray& val = *(first + 1);
54  if (key == "MinDisparity")
55  minDisparity = val.toInt();
56  else if (key == "NumDisparities")
57  numDisparities = val.toInt();
58  else if (key == "BlockSize")
59  blockSize = val.toInt();
60  else if (key == "P1")
61  P1 = val.toInt();
62  else if (key == "P2")
63  P2 = val.toInt();
64  else if (key == "Disp12MaxDiff")
65  disp12MaxDiff = val.toInt();
66  else if (key == "PreFilterCap")
67  preFilterCap = val.toInt();
68  else if (key == "UniquenessRatio")
69  uniquenessRatio = val.toInt();
70  else if (key == "SpeckleWindowSize")
71  speckleWindowSize = val.toInt();
72  else if (key == "SpeckleRange")
73  speckleRange = val.toInt();
74  else if (key == "Mode")
75  mode = (val.isChar() ?
76  SGBMModeMap[val.toString()] : val.toInt());
77  else
78  mexErrMsgIdAndTxt("mexopencv:error",
79  "Unrecognized option %s", key.c_str());
80  }
81  return StereoSGBM::create(minDisparity, numDisparities,
82  blockSize, P1, P2, disp12MaxDiff, preFilterCap, uniquenessRatio,
83  speckleWindowSize, speckleRange, mode);
84 }
85 }
86 
94 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
95 {
96  // Check the number of arguments
97  nargchk(nrhs>=2 && nlhs<=1);
98 
99  // Argument vector
100  vector<MxArray> rhs(prhs, prhs+nrhs);
101  int id = rhs[0].toInt();
102  string method(rhs[1].toString());
103 
104  // Constructor is called. Create a new object from argument
105  if (method == "new") {
106  nargchk(nrhs>=2 && nlhs<=1);
107  obj_[++last_id] = create_StereoSGBM(rhs.begin() + 2, rhs.end());
108  plhs[0] = MxArray(last_id);
109  return;
110  }
111 
112  // Big operation switch
113  Ptr<StereoSGBM> obj = obj_[id];
114  if (method == "delete") {
115  nargchk(nrhs==2 && nlhs==0);
116  obj_.erase(id);
117  }
118  else if (method == "clear") {
119  nargchk(nrhs==2 && nlhs==0);
120  obj->clear();
121  }
122  else if (method == "save") {
123  nargchk(nrhs==3 && nlhs==0);
124  obj->save(rhs[2].toString());
125  }
126  else if (method == "load") {
127  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
128  string objname;
129  bool loadFromString = false;
130  for (int i=3; i<nrhs; i+=2) {
131  string key(rhs[i].toString());
132  if (key=="ObjName")
133  objname = rhs[i+1].toString();
134  else if (key=="FromString")
135  loadFromString = rhs[i+1].toBool();
136  else
137  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
138  }
139  /*
140  obj_[id] = (loadFromString ?
141  Algorithm::loadFromString<StereoSGBM>(rhs[2].toString(), objname) :
142  Algorithm::load<StereoSGBM>(rhs[2].toString(), objname));
143  */
145  // HACK: workaround because StereoSGBM::create() doesnt accept zero arguments
146  FileStorage fs(rhs[2].toString(), FileStorage::READ +
147  (loadFromString ? FileStorage::MEMORY : 0));
148  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
149  if (obj.empty())
150  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
151  //*/
152  }
153  else if (method == "empty") {
154  nargchk(nrhs==2 && nlhs<=1);
155  plhs[0] = MxArray(obj->empty());
156  }
157  else if (method == "getDefaultName") {
158  nargchk(nrhs==2 && nlhs<=1);
159  plhs[0] = MxArray(obj->getDefaultName());
160  }
161  else if (method == "compute") {
162  nargchk(nrhs==4 && nlhs<=1);
163  Mat left(rhs[2].toMat(CV_8U)),
164  right(rhs[3].toMat(CV_8U)),
165  disparity;
166  obj->compute(left, right, disparity);
167  plhs[0] = MxArray(disparity);
168  }
169  else if (method == "get") {
170  nargchk(nrhs==3 && nlhs<=1);
171  string prop(rhs[2].toString());
172  if (prop == "MinDisparity")
173  plhs[0] = MxArray(obj->getMinDisparity());
174  else if (prop == "NumDisparities")
175  plhs[0] = MxArray(obj->getNumDisparities());
176  else if (prop == "BlockSize")
177  plhs[0] = MxArray(obj->getBlockSize());
178  else if (prop == "P1")
179  plhs[0] = MxArray(obj->getP1());
180  else if (prop == "P2")
181  plhs[0] = MxArray(obj->getP2());
182  else if (prop == "Disp12MaxDiff")
183  plhs[0] = MxArray(obj->getDisp12MaxDiff());
184  else if (prop == "PreFilterCap")
185  plhs[0] = MxArray(obj->getPreFilterCap());
186  else if (prop == "UniquenessRatio")
187  plhs[0] = MxArray(obj->getUniquenessRatio());
188  else if (prop == "SpeckleWindowSize")
189  plhs[0] = MxArray(obj->getSpeckleWindowSize());
190  else if (prop == "SpeckleRange")
191  plhs[0] = MxArray(obj->getSpeckleRange());
192  else if (prop == "Mode")
193  plhs[0] = MxArray(InvSGBMModeMap[obj->getMode()]);
194  else
195  mexErrMsgIdAndTxt("mexopencv:error",
196  "Unrecognized property %s", prop.c_str());
197  }
198  else if (method == "set") {
199  nargchk(nrhs==4 && nlhs==0);
200  string prop(rhs[2].toString());
201  if (prop == "MinDisparity")
202  obj->setMinDisparity(rhs[3].toInt());
203  else if (prop == "NumDisparities")
204  obj->setNumDisparities(rhs[3].toInt());
205  else if (prop == "BlockSize")
206  obj->setBlockSize(rhs[3].toInt());
207  else if (prop == "P1")
208  obj->setP1(rhs[3].toInt());
209  else if (prop == "P2")
210  obj->setP2(rhs[3].toInt());
211  else if (prop == "Disp12MaxDiff")
212  obj->setDisp12MaxDiff(rhs[3].toInt());
213  else if (prop == "PreFilterCap")
214  obj->setPreFilterCap(rhs[3].toInt());
215  else if (prop == "UniquenessRatio")
216  obj->setUniquenessRatio(rhs[3].toInt());
217  else if (prop == "SpeckleWindowSize")
218  obj->setSpeckleWindowSize(rhs[3].toInt());
219  else if (prop == "SpeckleRange")
220  obj->setSpeckleRange(rhs[3].toInt());
221  else if (prop == "Mode")
222  obj->setMode(rhs[3].isChar() ?
223  SGBMModeMap[rhs[3].toString()] : rhs[3].toInt());
224  else
225  mexErrMsgIdAndTxt("mexopencv:error",
226  "Unrecognized property %s", prop.c_str());
227  }
228  else
229  mexErrMsgIdAndTxt("mexopencv:error",
230  "Unrecognized operation %s", method.c_str());
231 }
int toInt() const
Convert MxArray to int.
Definition: MxArray.cpp:489
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: StereoSGBM_.cpp:94
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.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927