mexopencv  3.4.1
MEX interface for OpenCV library
calcOpticalFlowPyrLK.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/video.hpp"
10 using namespace std;
11 using namespace cv;
12 
20 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
21 {
22  // Check the number of arguments
23  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=3);
24 
25  // Argument vector
26  vector<MxArray> rhs(prhs, prhs+nrhs);
27 
28  // Option processing
29  vector<Point2f> nextPts;
30  Size winSize(21,21);
31  int maxLevel = 3;
32  TermCriteria criteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01);
33  int flags = 0;
34  double minEigThreshold = 1e-4;
35  for (int i=3; i<nrhs; i+=2) {
36  string key(rhs[i].toString());
37  if (key == "InitialFlow") {
38  nextPts = rhs[i+1].toVector<Point2f>();
40  }
41  else if (key == "WinSize")
42  winSize = rhs[i+1].toSize();
43  else if (key == "MaxLevel")
44  maxLevel = rhs[i+1].toInt();
45  else if (key == "Criteria")
46  criteria = rhs[i+1].toTermCriteria();
47  else if (key == "GetMinEigenvals")
48  UPDATE_FLAG(flags, rhs[i+1].toBool(), cv::OPTFLOW_LK_GET_MIN_EIGENVALS);
49  else if (key == "MinEigThreshold")
50  minEigThreshold = rhs[i+1].toDouble();
51  else
52  mexErrMsgIdAndTxt("mexopencv:error",
53  "Unrecognized option %s", key.c_str());
54  }
55 
56  // Process
57  vector<Point2f> prevPts(rhs[2].toVector<Point2f>());
58  Mat status, err;
59  if (rhs[0].isNumeric() && rhs[1].isNumeric()) {
60  // images
61  Mat prevImg(rhs[0].toMat(CV_8U)),
62  nextImg(rhs[1].toMat(CV_8U));
63  calcOpticalFlowPyrLK(prevImg, nextImg, prevPts, nextPts, status,
64  (nlhs>2 ? err : noArray()), winSize, maxLevel, criteria, flags,
65  minEigThreshold);
66  }
67  else if (rhs[0].isCell() && rhs[1].isCell()) {
68  // pyramids
69  vector<Mat> prevImg(rhs[0].toVector<Mat>()),
70  nextImg(rhs[1].toVector<Mat>());
71  calcOpticalFlowPyrLK(prevImg, nextImg, prevPts, nextPts, status,
72  (nlhs>2 ? err : noArray()), winSize, maxLevel, criteria, flags,
73  minEigThreshold);
74  }
75  else
76  mexErrMsgIdAndTxt("mexopencv:error", "Invalid image argument");
77  plhs[0] = MxArray(nextPts);
78  if (nlhs>1)
79  plhs[1] = MxArray(status);
80  if (nlhs>2)
81  plhs[2] = MxArray(err);
82 }
#define CV_8U
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
void calcOpticalFlowPyrLK(InputArray prevImg, InputArray nextImg, InputArray prevPts, InputOutputArray nextPts, OutputArray status, OutputArray err, Size winSize=Size(21, 21), int maxLevel=3, TermCriteria criteria=TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01), int flags=0, double minEigThreshold=1e-4)
InputOutputArray noArray()
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
Definition: mexopencv.hpp:174
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...
OPTFLOW_LK_GET_MIN_EIGENVALS
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
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
STL class.
Global constant definitions.
T c_str(T... args)
cv::Mat toMat() const
OPTFLOW_USE_INITIAL_FLOW