mexopencv  0.1
mex interface for opencv library
findHomography.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 namespace {
15  ("0", 0)
16  ("Ransac", cv::RANSAC)
17  ("LMedS", cv::LMEDS)
18  ("Rho", cv::RHO);
19 }
20 
28 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
29 {
30  // Check the number of arguments
31  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=2);
32 
33  // Argument vector
34  vector<MxArray> rhs(prhs, prhs+nrhs);
35 
36  // Option processing
37  int method = 0;
38  double ransacReprojThreshold = 3.0;
39  int maxIters = 2000;
40  double confidence = 0.995;
41  for (int i=2; i<nrhs; i+=2) {
42  string key(rhs[i].toString());
43  if (key=="Method")
44  method = (rhs[i+1].isChar()) ?
45  Method[rhs[i+1].toString()] : rhs[i+1].toInt();
46  else if (key=="RansacReprojThreshold")
47  ransacReprojThreshold = rhs[i+1].toDouble();
48  else if (key=="MaxIters")
49  maxIters = rhs[i+1].toInt();
50  else if (key=="Confidence")
51  confidence = rhs[i+1].toDouble();
52  else
53  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
54  }
55 
56  // Process
57  Mat mask, H;
58  if (rhs[0].isNumeric() && rhs[1].isNumeric()) {
59  Mat points1(rhs[0].toMat(CV_32F).reshape(2,0)), // CV_32FC2
60  points2(rhs[1].toMat(CV_32F).reshape(2,0));
61  H = findHomography(points1, points2, method, ransacReprojThreshold,
62  (nlhs>1 ? mask : noArray()), maxIters, confidence);
63  }
64  else if (rhs[0].isCell() && rhs[1].isCell()) {
65  vector<Point2f> points1(rhs[0].toVector<Point2f>()),
66  points2(rhs[1].toVector<Point2f>());
67  H = findHomography(points1, points2, method, ransacReprojThreshold,
68  (nlhs>1 ? mask : noArray()), maxIters, confidence);
69  }
70  else
71  mexErrMsgIdAndTxt("mexopencv:error","Invalid argument");
72  plhs[0] = MxArray(H);
73  if (nlhs>1)
74  plhs[1] = MxArray(mask);
75 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927