mexopencv  3.4.1
MEX interface for OpenCV library
TextDetectorCNN_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/text.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::text;
13 
14 // Persistent objects
15 namespace {
17 int last_id = 0;
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && nlhs<=2);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36  int id = rhs[0].toInt();
37  string method(rhs[1].toString());
38 
39  // constructor call
40  if (method == "new") {
41  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
42  vector<Size> detectionSizes(1, Size(300, 300));
43  for (int i=4; i<nrhs; i+=2) {
44  string key(rhs[i].toString());
45  if (key == "DetectionSizes")
46  detectionSizes = rhs[i+1].toVector<Size>();
47  else
48  mexErrMsgIdAndTxt("mexopencv:error",
49  "Unrecognized option %s", key.c_str());
50  }
51  string modelArchFilename(rhs[2].toString()),
52  modelWeightsFilename(rhs[3].toString());
54  modelArchFilename, modelWeightsFilename, detectionSizes);
55  plhs[0] = MxArray(last_id);
56  mexLock();
57  return;
58  }
59 
60  // Big operation switch
61  Ptr<TextDetectorCNN> obj = obj_[id];
62  if (obj.empty())
63  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
64  if (method == "delete") {
65  nargchk(nrhs==2 && nlhs==0);
66  obj_.erase(id);
67  mexUnlock();
68  }
69  else if (method == "detect") {
70  nargchk(nrhs==3 && nlhs<=2);
71  Mat inputImage(rhs[2].toMat(CV_8U));
72  vector<Rect> bbox;
73  vector<float> confidence;
74  obj->detect(inputImage, bbox, confidence);
75  plhs[0] = MxArray(bbox);
76  if (nlhs > 1)
77  plhs[1] = MxArray(confidence);
78  }
79  else
80  mexErrMsgIdAndTxt("mexopencv:error",
81  "Unrecognized operation %s", method.c_str());
82 }
map< int, Ptr< TextDetectorCNN > > obj_
Object container.
int last_id
Last object id to allocate.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual void detect(InputArray inputImage, std::vector< Rect > &Bbox, std::vector< float > &confidence)=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...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
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
Size2i Size
STL class.
bool empty() const
Global constant definitions.
T c_str(T... args)
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
cv::Mat toMat() const