mexopencv  0.1
mex interface for opencv library
KeyPointsFilter_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
19 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
20 {
21  // Check the number of arguments
22  nargchk(nrhs>=1 && nlhs<=1);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs+nrhs);
26  string method(rhs[0].toString());
27 
28  // Operation switch
29  if (method == "removeDuplicated") {
30  nargchk(nrhs==2 && nlhs<=1);
31  vector<KeyPoint> keypoints(rhs[1].toVector<KeyPoint>());
32  KeyPointsFilter::removeDuplicated(keypoints);
33  plhs[0] = MxArray(keypoints);
34  }
35  else if (method == "retainBest") {
36  nargchk(nrhs==3 && nlhs<=1);
37  vector<KeyPoint> keypoints(rhs[1].toVector<KeyPoint>());
38  int npoints = rhs[2].toInt();
39  KeyPointsFilter::retainBest(keypoints, npoints);
40  plhs[0] = MxArray(keypoints);
41  }
42  else if (method == "runByImageBorder") {
43  nargchk(nrhs==4 && nlhs<=1);
44  vector<KeyPoint> keypoints(rhs[1].toVector<KeyPoint>());
45  Size imageSize(rhs[2].toSize());
46  int borderSize = rhs[3].toInt();
47  KeyPointsFilter::runByImageBorder(keypoints, imageSize, borderSize);
48  plhs[0] = MxArray(keypoints);
49  }
50  else if (method == "runByKeypointSize") {
51  nargchk((nrhs==3 || nrhs==4) && nlhs<=1);
52  vector<KeyPoint> keypoints(rhs[1].toVector<KeyPoint>());
53  float minSize = rhs[2].toFloat();
54  float maxSize = (nrhs==4) ? rhs[3].toFloat() : FLT_MAX;
55  KeyPointsFilter::runByKeypointSize(keypoints, minSize, maxSize);
56  plhs[0] = MxArray(keypoints);
57  }
58  else if (method == "runByPixelsMask") {
59  nargchk(nrhs==3 && nlhs<=1);
60  vector<KeyPoint> keypoints(rhs[1].toVector<KeyPoint>());
61  Mat mask(rhs[2].toMat(CV_8U));
62  KeyPointsFilter::runByPixelsMask(keypoints, mask);
63  plhs[0] = MxArray(keypoints);
64  }
65  else if (method == "convertToPoints") {
66  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
67  vector<int> keypointIndexes;
68  for (int i=2; i<nrhs; i+=2) {
69  string key(rhs[i].toString());
70  if (key == "Indices")
71  keypointIndexes = rhs[i+1].toVector<int>();
72  else
73  mexErrMsgIdAndTxt("mexopencv:error",
74  "Unrecognized option %s", key.c_str());
75  }
76  vector<KeyPoint> keypoints(rhs[1].toVector<KeyPoint>());
77  vector<Point2f> points2f;
78  KeyPoint::convert(keypoints, points2f, keypointIndexes);
79  plhs[0] = MxArray(points2f);
80  }
81  else if (method == "convertFromPoints") {
82  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
83  float size = 1.0f;
84  float response = 1.0f;
85  int octave = 1;
86  int class_id = -1;
87  for (int i=2; i<nrhs; i+=2) {
88  string key(rhs[i].toString());
89  if (key == "Size")
90  size = rhs[i+1].toFloat();
91  else if (key == "Response")
92  response = rhs[i+1].toFloat();
93  else if (key == "Octave")
94  octave = rhs[i+1].toInt();
95  else if (key == "ClassId")
96  class_id = rhs[i+1].toInt();
97  else
98  mexErrMsgIdAndTxt("mexopencv:error",
99  "Unrecognized option %s", key.c_str());
100  }
101  vector<Point2f> points2f(rhs[1].toVector<Point2f>());
102  vector<KeyPoint> keypoints;
103  KeyPoint::convert(points2f, keypoints,
104  size, response, octave, class_id);
105  plhs[0] = MxArray(keypoints);
106  }
107  else if (method == "overlap") {
108  nargchk(nrhs==3 && nlhs<=1);
109  KeyPoint kp1 = rhs[1].toKeyPoint(),
110  kp2 = rhs[2].toKeyPoint();
111  float ovrl = KeyPoint::overlap(kp1, kp2);
112  plhs[0] = MxArray(ovrl);
113  }
114  else if (method == "hash") {
115  nargchk(nrhs==2 && nlhs<=1);
116  KeyPoint kp = rhs[1].toKeyPoint();
117  size_t val = kp.hash();
118  //plhs[0] = MxArray(val);
119  plhs[0] = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
120  {
121  uint64_t *data = reinterpret_cast<uint64_t*>(mxGetData(plhs[0]));
122  data[0] = static_cast<uint64_t>(val);
123  }
124  }
125  else
126  mexErrMsgIdAndTxt("mexopencv:error",
127  "Unrecognized operation %s", method.c_str());
128 }
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.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.