mexopencv  3.4.1
MEX interface for OpenCV library
InferBbox.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::dnn_objdetect;
13 
14 namespace {
20 {
21  const char *fields[4] = {"bbox", "class_idx", "label_name", "class_prob"};
22  MxArray s = MxArray::Struct(fields, 4, 1, detections.size());
23  for (mwIndex i = 0; i < detections.size(); ++i) {
24  int bbox[4] = {detections[i].xmin, detections[i].ymin,
25  detections[i].xmax, detections[i].ymax};
26  s.set(fields[0], vector<int>(bbox, bbox+4), i);
27  s.set(fields[1], static_cast<int>(detections[i].class_idx), i);
28  s.set(fields[2], detections[i].label_name, i);
29  s.set(fields[3], detections[i].class_prob, i);
30  }
31  return s;
32 }
33 }
34 
42 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
43 {
44  // Check the number of arguments
45  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
46 
47  // Argument vector
48  vector<MxArray> rhs(prhs, prhs+nrhs);
49 
50  // Option processing
51  double thresh = 0.8;
52  for (int i=3; i<nrhs; i+=2) {
53  string key(rhs[i].toString());
54  if (key == "Threshold")
55  thresh = rhs[i+1].toDouble();
56  else
57  mexErrMsgIdAndTxt("mexopencv:error",
58  "Unrecognized option %s", key.c_str());
59  }
60 
61  // Process
62  MatND delta_bbox(rhs[0].toMatND(CV_32F)),
63  class_scores(rhs[1].toMatND(CV_32F)),
64  conf_scores(rhs[2].toMatND(CV_32F));
65  InferBbox inf(delta_bbox, class_scores, conf_scores);
66  inf.filter(thresh);
67  plhs[0] = toStruct(inf.detections);
68 }
static softfloat inf()
MxArray toStruct(const vector< cv::dnn_objdetect::object > &detections)
Convert detections to struct array.
Definition: InferBbox.cpp:19
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
void set(mwIndex index, const T &value)
Template for numeric array element write accessor.
Definition: MxArray.hpp:1310
#define CV_32F
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...
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
static MxArray Struct(const char **fields=NULL, int nfields=0, mwSize m=1, mwSize n=1)
Create a new struct array.
Definition: MxArray.hpp:312
T size(T... args)
Global constant definitions.
T c_str(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: InferBbox.cpp:42