17 map<int,Ptr<CascadeClassifier> > obj_;
27 class MatlabMaskGenerator :
public cv::BaseCascadeClassifier::MaskGenerator
33 explicit MatlabMaskGenerator(
const string &func)
39 void initializeMask(
const Mat& )
46 Mat generateMask(
const Mat& src)
49 mxArray *lhs, *rhs[2];
56 if (mexCallMATLAB(1, &lhs, 2, rhs,
"feval") == 0) {
58 CV_Assert(res.isNumeric());
59 mask = res.toMat(CV_8U);
63 mask = Mat(src.size(), CV_8U, Scalar::all(255));
68 mxDestroyArray(rhs[0]);
69 mxDestroyArray(rhs[1]);
81 s.set(
"fun", fun_name);
89 static Ptr<MatlabMaskGenerator> create(
const string &func)
91 return makePtr<MatlabMaskGenerator>(func);
106 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
112 vector<MxArray> rhs(prhs, prhs+nrhs);
113 int id = rhs[0].toInt();
114 string method(rhs[1].toString());
117 if (method ==
"new") {
119 obj_[++last_id] = makePtr<CascadeClassifier>();
123 else if (method ==
"convert") {
125 string oldcascade(rhs[2].toString()),
126 newcascade(rhs[3].toString());
127 bool success = CascadeClassifier::convert(oldcascade, newcascade);
131 mexErrMsgIdAndTxt(
"mexopencv:error",
"Conversion failed");
136 Ptr<CascadeClassifier> obj = obj_[id];
137 if (method ==
"delete") {
141 else if (method ==
"empty") {
143 plhs[0] =
MxArray(obj->empty());
145 else if (method ==
"load") {
147 string filename(rhs[2].toString());
148 bool success = obj->load(filename);
152 mexErrMsgIdAndTxt(
"mexopencv:error",
153 "Invalid path or file specified");
155 else if (method ==
"isOldFormatCascade") {
157 plhs[0] =
MxArray(obj->isOldFormatCascade());
159 else if (method ==
"getFeatureType") {
161 plhs[0] =
MxArray(FeatureTypeMap[obj->getFeatureType()]);
163 else if (method ==
"getOriginalWindowSize") {
165 plhs[0] =
MxArray(obj->getOriginalWindowSize());
167 else if (method ==
"getMaskGenerator") {
169 Ptr<BaseCascadeClassifier::MaskGenerator> p = obj->getMaskGenerator();
171 Ptr<MatlabMaskGenerator> pp = p.dynamicCast<MatlabMaskGenerator>();
173 plhs[0] = pp->toStruct();
179 else if (method ==
"setMaskGenerator") {
181 string str(rhs[2].toString());
182 Ptr<BaseCascadeClassifier::MaskGenerator> p;
183 if (str ==
"FaceDetectionMaskGenerator")
184 p = createFaceDetectionMaskGenerator();
186 p = MatlabMaskGenerator::create(str);
187 obj->setMaskGenerator(p);
189 else if (method ==
"detectMultiScale") {
190 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=3);
191 double scaleFactor = 1.1;
192 int minNeighbors = 3;
194 Size minSize, maxSize;
195 bool outputRejectLevels =
false;
196 for (
int i=3; i<rhs.size(); i+=2) {
197 string key(rhs[i].toString());
198 if (key==
"ScaleFactor")
199 scaleFactor = rhs[i+1].toDouble();
200 else if (key==
"MinNeighbors")
201 minNeighbors = rhs[i+1].toInt();
202 else if (key==
"DoCannyPruning")
203 UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CASCADE_DO_CANNY_PRUNING);
204 else if (key==
"ScaleImage")
205 UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CASCADE_SCALE_IMAGE);
206 else if (key==
"FindBiggestObject")
207 UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CASCADE_FIND_BIGGEST_OBJECT);
208 else if (key==
"DoRoughSearch")
209 UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::CASCADE_DO_ROUGH_SEARCH);
210 else if (key==
"Flags")
211 flags = rhs[i+1].toInt();
212 else if (key==
"MinSize")
213 minSize = rhs[i+1].toSize();
214 else if (key==
"MaxSize")
215 maxSize = rhs[i+1].toSize();
216 else if (key==
"OutputRejectLevels")
217 outputRejectLevels = rhs[i+1].toBool();
219 mexErrMsgIdAndTxt(
"mexopencv:error",
220 "Unrecognized option %s",key.c_str());
223 Mat image(rhs[2].toMat(CV_8U));
224 vector<Rect> objects;
225 if (nlhs > 2 || outputRejectLevels) {
226 outputRejectLevels =
true;
227 vector<int> rejectLevels;
228 vector<double> levelWeights;
229 obj->detectMultiScale(image, objects, rejectLevels, levelWeights,
230 scaleFactor, minNeighbors, flags, minSize, maxSize,
233 plhs[1] =
MxArray(rejectLevels);
235 plhs[2] =
MxArray(levelWeights);
238 vector<int> numDetections;
239 obj->detectMultiScale(image, objects, numDetections,
240 scaleFactor, minNeighbors, flags, minSize, maxSize);
241 plhs[1] =
MxArray(numDetections);
244 obj->detectMultiScale(image, objects,
245 scaleFactor, minNeighbors, flags, minSize, maxSize);
249 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized operation");
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
MxArray toStruct(const std::vector< cv::ml::DTrees::Node > &nodes)
Convert tree nodes to struct array.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
static MxArray Struct(const char **fields=NULL, int nfields=0, mwSize m=1, mwSize n=1)
Create a new struct array.
Global constant definitions.
std::map wrapper with one-line initialization and lookup method.