mexopencv  0.1
mex interface for opencv library
GeneralizedHoughBallard_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 // Persistent objects
13 namespace {
15 int last_id = 0;
17 map<int,Ptr<GeneralizedHoughBallard> > obj_;
18 }
19 
27 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
28 {
29  // Check the number of arguments
30  nargchk(nrhs>=2 && nlhs<=2);
31 
32  // Arguments vector
33  vector<MxArray> rhs(prhs, prhs+nrhs);
34  int id = rhs[0].toInt();
35  string method(rhs[1].toString());
36 
37  // Constructor is called. Create a new object from argument
38  if (method == "new") {
39  nargchk(nrhs==2 && nlhs<=1);
40  obj_[++last_id] = createGeneralizedHoughBallard();
41  plhs[0] = MxArray(last_id);
42  return;
43  }
44 
45  // Big operation switch
46  Ptr<GeneralizedHoughBallard> obj = obj_[id];
47  if (method == "delete") {
48  nargchk(nrhs==2 && nlhs==0);
49  obj_.erase(id);
50  }
51  else if (method == "clear") {
52  nargchk(nrhs==2 && nlhs==0);
53  obj->clear();
54  }
55  else if (method == "load") {
56  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
57  string objname;
58  bool loadFromString = false;
59  for (int i=3; i<nrhs; i+=2) {
60  string key(rhs[i].toString());
61  if (key=="ObjName")
62  objname = rhs[i+1].toString();
63  else if (key=="FromString")
64  loadFromString = rhs[i+1].toBool();
65  else
66  mexErrMsgIdAndTxt("mexopencv:error", "Unrecognized option %s", key.c_str());
67  }
68  /*
69  obj_[id] = (loadFromString ?
70  Algorithm::loadFromString<GeneralizedHoughBallard>(rhs[2].toString(), objname) :
71  Algorithm::load<GeneralizedHoughBallard>(rhs[2].toString(), objname));
72  */
74  // HACK: workaround for missing GeneralizedHoughBallard::create()
75  FileStorage fs(rhs[2].toString(), FileStorage::READ +
76  (loadFromString ? FileStorage::MEMORY : 0));
77  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
78  if (obj.empty())
79  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
80  //*/
81  }
82  else if (method == "save") {
83  nargchk(nrhs==3 && nlhs==0);
84  obj->save(rhs[2].toString());
85  }
86  else if (method == "empty") {
87  nargchk(nrhs==2 && nlhs<=1);
88  plhs[0] = MxArray(obj->empty());
89  }
90  else if (method == "getDefaultName") {
91  nargchk(nrhs==2 && nlhs<=1);
92  plhs[0] = MxArray(obj->getDefaultName());
93  }
94  else if (method == "detect") {
95  nargchk((nrhs==3 || nrhs==5) && nlhs<=2);
96  vector<Vec4f> positions;
97  vector<Vec3i> votes;
98  if (nrhs == 3) {
99  Mat image(rhs[2].toMat(CV_8U));
100  obj->detect(image, positions, (nlhs>1) ? votes : noArray());
101  }
102  else {
103  Mat edges(rhs[2].toMat(CV_8U)),
104  dx(rhs[3].toMat(CV_32F)),
105  dy(rhs[4].toMat(CV_32F));
106  obj->detect(edges, dx, dy, positions, (nlhs>1) ? votes : noArray());
107  }
108  plhs[0] = MxArray(positions);
109  if (nlhs>1)
110  plhs[1] = MxArray(votes);
111  }
112  else if (method == "setTemplate") {
113  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
114  bool edges_variant = (nrhs>=5 && rhs[3].isNumeric() && rhs[4].isNumeric());
115  Point templCenter(-1,-1);
116  for (int i=(edges_variant ? 5 : 3); i<nrhs; i+=2) {
117  string key(rhs[i].toString());
118  if (key=="Center")
119  templCenter = rhs[i+1].toPoint();
120  else
121  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
122  }
123  if (edges_variant) {
124  Mat edges(rhs[2].toMat(CV_8U)),
125  dx(rhs[3].toMat(CV_32F)),
126  dy(rhs[4].toMat(CV_32F));
127  obj->setTemplate(edges, dx, dy, templCenter);
128  }
129  else {
130  Mat templ(rhs[2].toMat(CV_8U));
131  obj->setTemplate(templ, templCenter);
132  }
133  }
134  else if (method == "get") {
135  nargchk(nrhs==3 && nlhs<=1);
136  string prop(rhs[2].toString());
137  if (prop == "CannyHighThresh")
138  plhs[0] = MxArray(obj->getCannyHighThresh());
139  else if (prop == "CannyLowThresh")
140  plhs[0] = MxArray(obj->getCannyLowThresh());
141  else if (prop == "Dp")
142  plhs[0] = MxArray(obj->getDp());
143  else if (prop == "MaxBufferSize")
144  plhs[0] = MxArray(obj->getMaxBufferSize());
145  else if (prop == "MinDist")
146  plhs[0] = MxArray(obj->getMinDist());
147  else if (prop == "Levels")
148  plhs[0] = MxArray(obj->getLevels());
149  else if (prop == "VotesThreshold")
150  plhs[0] = MxArray(obj->getVotesThreshold());
151  else
152  mexErrMsgIdAndTxt("mexopencv:error", "Unrecognized property %s", prop.c_str());
153  }
154  else if (method == "set") {
155  nargchk(nrhs==4 && nlhs==0);
156  string prop(rhs[2].toString());
157  if (prop == "CannyHighThresh")
158  obj->setCannyHighThresh(rhs[3].toInt());
159  else if (prop == "CannyLowThresh")
160  obj->setCannyLowThresh(rhs[3].toInt());
161  else if (prop == "Dp")
162  obj->setDp(rhs[3].toDouble());
163  else if (prop == "MaxBufferSize")
164  obj->setMaxBufferSize(rhs[3].toInt());
165  else if (prop == "MinDist")
166  obj->setMinDist(rhs[3].toDouble());
167  else if (prop == "Levels")
168  obj->setLevels(rhs[3].toInt());
169  else if (prop == "VotesThreshold")
170  obj->setVotesThreshold(rhs[3].toInt());
171  else
172  mexErrMsgIdAndTxt("mexopencv:error", "Unrecognized property %s", prop.c_str());
173  }
174  else
175  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized operation");
176 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
Global constant definitions.