mexopencv  0.1
mex interface for opencv library
estimateGlobalMotionRansac.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/videostab.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::videostab;
13 
14 namespace {
17  ("Translation", cv::videostab::MM_TRANSLATION)
18  ("TranslationAndScale", cv::videostab::MM_TRANSLATION_AND_SCALE)
19  ("Rotation", cv::videostab::MM_ROTATION)
20  ("Rigid", cv::videostab::MM_RIGID)
21  ("Similarity", cv::videostab::MM_SIMILARITY)
22  ("Affine", cv::videostab::MM_AFFINE)
23  ("Homography", cv::videostab::MM_HOMOGRAPHY)
24  ("Unknown", cv::videostab::MM_UNKNOWN);
25 
30 RansacParams toRansacParams(const MxArray &arr)
31 {
32  return RansacParams(
33  arr.at("Size").toInt(),
34  arr.at("Thresh").toFloat(),
35  arr.at("Eps").toFloat(),
36  arr.at("Prob").toFloat());
37 }
38 }
39 
47 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
48 {
49  // Check the number of arguments
50  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=3);
51 
52  // Argument vector
53  vector<MxArray> rhs(prhs, prhs+nrhs);
54 
55  // Option processing
56  int model = cv::videostab::MM_AFFINE;
57  RansacParams params = RansacParams::default2dMotion(cv::videostab::MM_AFFINE);
58  for (int i=2; i<nrhs; i+=2) {
59  string key(rhs[i].toString());
60  if (key == "MotionModel")
61  model = MotionModelMap[rhs[i+1].toString()];
62  else if (key == "RansacParams")
63  params = (rhs[i+1].isStruct() ? toRansacParams(rhs[i+1]) :
64  RansacParams::default2dMotion(MotionModelMap[rhs[i+1].toString()]));
65  else
66  mexErrMsgIdAndTxt("mexopencv:error",
67  "Unrecognized option %s", key.c_str());
68  }
69 
70  // Process
71  Mat M;
72  float rmse = 0;
73  int ninliers = 0;
74  if (rhs[0].isNumeric() && rhs[1].isNumeric()) {
75  Mat points0(rhs[0].toMat(CV_32F)),
76  points1(rhs[1].toMat(CV_32F));
77  M = estimateGlobalMotionRansac(points0, points1, model, params,
78  &rmse, &ninliers);
79  }
80  else if (rhs[0].isCell() && rhs[1].isCell()) {
81  vector<Point2f> points0(rhs[0].toVector<Point2f>()),
82  points1(rhs[1].toVector<Point2f>());
83  M = estimateGlobalMotionRansac(points0, points1, model, params,
84  &rmse, &ninliers);
85  }
86  else
87  mexErrMsgIdAndTxt("mexopencv:error","Invalid argument");
88  plhs[0] = MxArray(M);
89  if (nlhs > 1)
90  plhs[1] = MxArray(rmse);
91  if (nlhs > 2)
92  plhs[2] = MxArray(ninliers);
93 }
cv::videostab::RansacParams toRansacParams(const MxArray &arr)
Convert MxArray to RansacParams.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
const ConstMap< std::string, cv::videostab::MotionModel > MotionModelMap
motion model types for option processing
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.
T at(mwIndex index) const
Template for numeric array element accessor.
Definition: MxArray.hpp:1250
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927