17 map<int,Ptr<StereoSGBM> > obj_;
21 (
"SGBM", cv::StereoSGBM::MODE_SGBM)
22 (
"HH", cv::StereoSGBM::MODE_HH)
23 (
"SGBM3Way", cv::StereoSGBM::MODE_SGBM_3WAY);
25 (cv::StereoSGBM::MODE_SGBM,
"SGBM")
26 (cv::StereoSGBM::MODE_HH,
"HH")
27 (cv::StereoSGBM::MODE_SGBM_3WAY,
"SGBM3Way");
34 Ptr<StereoSGBM> create_StereoSGBM(
35 vector<MxArray>::const_iterator first,
36 vector<MxArray>::const_iterator last)
38 ptrdiff_t len = std::distance(first, last);
41 int numDisparities = 64;
45 int disp12MaxDiff = 0;
47 int uniquenessRatio = 0;
48 int speckleWindowSize = 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();
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());
78 mexErrMsgIdAndTxt(
"mexopencv:error",
79 "Unrecognized option %s", key.c_str());
81 return StereoSGBM::create(minDisparity, numDisparities,
82 blockSize, P1, P2, disp12MaxDiff, preFilterCap, uniquenessRatio,
83 speckleWindowSize, speckleRange, mode);
94 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
100 vector<MxArray> rhs(prhs, prhs+nrhs);
101 int id = rhs[0].toInt();
102 string method(rhs[1].toString());
105 if (method ==
"new") {
107 obj_[++last_id] = create_StereoSGBM(rhs.begin() + 2, rhs.end());
113 Ptr<StereoSGBM> obj = obj_[id];
114 if (method ==
"delete") {
118 else if (method ==
"clear") {
122 else if (method ==
"save") {
124 obj->save(rhs[2].toString());
126 else if (method ==
"load") {
127 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
129 bool loadFromString =
false;
130 for (
int i=3; i<nrhs; i+=2) {
131 string key(rhs[i].toString());
133 objname = rhs[i+1].toString();
134 else if (key==
"FromString")
135 loadFromString = rhs[i+1].toBool();
137 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
146 FileStorage fs(rhs[2].toString(), FileStorage::READ +
147 (loadFromString ? FileStorage::MEMORY : 0));
148 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
150 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
153 else if (method ==
"empty") {
155 plhs[0] =
MxArray(obj->empty());
157 else if (method ==
"getDefaultName") {
159 plhs[0] =
MxArray(obj->getDefaultName());
161 else if (method ==
"compute") {
163 Mat left(rhs[2].toMat(CV_8U)),
164 right(rhs[3].toMat(CV_8U)),
166 obj->compute(left, right, disparity);
169 else if (method ==
"get") {
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()]);
195 mexErrMsgIdAndTxt(
"mexopencv:error",
196 "Unrecognized property %s", prop.c_str());
198 else if (method ==
"set") {
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());
225 mexErrMsgIdAndTxt(
"mexopencv:error",
226 "Unrecognized property %s", prop.c_str());
229 mexErrMsgIdAndTxt(
"mexopencv:error",
230 "Unrecognized operation %s", method.c_str());
int toInt() const
Convert MxArray to int.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Global constant definitions.
std::map wrapper with one-line initialization and lookup method.