mexopencv  3.4.1
MEX interface for OpenCV library
FeaturesMatcher_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 #include "opencv2/stitching.hpp"
11 #include <typeinfo>
12 using namespace std;
13 using namespace cv;
14 using namespace cv::detail;
15 
16 // Persistent objects
17 namespace {
19 int last_id = 0;
22 }
23 
31 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
32 {
33  // Check the number of arguments
34  nargchk(nrhs>=2 && nlhs<=1);
35 
36  // Argument vector
37  vector<MxArray> rhs(prhs, prhs+nrhs);
38  int id = rhs[0].toInt();
39  string method(rhs[1].toString());
40 
41  // Constructor is called. Create a new object from argument
42  if (method == "new") {
43  nargchk(nrhs>=3 && nlhs<=1);
45  rhs[2].toString(), rhs.begin() + 3, rhs.end());
46  plhs[0] = MxArray(last_id);
47  mexLock();
48  return;
49  }
50  // static methods
51  else if (method == "matchesGraphAsString") {
52  nargchk(nrhs==4 && nlhs<=1);
53  vector<MatchesInfo> pairwise_matches(MxArrayToVectorMatchesInfo(rhs[2]));
54  float conf_threshold = rhs[3].toFloat();
55  vector<String> pathes;
56  pathes.reserve(pairwise_matches.size());
57  for (int i=0; i<pairwise_matches.size(); ++i) {
58  ostringstream ss;
59  ss << "img" << (i+1);
60  pathes.push_back(ss.str());
61  }
62  string str(matchesGraphAsString(
63  pathes, pairwise_matches, conf_threshold));
64  plhs[0] = MxArray(str);
65  return;
66  }
67  else if (method == "leaveBiggestComponent") {
68  nargchk(nrhs==5 && nlhs<=1);
70  vector<MatchesInfo> pairwise_matches(MxArrayToVectorMatchesInfo(rhs[3]));
71  float conf_threshold = rhs[4].toFloat();
73  features, pairwise_matches, conf_threshold));
74  plhs[0] = MxArray(indices);
75  return;
76  }
77 
78  // Big operation switch
79  Ptr<FeaturesMatcher> obj = obj_[id];
80  if (obj.empty())
81  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
82  if (method == "delete") {
83  nargchk(nrhs==2 && nlhs==0);
84  obj_.erase(id);
85  mexUnlock();
86  }
87  else if (method == "typeid") {
88  nargchk(nrhs==2 && nlhs<=1);
89  plhs[0] = MxArray(string(typeid(*obj).name()));
90  }
91  else if (method == "collectGarbage") {
92  nargchk(nrhs==2 && nlhs==0);
93  obj->collectGarbage();
94  }
95  else if (method == "isThreadSafe") {
96  nargchk(nrhs==2 && nlhs<=1);
97  bool tf = obj->isThreadSafe();
98  plhs[0] = MxArray(tf);
99  }
100  else if (method == "match") {
101  nargchk(nrhs==4 && nlhs<=1);
102  ImageFeatures features1(MxArrayToImageFeatures(rhs[2])),
103  features2(MxArrayToImageFeatures(rhs[3]));
104  MatchesInfo matches_info;
105  obj->operator()(features1, features2, matches_info);
106  plhs[0] = toStruct(matches_info);
107  }
108  else if (method == "match_pairwise") {
109  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
110  Mat mask;
111  for (int i=3; i<nrhs; i+=2) {
112  string key(rhs[i].toString());
113  if (key == "Mask")
114  mask = rhs[i+1].toMat(CV_8U);
115  else
116  mexErrMsgIdAndTxt("mexopencv:error",
117  "Unrecognized option %s", key.c_str());
118  }
120  vector<MatchesInfo> pairwise_matches;
121  obj->operator()(features, pairwise_matches, mask.getUMat(ACCESS_READ));
122  plhs[0] = toStruct(pairwise_matches);
123  }
124  else
125  mexErrMsgIdAndTxt("mexopencv:error",
126  "Unrecognized operation %s", method.c_str());
127 }
Common definitions for the stitching module.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
std::vector< cv::detail::MatchesInfo > MxArrayToVectorMatchesInfo(const MxArray &arr)
Convert MxArray to std::vector<cv::details::MatchesInfo>
#define CV_8U
STL namespace.
T end(T... args)
int last_id
Last object id to allocate.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
MxArray toStruct(const std::vector< cv::ml::DTrees::Node > &nodes)
Convert tree nodes to struct array.
map< int, Ptr< FeaturesMatcher > > obj_
Object container.
T push_back(T... args)
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...
T str(T... args)
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
T size(T... args)
STL class.
bool empty() const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.
T begin(T... args)
ACCESS_READ
T c_str(T... args)
std::vector< int > leaveBiggestComponent(std::vector< ImageFeatures > &features, std::vector< MatchesInfo > &pairwise_matches, float conf_threshold)
cv::detail::ImageFeatures MxArrayToImageFeatures(const MxArray &arr, mwIndex idx=0)
Convert MxArray to cv::details::ImageFeatures.
std::vector< cv::detail::ImageFeatures > MxArrayToVectorImageFeatures(const MxArray &arr)
Convert MxArray std::vector<cv::details::ImageFeatures>
UMat getUMat(int accessFlags, UMatUsageFlags usageFlags=USAGE_DEFAULT) const
String matchesGraphAsString(std::vector< String > &pathes, std::vector< MatchesInfo > &pairwise_matches, float conf_threshold)
cv::Ptr< cv::detail::FeaturesMatcher > createFeaturesMatcher(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of FeaturesMatcher using options in arguments.
virtual void collectGarbage()
T reserve(T... args)