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