mexopencv  3.4.1
MEX interface for OpenCV library
GeneralizedHoughBallard_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/imgproc.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 // Persistent objects
14 namespace {
16 int last_id = 0;
19 }
20 
28 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
29 {
30  // Check the number of arguments
31  nargchk(nrhs>=2 && nlhs<=2);
32 
33  // Arguments vector
34  vector<MxArray> rhs(prhs, prhs+nrhs);
35  int id = rhs[0].toInt();
36  string method(rhs[1].toString());
37 
38  // Constructor is called. Create a new object from argument
39  if (method == "new") {
40  nargchk(nrhs==2 && nlhs<=1);
42  plhs[0] = MxArray(last_id);
43  mexLock();
44  return;
45  }
46 
47  // Big operation switch
49  if (obj.empty())
50  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
51  if (method == "delete") {
52  nargchk(nrhs==2 && nlhs==0);
53  obj_.erase(id);
54  mexUnlock();
55  }
56  else if (method == "clear") {
57  nargchk(nrhs==2 && nlhs==0);
58  obj->clear();
59  }
60  else if (method == "load") {
61  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
62  string objname;
63  bool loadFromString = false;
64  for (int i=3; i<nrhs; i+=2) {
65  string key(rhs[i].toString());
66  if (key == "ObjName")
67  objname = rhs[i+1].toString();
68  else if (key == "FromString")
69  loadFromString = rhs[i+1].toBool();
70  else
71  mexErrMsgIdAndTxt("mexopencv:error",
72  "Unrecognized option %s", key.c_str());
73  }
74  /*
75  obj_[id] = (loadFromString ?
76  Algorithm::loadFromString<GeneralizedHoughBallard>(rhs[2].toString(), objname) :
77  Algorithm::load<GeneralizedHoughBallard>(rhs[2].toString(), objname));
78  */
80  // HACK: workaround for missing GeneralizedHoughBallard::create()
81  FileStorage fs(rhs[2].toString(), FileStorage::READ +
82  (loadFromString ? FileStorage::MEMORY : 0));
83  if (!fs.isOpened())
84  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
85  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
86  if (fn.empty())
87  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
88  obj->read(fn);
89  //*/
90  }
91  else if (method == "save") {
92  nargchk(nrhs==3 && nlhs==0);
93  obj->save(rhs[2].toString());
94  }
95  else if (method == "empty") {
96  nargchk(nrhs==2 && nlhs<=1);
97  plhs[0] = MxArray(obj->empty());
98  }
99  else if (method == "getDefaultName") {
100  nargchk(nrhs==2 && nlhs<=1);
101  plhs[0] = MxArray(obj->getDefaultName());
102  }
103  else if (method == "detect") {
104  nargchk((nrhs==3 || nrhs==5) && nlhs<=2);
105  vector<Vec4f> positions;
106  vector<Vec3i> votes;
107  if (nrhs == 3) {
108  Mat image(rhs[2].toMat(CV_8U));
109  obj->detect(image, positions, (nlhs>1) ? votes : noArray());
110  }
111  else {
112  Mat edges(rhs[2].toMat(CV_8U)),
113  dx(rhs[3].toMat(CV_32F)),
114  dy(rhs[4].toMat(CV_32F));
115  obj->detect(edges, dx, dy, positions, (nlhs>1) ? votes : noArray());
116  }
117  plhs[0] = MxArray(positions);
118  if (nlhs>1)
119  plhs[1] = MxArray(votes);
120  }
121  else if (method == "setTemplate") {
122  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
123  bool edges_variant = (nrhs>=5 && rhs[3].isNumeric() && rhs[4].isNumeric());
124  Point templCenter(-1,-1);
125  for (int i=(edges_variant ? 5 : 3); i<nrhs; i+=2) {
126  string key(rhs[i].toString());
127  if (key == "Center")
128  templCenter = rhs[i+1].toPoint();
129  else
130  mexErrMsgIdAndTxt("mexopencv:error",
131  "Unrecognized option %s", key.c_str());
132  }
133  if (edges_variant) {
134  Mat edges(rhs[2].toMat(CV_8U)),
135  dx(rhs[3].toMat(CV_32F)),
136  dy(rhs[4].toMat(CV_32F));
137  obj->setTemplate(edges, dx, dy, templCenter);
138  }
139  else {
140  Mat templ(rhs[2].toMat(CV_8U));
141  obj->setTemplate(templ, templCenter);
142  }
143  }
144  else if (method == "get") {
145  nargchk(nrhs==3 && nlhs<=1);
146  string prop(rhs[2].toString());
147  if (prop == "CannyHighThresh")
148  plhs[0] = MxArray(obj->getCannyHighThresh());
149  else if (prop == "CannyLowThresh")
150  plhs[0] = MxArray(obj->getCannyLowThresh());
151  else if (prop == "Dp")
152  plhs[0] = MxArray(obj->getDp());
153  else if (prop == "MaxBufferSize")
154  plhs[0] = MxArray(obj->getMaxBufferSize());
155  else if (prop == "MinDist")
156  plhs[0] = MxArray(obj->getMinDist());
157  else if (prop == "Levels")
158  plhs[0] = MxArray(obj->getLevels());
159  else if (prop == "VotesThreshold")
160  plhs[0] = MxArray(obj->getVotesThreshold());
161  else
162  mexErrMsgIdAndTxt("mexopencv:error",
163  "Unrecognized property %s", prop.c_str());
164  }
165  else if (method == "set") {
166  nargchk(nrhs==4 && nlhs==0);
167  string prop(rhs[2].toString());
168  if (prop == "CannyHighThresh")
169  obj->setCannyHighThresh(rhs[3].toInt());
170  else if (prop == "CannyLowThresh")
171  obj->setCannyLowThresh(rhs[3].toInt());
172  else if (prop == "Dp")
173  obj->setDp(rhs[3].toDouble());
174  else if (prop == "MaxBufferSize")
175  obj->setMaxBufferSize(rhs[3].toInt());
176  else if (prop == "MinDist")
177  obj->setMinDist(rhs[3].toDouble());
178  else if (prop == "Levels")
179  obj->setLevels(rhs[3].toInt());
180  else if (prop == "VotesThreshold")
181  obj->setVotesThreshold(rhs[3].toInt());
182  else
183  mexErrMsgIdAndTxt("mexopencv:error",
184  "Unrecognized property %s", prop.c_str());
185  }
186  else
187  mexErrMsgIdAndTxt("mexopencv:error",
188  "Unrecognized operation %s", method.c_str());
189 }
virtual double getMinDist() const=0
T empty(T... args)
virtual int getCannyLowThresh() const=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
#define CV_8U
virtual int getVotesThreshold() const=0
STL namespace.
virtual bool isOpened() const
map< int, Ptr< GeneralizedHoughBallard > > obj_
Object container.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void setCannyLowThresh(int cannyLowThresh)=0
virtual void setMinDist(double minDist)=0
virtual void clear()
#define CV_32F
virtual void setDp(double dp)=0
virtual void read(const FileNode &fn)
InputOutputArray noArray()
virtual void setMaxBufferSize(int maxBufferSize)=0
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 int getCannyHighThresh() const=0
virtual void setTemplate(InputArray templ, Point templCenter=Point(-1, -1))=0
virtual void setLevels(int levels)=0
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
Ptr< GeneralizedHoughBallard > createGeneralizedHoughBallard()
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
FileNode getFirstTopLevelNode() const
STL class.
bool empty() const
virtual void setVotesThreshold(int votesThreshold)=0
virtual void detect(InputArray image, OutputArray positions, OutputArray votes=noArray())=0
virtual String getDefaultName() const
Global constant definitions.
virtual int getMaxBufferSize() const=0
virtual double getDp() const=0
T c_str(T... args)
virtual void setCannyHighThresh(int cannyHighThresh)=0
virtual void save(const String &filename) const
virtual bool empty() const
cv::Mat toMat() const
virtual int getLevels() const=0