17 map<int,Ptr<StereoBM> > obj_;
21 (
"NormalizedResponse", cv::StereoBM::PREFILTER_NORMALIZED_RESPONSE)
22 (
"XSobel", cv::StereoBM::PREFILTER_XSOBEL);
24 (cv::StereoBM::PREFILTER_NORMALIZED_RESPONSE,
"NormalizedResponse")
25 (cv::StereoBM::PREFILTER_XSOBEL,
"XSobel");
32 Ptr<StereoBM> create_StereoBM(
33 vector<MxArray>::const_iterator first,
34 vector<MxArray>::const_iterator last)
36 ptrdiff_t len = std::distance(first, last);
38 int numDisparities = 0;
41 int speckleWindowSize = 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;
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")
82 else if (key ==
"ROI2")
85 mexErrMsgIdAndTxt(
"mexopencv:error",
86 "Unrecognized option %s", key.c_str());
88 Ptr<StereoBM> p = StereoBM::create(numDisparities, blockSize);
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);
114 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
120 vector<MxArray> rhs(prhs, prhs+nrhs);
121 int id = rhs[0].toInt();
122 string method(rhs[1].toString());
125 if (method ==
"new") {
127 obj_[++last_id] = create_StereoBM(rhs.begin() + 2, rhs.end());
133 Ptr<StereoBM> obj = obj_[id];
134 if (method ==
"delete") {
138 else if (method ==
"clear") {
142 else if (method ==
"save") {
144 obj->save(rhs[2].toString());
146 else if (method ==
"load") {
147 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
149 bool loadFromString =
false;
150 for (
int i=3; i<nrhs; i+=2) {
151 string key(rhs[i].toString());
153 objname = rhs[i+1].toString();
154 else if (key==
"FromString")
155 loadFromString = rhs[i+1].toBool();
157 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
159 obj_[id] = (loadFromString ?
160 Algorithm::loadFromString<StereoBM>(rhs[2].toString(), objname) :
161 Algorithm::load<StereoBM>(rhs[2].toString(), objname));
163 else if (method ==
"empty") {
165 plhs[0] =
MxArray(obj->empty());
167 else if (method ==
"getDefaultName") {
169 plhs[0] =
MxArray(obj->getDefaultName());
171 else if (method ==
"compute") {
173 Mat left(rhs[2].toMat(CV_8U)),
174 right(rhs[3].toMat(CV_8U)),
176 obj->compute(left, right, disparity);
179 else if (method ==
"get") {
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());
211 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property");
213 else if (method ==
"set") {
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());
246 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property");
249 mexErrMsgIdAndTxt(
"mexopencv:error",
250 "Unrecognized operation %s", method.c_str());
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
int toInt() const
Convert MxArray to int.
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.