mexopencv  3.4.1
MEX interface for OpenCV library
Estimator_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 #include "opencv2/stitching.hpp"
12 #include <typeinfo>
13 using namespace std;
14 using namespace cv;
15 using namespace cv::detail;
16 
17 // Persistent objects
18 namespace {
20 int last_id = 0;
23 }
24 
32 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
33 {
34  // Check the number of arguments
35  nargchk(nrhs>=2 && nlhs<=4);
36 
37  // Argument vector
38  vector<MxArray> rhs(prhs, prhs+nrhs);
39  int id = rhs[0].toInt();
40  string method(rhs[1].toString());
41 
42  // Constructor is called. Create a new object from argument
43  if (method == "new") {
44  nargchk(nrhs>=3 && nlhs<=1);
46  rhs[2].toString(), rhs.begin() + 3, rhs.end());
47  plhs[0] = MxArray(last_id);
48  mexLock();
49  return;
50  }
51  // static methods
52  else if (method == "focalsFromHomography") {
53  nargchk(nrhs==3 && nlhs<=4);
54  Mat H(rhs[2].toMat(CV_64F));
55  double f0, f1;
56  bool f0_ok, f1_ok;
57  focalsFromHomography(H, f0, f1, f0_ok, f1_ok);
58  if (nlhs > 2)
59  plhs[2] = MxArray(f0_ok);
60  else if (!f0_ok)
61  mexErrMsgIdAndTxt("mexopencv:error",
62  "Estimated focal length along X-axis failed");
63  if (nlhs > 3)
64  plhs[3] = MxArray(f1_ok);
65  else if (!f1_ok)
66  mexErrMsgIdAndTxt("mexopencv:error",
67  "Estimated focal length along Y-axis failed");
68  plhs[0] = MxArray(f0);
69  if (nlhs > 1)
70  plhs[1] = MxArray(f1);
71  return;
72  }
73  else if (method == "estimateFocal") {
74  nargchk(nrhs==4 && nlhs<=1);
76  vector<MatchesInfo> pairwise_matches(MxArrayToVectorMatchesInfo(rhs[3]));
77  vector<double> focals;
78  estimateFocal(features, pairwise_matches, focals);
79  plhs[0] = MxArray(focals);
80  return;
81  }
82  else if (method == "calibrateRotatingCamera") {
83  nargchk(nrhs==3 && nlhs<=2);
84  vector<Mat> Hs;
85  {
86  vector<MxArray> arr(rhs[2].toVector<MxArray>());
87  Hs.reserve(arr.size());
88  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
89  Hs.push_back(it->toMat(CV_64F));
90  }
91  Mat K;
92  bool success = calibrateRotatingCamera(Hs, K);
93  if (nlhs > 1)
94  plhs[1] = MxArray(success);
95  else if (!success)
96  mexErrMsgIdAndTxt("mexopencv:error",
97  "Calibrating rotating Camera failed");
98  plhs[0] = MxArray(K);
99  return;
100  }
101 
102  // Big operation switch
103  Ptr<Estimator> obj = obj_[id];
104  if (obj.empty())
105  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
106  if (method == "delete") {
107  nargchk(nrhs==2 && nlhs==0);
108  obj_.erase(id);
109  mexUnlock();
110  }
111  else if (method == "typeid") {
112  nargchk(nrhs==2 && nlhs<=1);
113  plhs[0] = MxArray(string(typeid(*obj).name()));
114  }
115  else if (method == "estimate") {
116  nargchk(nrhs==4 && nlhs<=2);
118  vector<MatchesInfo> pairwise_matches(MxArrayToVectorMatchesInfo(rhs[3]));
119  vector<CameraParams> cameras;
120  bool success = obj->operator()(features, pairwise_matches, cameras);
121  if (nlhs > 1)
122  plhs[1] = MxArray(success);
123  else if (!success)
124  mexErrMsgIdAndTxt("mexopencv:error", "Estimation failed");
125  plhs[0] = toStruct(cameras);
126  }
127  else
128  mexErrMsgIdAndTxt("mexopencv:error",
129  "Unrecognized operation %s", method.c_str());
130 }
bool calibrateRotatingCamera(const std::vector< Mat > &Hs, Mat &K)
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>
cv::Ptr< cv::detail::Estimator > createEstimator(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of Estimator using options in arguments.
STL namespace.
T end(T... args)
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.
T push_back(T... args)
void focalsFromHomography(const Mat &H, double &f0, double &f1, bool &f0_ok, bool &f1_ok)
#define CV_64F
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
T size(T... args)
STL class.
bool empty() const
Global constant definitions.
T begin(T... args)
int last_id
Last object id to allocate.
Definition: Estimator_.cpp:20
T c_str(T... args)
map< int, Ptr< Estimator > > obj_
Object container.
Definition: Estimator_.cpp:22
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: Estimator_.cpp:32
void estimateFocal(const std::vector< ImageFeatures > &features, const std::vector< MatchesInfo > &pairwise_matches, std::vector< double > &focals)
std::vector< cv::detail::ImageFeatures > MxArrayToVectorImageFeatures(const MxArray &arr)
Convert MxArray std::vector<cv::details::ImageFeatures>
cv::Mat toMat() const
T reserve(T... args)