mexopencv  3.4.1
MEX interface for OpenCV library
StereoBM_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/calib3d.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 // Persistent objects
14 namespace {
16 int last_id = 0;
19 
22  ("NormalizedResponse", cv::StereoBM::PREFILTER_NORMALIZED_RESPONSE)
25  (cv::StereoBM::PREFILTER_NORMALIZED_RESPONSE, "NormalizedResponse")
27 
36 {
37  ptrdiff_t len = std::distance(first, last);
38  nargchk((len%2)==0);
39  int numDisparities = 0;
40  int blockSize = 21;
41  int minDisparity = 0;
42  int speckleWindowSize = 0;
43  int speckleRange = 0;
44  int disp12MaxDiff = -1;
45  int preFilterType = cv::StereoBM::PREFILTER_XSOBEL;
46  int preFilterSize = 9;
47  int preFilterCap = 31;
48  int textureThreshold = 10;
49  int uniquenessRatio = 15;
50  int smallerBlockSize = 0;
51  Rect roi1;
52  Rect roi2;
53  for (; first != last; first += 2) {
54  string key(first->toString());
55  const MxArray& val = *(first + 1);
56  if (key == "NumDisparities")
57  numDisparities = val.toInt();
58  else if (key == "BlockSize")
59  blockSize = val.toInt();
60  else if (key == "MinDisparity")
61  minDisparity = val.toInt();
62  else if (key == "SpeckleWindowSize")
63  speckleWindowSize = val.toInt();
64  else if (key == "SpeckleRange")
65  speckleRange = val.toInt();
66  else if (key == "Disp12MaxDiff")
67  disp12MaxDiff = val.toInt();
68  else if (key == "PreFilterType")
69  preFilterType = (val.isChar() ?
70  PreFilerTypeMap[val.toString()] : val.toInt());
71  else if (key == "PreFilterSize")
72  preFilterSize = val.toInt();
73  else if (key == "PreFilterCap")
74  preFilterCap = val.toInt();
75  else if (key == "TextureThreshold")
76  textureThreshold = val.toInt();
77  else if (key == "UniquenessRatio")
78  uniquenessRatio = val.toInt();
79  else if (key == "SmallerBlockSize")
80  smallerBlockSize = val.toInt();
81  else if (key == "ROI1")
82  roi1 = val.toRect();
83  else if (key == "ROI2")
84  roi2 = val.toRect();
85  else
86  mexErrMsgIdAndTxt("mexopencv:error",
87  "Unrecognized option %s", key.c_str());
88  }
89  Ptr<StereoBM> p = StereoBM::create(numDisparities, blockSize);
90  if (p.empty())
91  mexErrMsgIdAndTxt("mexopencv:error", "Failed to create StereoBM");
92  p->setMinDisparity(minDisparity);
93  p->setSpeckleWindowSize(speckleWindowSize);
94  p->setSpeckleRange(speckleRange);
95  p->setDisp12MaxDiff(disp12MaxDiff);
96  p->setPreFilterType(preFilterType);
97  p->setPreFilterSize(preFilterSize);
98  p->setPreFilterCap(preFilterCap);
99  p->setTextureThreshold(textureThreshold);
100  p->setUniquenessRatio(uniquenessRatio);
101  p->setSmallerBlockSize(smallerBlockSize);
102  p->setROI1(roi1);
103  p->setROI2(roi2);
104  return p;
105 }
106 }
107 
115 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
116 {
117  // Check the number of arguments
118  nargchk(nrhs>=2 && nlhs<=1);
119 
120  // Argument vector
121  vector<MxArray> rhs(prhs, prhs+nrhs);
122  int id = rhs[0].toInt();
123  string method(rhs[1].toString());
124 
125  // Constructor is called. Create a new object from argument
126  if (method == "new") {
127  nargchk(nrhs>=2 && nlhs<=1);
128  obj_[++last_id] = create_StereoBM(rhs.begin() + 2, rhs.end());
129  plhs[0] = MxArray(last_id);
130  mexLock();
131  return;
132  }
133 
134  // Big operation switch
135  Ptr<StereoBM> obj = obj_[id];
136  if (obj.empty())
137  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
138  if (method == "delete") {
139  nargchk(nrhs==2 && nlhs==0);
140  obj_.erase(id);
141  mexUnlock();
142  }
143  else if (method == "clear") {
144  nargchk(nrhs==2 && nlhs==0);
145  obj->clear();
146  }
147  else if (method == "save") {
148  nargchk(nrhs==3 && nlhs==0);
149  obj->save(rhs[2].toString());
150  }
151  else if (method == "load") {
152  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
153  string objname;
154  bool loadFromString = false;
155  for (int i=3; i<nrhs; i+=2) {
156  string key(rhs[i].toString());
157  if (key == "ObjName")
158  objname = rhs[i+1].toString();
159  else if (key == "FromString")
160  loadFromString = rhs[i+1].toBool();
161  else
162  mexErrMsgIdAndTxt("mexopencv:error",
163  "Unrecognized option %s", key.c_str());
164  }
165  obj_[id] = (loadFromString ?
166  Algorithm::loadFromString<StereoBM>(rhs[2].toString(), objname) :
167  Algorithm::load<StereoBM>(rhs[2].toString(), objname));
168  }
169  else if (method == "empty") {
170  nargchk(nrhs==2 && nlhs<=1);
171  plhs[0] = MxArray(obj->empty());
172  }
173  else if (method == "getDefaultName") {
174  nargchk(nrhs==2 && nlhs<=1);
175  plhs[0] = MxArray(obj->getDefaultName());
176  }
177  else if (method == "compute") {
178  nargchk(nrhs==4 && nlhs<=1);
179  Mat left(rhs[2].toMat(CV_8U)),
180  right(rhs[3].toMat(CV_8U)),
181  disparity;
182  obj->compute(left, right, disparity);
183  plhs[0] = MxArray(disparity);
184  }
185  else if (method == "get") {
186  nargchk(nrhs==3 && nlhs<=1);
187  string prop(rhs[2].toString());
188  if (prop == "NumDisparities")
189  plhs[0] = MxArray(obj->getNumDisparities());
190  else if (prop == "BlockSize")
191  plhs[0] = MxArray(obj->getBlockSize());
192  else if (prop == "MinDisparity")
193  plhs[0] = MxArray(obj->getMinDisparity());
194  else if (prop == "SpeckleWindowSize")
195  plhs[0] = MxArray(obj->getSpeckleWindowSize());
196  else if (prop == "SpeckleRange")
197  plhs[0] = MxArray(obj->getSpeckleRange());
198  else if (prop == "Disp12MaxDiff")
199  plhs[0] = MxArray(obj->getDisp12MaxDiff());
200  else if (prop == "PreFilterType")
201  plhs[0] = MxArray(InvPreFilerTypeMap[obj->getPreFilterType()]);
202  else if (prop == "PreFilterSize")
203  plhs[0] = MxArray(obj->getPreFilterSize());
204  else if (prop == "PreFilterCap")
205  plhs[0] = MxArray(obj->getPreFilterCap());
206  else if (prop == "TextureThreshold")
207  plhs[0] = MxArray(obj->getTextureThreshold());
208  else if (prop == "UniquenessRatio")
209  plhs[0] = MxArray(obj->getUniquenessRatio());
210  else if (prop == "SmallerBlockSize")
211  plhs[0] = MxArray(obj->getSmallerBlockSize());
212  else if (prop == "ROI1")
213  plhs[0] = MxArray(obj->getROI1());
214  else if (prop == "ROI2")
215  plhs[0] = MxArray(obj->getROI2());
216  else
217  mexErrMsgIdAndTxt("mexopencv:error",
218  "Unrecognized property %s", prop.c_str());
219  }
220  else if (method == "set") {
221  nargchk(nrhs==4 && nlhs==0);
222  string prop(rhs[2].toString());
223  if (prop == "NumDisparities")
224  obj->setNumDisparities(rhs[3].toInt());
225  else if (prop == "BlockSize")
226  obj->setBlockSize(rhs[3].toInt());
227  else if (prop == "MinDisparity")
228  obj->setMinDisparity(rhs[3].toInt());
229  else if (prop == "SpeckleWindowSize")
230  obj->setSpeckleWindowSize(rhs[3].toInt());
231  else if (prop == "SpeckleRange")
232  obj->setSpeckleRange(rhs[3].toInt());
233  else if (prop == "Disp12MaxDiff")
234  obj->setDisp12MaxDiff(rhs[3].toInt());
235  else if (prop == "PreFilterType")
236  obj->setPreFilterType(rhs[3].isChar() ?
237  PreFilerTypeMap[rhs[3].toString()] : rhs[3].toInt());
238  else if (prop == "PreFilterSize")
239  obj->setPreFilterSize(rhs[3].toInt());
240  else if (prop == "PreFilterCap")
241  obj->setPreFilterCap(rhs[3].toInt());
242  else if (prop == "TextureThreshold")
243  obj->setTextureThreshold(rhs[3].toInt());
244  else if (prop == "UniquenessRatio")
245  obj->setUniquenessRatio(rhs[3].toInt());
246  else if (prop == "SmallerBlockSize")
247  obj->setSmallerBlockSize(rhs[3].toInt());
248  else if (prop == "ROI1")
249  obj->setROI1(rhs[3].toRect());
250  else if (prop == "ROI2")
251  obj->setROI2(rhs[3].toRect());
252  else
253  mexErrMsgIdAndTxt("mexopencv:error",
254  "Unrecognized property %s", prop.c_str());
255  }
256  else
257  mexErrMsgIdAndTxt("mexopencv:error",
258  "Unrecognized operation %s", method.c_str());
259 }
virtual int getTextureThreshold() const=0
int toInt() const
Convert MxArray to int.
Definition: MxArray.cpp:489
T distance(T... args)
virtual int getPreFilterSize() const=0
virtual int getSpeckleRange() const=0
int last_id
Last object id to allocate.
Definition: StereoBM_.cpp:16
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual int getPreFilterType() const=0
#define CV_8U
virtual void setMinDisparity(int minDisparity)=0
T left(T... args)
PREFILTER_NORMALIZED_RESPONSE
virtual void compute(InputArray left, InputArray right, OutputArray disparity)=0
STL namespace.
virtual void setSpeckleWindowSize(int speckleWindowSize)=0
T end(T... args)
const ConstMap< int, string > InvPreFilerTypeMap
Definition: StereoBM_.cpp:24
virtual void setNumDisparities(int numDisparities)=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: StereoBM_.cpp:115
virtual Rect getROI1() const=0
virtual int getUniquenessRatio() const=0
virtual void setTextureThreshold(int textureThreshold)=0
virtual void setDisp12MaxDiff(int disp12MaxDiff)=0
virtual Rect getROI2() const=0
virtual void clear()
Ptr< StereoBM > create_StereoBM(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of StereoBM using options in arguments.
Definition: StereoBM_.cpp:33
virtual void setROI1(Rect roi1)=0
virtual void setSmallerBlockSize(int blockSize)=0
const ConstMap< string, int > PreFilerTypeMap
Option values for StereoBM PreFilterType.
Definition: StereoBM_.cpp:21
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...
virtual void setBlockSize(int blockSize)=0
virtual void setPreFilterCap(int preFilterCap)=0
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
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
virtual int getSpeckleWindowSize() const=0
virtual int getBlockSize() const=0
virtual int getMinDisparity() const=0
virtual void setUniquenessRatio(int uniquenessRatio)=0
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
virtual int getDisp12MaxDiff() const=0
virtual void save(const String &filename) const
virtual bool empty() const
virtual void setROI2(Rect roi2)=0
virtual int getSmallerBlockSize() const=0
virtual int getPreFilterCap() const=0
map< int, Ptr< StereoBM > > obj_
Object container.
Definition: StereoBM_.cpp:18
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
virtual void setPreFilterSize(int preFilterSize)=0
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
cv::Mat toMat() const
virtual void setSpeckleRange(int speckleRange)=0
virtual int getNumDisparities() const=0
virtual void setPreFilterType(int preFilterType)=0