mexopencv  3.4.1
MEX interface for OpenCV library
StereoSGBM_.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 
24  ("SGBM3Way", cv::StereoSGBM::MODE_SGBM_3WAY)
25  ("HH4", cv::StereoSGBM::MODE_HH4);
29  (cv::StereoSGBM::MODE_SGBM_3WAY, "SGBM3Way")
30  (cv::StereoSGBM::MODE_HH4, "HH4");
31 
40 {
41  ptrdiff_t len = std::distance(first, last);
42  nargchk((len%2)==0);
43  int minDisparity = 0;
44  int numDisparities = 16;
45  int blockSize = 3;
46  int P1 = 0;
47  int P2 = 0;
48  int disp12MaxDiff = 0;
49  int preFilterCap = 0;
50  int uniquenessRatio = 0;
51  int speckleWindowSize = 0;
52  int speckleRange = 0;
53  int mode = cv::StereoSGBM::MODE_SGBM;
54  for (; first != last; first += 2) {
55  string key(first->toString());
56  const MxArray& val = *(first + 1);
57  if (key == "MinDisparity")
58  minDisparity = val.toInt();
59  else if (key == "NumDisparities")
60  numDisparities = val.toInt();
61  else if (key == "BlockSize")
62  blockSize = val.toInt();
63  else if (key == "P1")
64  P1 = val.toInt();
65  else if (key == "P2")
66  P2 = val.toInt();
67  else if (key == "Disp12MaxDiff")
68  disp12MaxDiff = val.toInt();
69  else if (key == "PreFilterCap")
70  preFilterCap = val.toInt();
71  else if (key == "UniquenessRatio")
72  uniquenessRatio = val.toInt();
73  else if (key == "SpeckleWindowSize")
74  speckleWindowSize = val.toInt();
75  else if (key == "SpeckleRange")
76  speckleRange = val.toInt();
77  else if (key == "Mode")
78  mode = (val.isChar() ?
79  SGBMModeMap[val.toString()] : val.toInt());
80  else
81  mexErrMsgIdAndTxt("mexopencv:error",
82  "Unrecognized option %s", key.c_str());
83  }
84  return StereoSGBM::create(minDisparity, numDisparities,
85  blockSize, P1, P2, disp12MaxDiff, preFilterCap, uniquenessRatio,
86  speckleWindowSize, speckleRange, mode);
87 }
88 }
89 
97 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
98 {
99  // Check the number of arguments
100  nargchk(nrhs>=2 && nlhs<=1);
101 
102  // Argument vector
103  vector<MxArray> rhs(prhs, prhs+nrhs);
104  int id = rhs[0].toInt();
105  string method(rhs[1].toString());
106 
107  // Constructor is called. Create a new object from argument
108  if (method == "new") {
109  nargchk(nrhs>=2 && nlhs<=1);
110  obj_[++last_id] = create_StereoSGBM(rhs.begin() + 2, rhs.end());
111  plhs[0] = MxArray(last_id);
112  mexLock();
113  return;
114  }
115 
116  // Big operation switch
117  Ptr<StereoSGBM> obj = obj_[id];
118  if (obj.empty())
119  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
120  if (method == "delete") {
121  nargchk(nrhs==2 && nlhs==0);
122  obj_.erase(id);
123  mexUnlock();
124  }
125  else if (method == "clear") {
126  nargchk(nrhs==2 && nlhs==0);
127  obj->clear();
128  }
129  else if (method == "save") {
130  nargchk(nrhs==3 && nlhs==0);
131  obj->save(rhs[2].toString());
132  }
133  else if (method == "load") {
134  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
135  string objname;
136  bool loadFromString = false;
137  for (int i=3; i<nrhs; i+=2) {
138  string key(rhs[i].toString());
139  if (key == "ObjName")
140  objname = rhs[i+1].toString();
141  else if (key == "FromString")
142  loadFromString = rhs[i+1].toBool();
143  else
144  mexErrMsgIdAndTxt("mexopencv:error",
145  "Unrecognized option %s", key.c_str());
146  }
147  obj_[id] = (loadFromString ?
148  Algorithm::loadFromString<StereoSGBM>(rhs[2].toString(), objname) :
149  Algorithm::load<StereoSGBM>(rhs[2].toString(), objname));
150  }
151  else if (method == "empty") {
152  nargchk(nrhs==2 && nlhs<=1);
153  plhs[0] = MxArray(obj->empty());
154  }
155  else if (method == "getDefaultName") {
156  nargchk(nrhs==2 && nlhs<=1);
157  plhs[0] = MxArray(obj->getDefaultName());
158  }
159  else if (method == "compute") {
160  nargchk(nrhs==4 && nlhs<=1);
161  Mat left(rhs[2].toMat(CV_8U)),
162  right(rhs[3].toMat(CV_8U)),
163  disparity;
164  obj->compute(left, right, disparity);
165  plhs[0] = MxArray(disparity);
166  }
167  else if (method == "get") {
168  nargchk(nrhs==3 && nlhs<=1);
169  string prop(rhs[2].toString());
170  if (prop == "MinDisparity")
171  plhs[0] = MxArray(obj->getMinDisparity());
172  else if (prop == "NumDisparities")
173  plhs[0] = MxArray(obj->getNumDisparities());
174  else if (prop == "BlockSize")
175  plhs[0] = MxArray(obj->getBlockSize());
176  else if (prop == "P1")
177  plhs[0] = MxArray(obj->getP1());
178  else if (prop == "P2")
179  plhs[0] = MxArray(obj->getP2());
180  else if (prop == "Disp12MaxDiff")
181  plhs[0] = MxArray(obj->getDisp12MaxDiff());
182  else if (prop == "PreFilterCap")
183  plhs[0] = MxArray(obj->getPreFilterCap());
184  else if (prop == "UniquenessRatio")
185  plhs[0] = MxArray(obj->getUniquenessRatio());
186  else if (prop == "SpeckleWindowSize")
187  plhs[0] = MxArray(obj->getSpeckleWindowSize());
188  else if (prop == "SpeckleRange")
189  plhs[0] = MxArray(obj->getSpeckleRange());
190  else if (prop == "Mode")
191  plhs[0] = MxArray(InvSGBMModeMap[obj->getMode()]);
192  else
193  mexErrMsgIdAndTxt("mexopencv:error",
194  "Unrecognized property %s", prop.c_str());
195  }
196  else if (method == "set") {
197  nargchk(nrhs==4 && nlhs==0);
198  string prop(rhs[2].toString());
199  if (prop == "MinDisparity")
200  obj->setMinDisparity(rhs[3].toInt());
201  else if (prop == "NumDisparities")
202  obj->setNumDisparities(rhs[3].toInt());
203  else if (prop == "BlockSize")
204  obj->setBlockSize(rhs[3].toInt());
205  else if (prop == "P1")
206  obj->setP1(rhs[3].toInt());
207  else if (prop == "P2")
208  obj->setP2(rhs[3].toInt());
209  else if (prop == "Disp12MaxDiff")
210  obj->setDisp12MaxDiff(rhs[3].toInt());
211  else if (prop == "PreFilterCap")
212  obj->setPreFilterCap(rhs[3].toInt());
213  else if (prop == "UniquenessRatio")
214  obj->setUniquenessRatio(rhs[3].toInt());
215  else if (prop == "SpeckleWindowSize")
216  obj->setSpeckleWindowSize(rhs[3].toInt());
217  else if (prop == "SpeckleRange")
218  obj->setSpeckleRange(rhs[3].toInt());
219  else if (prop == "Mode")
220  obj->setMode(rhs[3].isChar() ?
221  SGBMModeMap[rhs[3].toString()] : rhs[3].toInt());
222  else
223  mexErrMsgIdAndTxt("mexopencv:error",
224  "Unrecognized property %s", prop.c_str());
225  }
226  else
227  mexErrMsgIdAndTxt("mexopencv:error",
228  "Unrecognized operation %s", method.c_str());
229 }
int toInt() const
Convert MxArray to int.
Definition: MxArray.cpp:489
virtual void setP2(int P2)=0
T distance(T... args)
virtual int getSpeckleRange() const=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual int getP1() const=0
#define CV_8U
virtual void setMinDisparity(int minDisparity)=0
virtual int getMode() const=0
T left(T... args)
virtual void compute(InputArray left, InputArray right, OutputArray disparity)=0
STL namespace.
virtual void setSpeckleWindowSize(int speckleWindowSize)=0
T end(T... args)
virtual void setUniquenessRatio(int uniquenessRatio)=0
virtual void setNumDisparities(int numDisparities)=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
const ConstMap< string, int > SGBMModeMap
Option values for StereoSGBM mode.
Definition: StereoSGBM_.cpp:21
virtual int getP2() const=0
virtual int getUniquenessRatio() const=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: StereoSGBM_.cpp:97
const ConstMap< int, string > InvSGBMModeMap
Definition: StereoSGBM_.cpp:26
virtual void setDisp12MaxDiff(int disp12MaxDiff)=0
virtual void clear()
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
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
int last_id
Last object id to allocate.
Definition: StereoSGBM_.cpp:16
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
STL class.
bool empty() const
virtual void setMode(int mode)=0
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
Ptr< StereoSGBM > create_StereoSGBM(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of StereoSGBM using options in arguments.
Definition: StereoSGBM_.cpp:37
virtual void setP1(int P1)=0
virtual int getDisp12MaxDiff() const=0
virtual void setPreFilterCap(int preFilterCap)=0
virtual void save(const String &filename) const
virtual bool empty() const
virtual int getPreFilterCap() const=0
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
map< int, Ptr< StereoSGBM > > obj_
Object container.
Definition: StereoSGBM_.cpp:18
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