mexopencv  3.4.1
MEX interface for OpenCV library
KeyPointsFilter_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/features2d.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 namespace {
18 MxArray toMxArray(size_t i)
19 {
21  if (arr.isNull())
22  mexErrMsgIdAndTxt("mexopencv:error", "Allocation error");
23  arr.set(0, static_cast<uint64_t>(i));
24  return arr;
25 }
26 }
27 
35 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
36 {
37  // Check the number of arguments
38  nargchk(nrhs>=1 && nlhs<=1);
39 
40  // Argument vector
41  vector<MxArray> rhs(prhs, prhs+nrhs);
42  string method(rhs[0].toString());
43 
44  // Operation switch
45  if (method == "removeDuplicated") {
46  nargchk(nrhs==2 && nlhs<=1);
47  vector<KeyPoint> keypoints(rhs[1].toVector<KeyPoint>());
48  KeyPointsFilter::removeDuplicated(keypoints);
49  plhs[0] = MxArray(keypoints);
50  }
51  else if (method == "removeDuplicatedSorted") {
52  nargchk(nrhs==2 && nlhs<=1);
53  vector<KeyPoint> keypoints(rhs[1].toVector<KeyPoint>());
54  KeyPointsFilter::removeDuplicatedSorted(keypoints);
55  plhs[0] = MxArray(keypoints);
56  }
57  else if (method == "retainBest") {
58  nargchk(nrhs==3 && nlhs<=1);
59  vector<KeyPoint> keypoints(rhs[1].toVector<KeyPoint>());
60  int npoints = rhs[2].toInt();
61  KeyPointsFilter::retainBest(keypoints, npoints);
62  plhs[0] = MxArray(keypoints);
63  }
64  else if (method == "runByImageBorder") {
65  nargchk(nrhs==4 && nlhs<=1);
66  vector<KeyPoint> keypoints(rhs[1].toVector<KeyPoint>());
67  Size imageSize(rhs[2].toSize());
68  int borderSize = rhs[3].toInt();
69  KeyPointsFilter::runByImageBorder(keypoints, imageSize, borderSize);
70  plhs[0] = MxArray(keypoints);
71  }
72  else if (method == "runByKeypointSize") {
73  nargchk((nrhs==3 || nrhs==4) && nlhs<=1);
74  vector<KeyPoint> keypoints(rhs[1].toVector<KeyPoint>());
75  float minSize = rhs[2].toFloat();
76  float maxSize = (nrhs==4) ? rhs[3].toFloat() : FLT_MAX;
77  KeyPointsFilter::runByKeypointSize(keypoints, minSize, maxSize);
78  plhs[0] = MxArray(keypoints);
79  }
80  else if (method == "runByPixelsMask") {
81  nargchk(nrhs==3 && nlhs<=1);
82  vector<KeyPoint> keypoints(rhs[1].toVector<KeyPoint>());
83  Mat mask(rhs[2].toMat(CV_8U));
84  KeyPointsFilter::runByPixelsMask(keypoints, mask);
85  plhs[0] = MxArray(keypoints);
86  }
87  else if (method == "convertToPoints") {
88  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
89  vector<int> keypointIndexes;
90  for (int i=2; i<nrhs; i+=2) {
91  string key(rhs[i].toString());
92  if (key == "Indices")
93  keypointIndexes = rhs[i+1].toVector<int>();
94  else
95  mexErrMsgIdAndTxt("mexopencv:error",
96  "Unrecognized option %s", key.c_str());
97  }
98  vector<KeyPoint> keypoints(rhs[1].toVector<KeyPoint>());
99  vector<Point2f> points2f;
100  KeyPoint::convert(keypoints, points2f, keypointIndexes);
101  plhs[0] = MxArray(points2f);
102  }
103  else if (method == "convertFromPoints") {
104  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
105  float size = 1.0f;
106  float response = 1.0f;
107  int octave = 0;
108  int class_id = -1;
109  for (int i=2; i<nrhs; i+=2) {
110  string key(rhs[i].toString());
111  if (key == "Size")
112  size = rhs[i+1].toFloat();
113  else if (key == "Response")
114  response = rhs[i+1].toFloat();
115  else if (key == "Octave")
116  octave = rhs[i+1].toInt();
117  else if (key == "ClassId")
118  class_id = rhs[i+1].toInt();
119  else
120  mexErrMsgIdAndTxt("mexopencv:error",
121  "Unrecognized option %s", key.c_str());
122  }
123  vector<Point2f> points2f(rhs[1].toVector<Point2f>());
124  vector<KeyPoint> keypoints;
125  KeyPoint::convert(points2f, keypoints,
126  size, response, octave, class_id);
127  plhs[0] = MxArray(keypoints);
128  }
129  else if (method == "overlap") {
130  nargchk(nrhs==3 && nlhs<=1);
131  KeyPoint kp1 = rhs[1].toKeyPoint(),
132  kp2 = rhs[2].toKeyPoint();
133  float ovrl = KeyPoint::overlap(kp1, kp2);
134  plhs[0] = MxArray(ovrl);
135  }
136  else if (method == "hash") {
137  nargchk(nrhs==2 && nlhs<=1);
138  KeyPoint kp = rhs[1].toKeyPoint();
139  size_t val = kp.hash();
140  plhs[0] = toMxArray(val);
141  }
142  else
143  mexErrMsgIdAndTxt("mexopencv:error",
144  "Unrecognized operation %s", method.c_str());
145 }
MxArray toMxArray(size_t i)
Convert size type to MxArray.
size_t hash() const
#define CV_8U
Identifies a numeric mxArray whose data is stored as the type specified in the MATLAB Primitive Types...
Definition: matrix.h:306
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
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...
#define mxCreateNumericMatrix
Definition: matrix.h:1516
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
STL class.
Global constant definitions.
T c_str(T... args)
bool isNull() const
Determine whether the array is initialized or not.
Definition: MxArray.hpp:606
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
cv::Mat toMat() const
size_t size() const
Identifies an mxArray with no imaginary components.
Definition: matrix.h:325