mexopencv  3.4.1
MEX interface for OpenCV library
BundleAdjuster_.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<=2);
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 == "waveCorrect") {
52  nargchk(nrhs>=3 && nlhs<=1);
54  for (int i=3; i<nrhs; i+=2) {
55  string key(rhs[i].toString());
56  if (key == "Kind")
57  kind = WaveCorrectionMap[rhs[i+1].toString()];
58  else
59  mexErrMsgIdAndTxt("mexopencv:error",
60  "Unrecognized option %s", key.c_str());
61  }
62  vector<Mat> rmats;
63  {
64  vector<MxArray> arr(rhs[2].toVector<MxArray>());
65  rmats.reserve(arr.size());
66  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
67  rmats.push_back(it->toMat(it->isDouble() ? CV_64F : CV_32F));
68  }
69  waveCorrect(rmats, kind);
70  plhs[0] = MxArray(rmats);
71  return;
72  }
73 
74  // Big operation switch
75  Ptr<BundleAdjusterBase> obj = obj_[id];
76  if (obj.empty())
77  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
78  if (method == "delete") {
79  nargchk(nrhs==2 && nlhs==0);
80  obj_.erase(id);
81  mexUnlock();
82  }
83  else if (method == "typeid") {
84  nargchk(nrhs==2 && nlhs<=1);
85  plhs[0] = MxArray(string(typeid(*obj).name()));
86  }
87  else if (method == "refine") {
88  nargchk(nrhs==5 && nlhs<=2);
90  vector<MatchesInfo> pairwise_matches(MxArrayToVectorMatchesInfo(rhs[3]));
92  bool success = obj->operator()(features, pairwise_matches, cameras);
93  if (nlhs > 1)
94  plhs[1] = MxArray(success);
95  else if (!success)
96  mexErrMsgIdAndTxt("mexopencv:error", "Bundle adjustment failed");
97  plhs[0] = toStruct(cameras);
98  }
99  else if (method == "get") {
100  nargchk(nrhs==3 && nlhs<=1);
101  string prop(rhs[2].toString());
102  if (prop == "ConfThresh")
103  plhs[0] = MxArray(obj->confThresh());
104  else if (prop == "RefinementMask")
105  plhs[0] = MxArray(obj->refinementMask());
106  else if (prop == "TermCriteria")
107  plhs[0] = MxArray(obj->termCriteria());
108  else
109  mexErrMsgIdAndTxt("mexopencv:error",
110  "Unrecognized property %s", prop.c_str());
111  }
112  else if (method == "set") {
113  nargchk(nrhs==4 && nlhs==0);
114  string prop(rhs[2].toString());
115  if (prop == "ConfThresh")
116  obj->setConfThresh(rhs[3].toDouble());
117  else if (prop == "RefinementMask")
118  obj->setRefinementMask(rhs[3].toMat(CV_8U));
119  else if (prop == "TermCriteria")
120  obj->setTermCriteria(rhs[3].toTermCriteria());
121  else
122  mexErrMsgIdAndTxt("mexopencv:error",
123  "Unrecognized property %s", prop.c_str());
124  }
125  else
126  mexErrMsgIdAndTxt("mexopencv:error",
127  "Unrecognized operation %s", method.c_str());
128 }
void waveCorrect(std::vector< Mat > &rmats, WaveCorrectKind kind)
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.
std::vector< cv::detail::CameraParams > MxArrayToVectorCameraParams(const MxArray &arr)
Convert MxArray to std::vector<cv::details::CameraParams>
T end(T... args)
WaveCorrectKind
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.
int last_id
Last object id to allocate.
T push_back(T... args)
#define CV_32F
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
#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...
void setTermCriteria(const TermCriteria &term_criteria)
void setConfThresh(double conf_thresh)
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
void setRefinementMask(const Mat &mask)
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
const ConstMap< std::string, cv::detail::WaveCorrectKind > WaveCorrectionMap
wave correction kinds
T size(T... args)
STL class.
bool empty() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
map< int, Ptr< BundleAdjusterBase > > obj_
Object container.
std::vector< cv::detail::ImageFeatures > MxArrayToVectorImageFeatures(const MxArray &arr)
Convert MxArray std::vector<cv::details::ImageFeatures>
const Mat refinementMask() const
cv::Mat toMat() const
cv::Ptr< cv::detail::BundleAdjusterBase > createBundleAdjusterBase(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of BundleAdjusterBase using options in arguments.
T reserve(T... args)