mexopencv  0.1
mex interface for opencv library
StereoBM_.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<StereoBM> > obj_;
18 
20 const ConstMap<string, int> PreFilerTypeMap = ConstMap<string, int>
21  ("NormalizedResponse", cv::StereoBM::PREFILTER_NORMALIZED_RESPONSE)
22  ("XSobel", cv::StereoBM::PREFILTER_XSOBEL);
23 const ConstMap<int, string> InvPreFilerTypeMap = ConstMap<int, string>
24  (cv::StereoBM::PREFILTER_NORMALIZED_RESPONSE, "NormalizedResponse")
25  (cv::StereoBM::PREFILTER_XSOBEL, "XSobel");
26 
32 Ptr<StereoBM> create_StereoBM(
33  vector<MxArray>::const_iterator first,
34  vector<MxArray>::const_iterator last)
35 {
36  ptrdiff_t len = std::distance(first, last);
37  nargchk((len%2)==0);
38  int numDisparities = 0;
39  int blockSize = 21;
40  int minDisparity = 0;
41  int speckleWindowSize = 0;
42  int speckleRange = 0;
43  int disp12MaxDiff = -1;
44  int preFilterType = cv::StereoBM::PREFILTER_XSOBEL;
45  int preFilterSize = 9;
46  int preFilterCap = 31;
47  int textureThreshold = 10;
48  int uniquenessRatio = 15;
49  int smallerBlockSize = 0;
50  Rect roi1;
51  Rect roi2;
52  for (; first != last; first += 2) {
53  string key(first->toString());
54  const MxArray& val = *(first + 1);
55  if (key == "NumDisparities")
56  numDisparities = val.toInt();
57  else if (key == "BlockSize")
58  blockSize = val.toInt();
59  else if (key == "MinDisparity")
60  minDisparity = val.toInt();
61  else if (key == "SpeckleWindowSize")
62  speckleWindowSize = val.toInt();
63  else if (key == "SpeckleRange")
64  speckleRange = val.toInt();
65  else if (key == "Disp12MaxDiff")
66  disp12MaxDiff = val.toInt();
67  else if (key == "PreFilterType")
68  preFilterType = (val.isChar() ?
69  PreFilerTypeMap[val.toString()] : val.toInt());
70  else if (key == "PreFilterSize")
71  preFilterSize = val.toInt();
72  else if (key == "PreFilterCap")
73  preFilterCap = val.toInt();
74  else if (key == "TextureThreshold")
75  textureThreshold = val.toInt();
76  else if (key == "UniquenessRatio")
77  uniquenessRatio = val.toInt();
78  else if (key == "SmallerBlockSize")
79  smallerBlockSize = val.toInt();
80  else if (key == "ROI1")
81  roi1 = val.toRect();
82  else if (key == "ROI2")
83  roi2 = val.toRect();
84  else
85  mexErrMsgIdAndTxt("mexopencv:error",
86  "Unrecognized option %s", key.c_str());
87  }
88  Ptr<StereoBM> p = StereoBM::create(numDisparities, blockSize);
89  if (p.empty())
90  mexErrMsgIdAndTxt("mexopencv:error", "Failed to create StereoBM");
91  p->setMinDisparity(minDisparity);
92  p->setSpeckleWindowSize(speckleWindowSize);
93  p->setSpeckleRange(speckleRange);
94  p->setDisp12MaxDiff(disp12MaxDiff);
95  p->setPreFilterType(preFilterType);
96  p->setPreFilterSize(preFilterSize);
97  p->setPreFilterCap(preFilterCap);
98  p->setTextureThreshold(textureThreshold);
99  p->setUniquenessRatio(uniquenessRatio);
100  p->setSmallerBlockSize(smallerBlockSize);
101  p->setROI1(roi1);
102  p->setROI2(roi2);
103  return p;
104 }
105 }
106 
114 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
115 {
116  // Check the number of arguments
117  nargchk(nrhs>=2 && nlhs<=1);
118 
119  // Argument vector
120  vector<MxArray> rhs(prhs, prhs+nrhs);
121  int id = rhs[0].toInt();
122  string method(rhs[1].toString());
123 
124  // Constructor is called. Create a new object from argument
125  if (method == "new") {
126  nargchk(nrhs>=2 && nlhs<=1);
127  obj_[++last_id] = create_StereoBM(rhs.begin() + 2, rhs.end());
128  plhs[0] = MxArray(last_id);
129  return;
130  }
131 
132  // Big operation switch
133  Ptr<StereoBM> obj = obj_[id];
134  if (method == "delete") {
135  nargchk(nrhs==2 && nlhs==0);
136  obj_.erase(id);
137  }
138  else if (method == "clear") {
139  nargchk(nrhs==2 && nlhs==0);
140  obj->clear();
141  }
142  else if (method == "save") {
143  nargchk(nrhs==3 && nlhs==0);
144  obj->save(rhs[2].toString());
145  }
146  else if (method == "load") {
147  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
148  string objname;
149  bool loadFromString = false;
150  for (int i=3; i<nrhs; i+=2) {
151  string key(rhs[i].toString());
152  if (key=="ObjName")
153  objname = rhs[i+1].toString();
154  else if (key=="FromString")
155  loadFromString = rhs[i+1].toBool();
156  else
157  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
158  }
159  obj_[id] = (loadFromString ?
160  Algorithm::loadFromString<StereoBM>(rhs[2].toString(), objname) :
161  Algorithm::load<StereoBM>(rhs[2].toString(), objname));
162  }
163  else if (method == "empty") {
164  nargchk(nrhs==2 && nlhs<=1);
165  plhs[0] = MxArray(obj->empty());
166  }
167  else if (method == "getDefaultName") {
168  nargchk(nrhs==2 && nlhs<=1);
169  plhs[0] = MxArray(obj->getDefaultName());
170  }
171  else if (method == "compute") {
172  nargchk(nrhs==4 && nlhs<=1);
173  Mat left(rhs[2].toMat(CV_8U)),
174  right(rhs[3].toMat(CV_8U)),
175  disparity;
176  obj->compute(left, right, disparity);
177  plhs[0] = MxArray(disparity);
178  }
179  else if (method == "get") {
180  nargchk(nrhs==3 && nlhs<=1);
181  string prop(rhs[2].toString());
182  if (prop == "NumDisparities")
183  plhs[0] = MxArray(obj->getNumDisparities());
184  else if (prop == "BlockSize")
185  plhs[0] = MxArray(obj->getBlockSize());
186  else if (prop == "MinDisparity")
187  plhs[0] = MxArray(obj->getMinDisparity());
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 == "Disp12MaxDiff")
193  plhs[0] = MxArray(obj->getDisp12MaxDiff());
194  else if (prop == "PreFilterType")
195  plhs[0] = MxArray(InvPreFilerTypeMap[obj->getPreFilterType()]);
196  else if (prop == "PreFilterSize")
197  plhs[0] = MxArray(obj->getPreFilterSize());
198  else if (prop == "PreFilterCap")
199  plhs[0] = MxArray(obj->getPreFilterCap());
200  else if (prop == "TextureThreshold")
201  plhs[0] = MxArray(obj->getTextureThreshold());
202  else if (prop == "UniquenessRatio")
203  plhs[0] = MxArray(obj->getUniquenessRatio());
204  else if (prop == "SmallerBlockSize")
205  plhs[0] = MxArray(obj->getSmallerBlockSize());
206  else if (prop == "ROI1")
207  plhs[0] = MxArray(obj->getROI1());
208  else if (prop == "ROI2")
209  plhs[0] = MxArray(obj->getROI2());
210  else
211  mexErrMsgIdAndTxt("mexopencv:error", "Unrecognized property");
212  }
213  else if (method == "set") {
214  nargchk(nrhs==4 && nlhs==0);
215  string prop(rhs[2].toString());
216  if (prop == "NumDisparities")
217  obj->setNumDisparities(rhs[3].toInt());
218  else if (prop == "BlockSize")
219  obj->setBlockSize(rhs[3].toInt());
220  else if (prop == "MinDisparity")
221  obj->setMinDisparity(rhs[3].toInt());
222  else if (prop == "SpeckleWindowSize")
223  obj->setSpeckleWindowSize(rhs[3].toInt());
224  else if (prop == "SpeckleRange")
225  obj->setSpeckleRange(rhs[3].toInt());
226  else if (prop == "Disp12MaxDiff")
227  obj->setDisp12MaxDiff(rhs[3].toInt());
228  else if (prop == "PreFilterType")
229  obj->setPreFilterType(rhs[3].isChar() ?
230  PreFilerTypeMap[rhs[3].toString()] : rhs[3].toInt());
231  else if (prop == "PreFilterSize")
232  obj->setPreFilterSize(rhs[3].toInt());
233  else if (prop == "PreFilterCap")
234  obj->setPreFilterCap(rhs[3].toInt());
235  else if (prop == "TextureThreshold")
236  obj->setTextureThreshold(rhs[3].toInt());
237  else if (prop == "UniquenessRatio")
238  obj->setUniquenessRatio(rhs[3].toInt());
239  else if (prop == "SmallerBlockSize")
240  obj->setSmallerBlockSize(rhs[3].toInt());
241  else if (prop == "ROI1")
242  obj->setROI1(rhs[3].toRect());
243  else if (prop == "ROI2")
244  obj->setROI2(rhs[3].toRect());
245  else
246  mexErrMsgIdAndTxt("mexopencv:error", "Unrecognized property");
247  }
248  else
249  mexErrMsgIdAndTxt("mexopencv:error",
250  "Unrecognized operation %s", method.c_str());
251 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: StereoBM_.cpp:114
int toInt() const
Convert MxArray to int.
Definition: MxArray.cpp:489
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